npm package: @hatkom/prisma-generator-default-values
Description: Expose default values from your Prisma schema as typed constants
Author: Vladyslav Zavalykhatko
Latest version: 1.0.0

# @hatkom/prisma-generator-default-values

[![npm](https://img.shields.io/npm/v/@hatkom/prisma-generator-default-values.svg)](https://www.npmjs.com/package/@hatkom/prisma-generator-default-values)

A Prisma generator that exposes the `@default(...)` values from your schema as typed, importable constants.

## Why?

Prisma schemas define default values, but there's no way to access them at runtime. If you want to apply defaults in your application layer (e.g. for optimistic UI, form pre-filling, or validation), you end up duplicating them manually.

This generator reads your schema and produces a TypeScript file with all default values exported as `as const` objects.

## Installation

```sh
npm i @hatkom/prisma-generator-default-values
```

## Setup

Add the generator to your `schema.prisma`:

```prisma
generator default_values {
  provider = "prisma-generator-default-values"
  output   = "./generated/defaults.ts"
}
```

Then run:

```sh
npx prisma generate
```

## Example

Given this schema:

```prisma
enum Role {
  USER
  ADMIN
}

model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  active    Boolean  @default(true)
  createdAt DateTime @default(now())
}

model Post {
  id        String   @id @default(uuid())
  title     String
  published Boolean  @default(false)
  views     Int      @default(0)
  authorId  String
  author    User     @relation(fields: [authorId], references: [id])
}
```

The generator produces:

```ts
// Generated by prisma-generator-default-values — do not edit manually

export const UserDefaults = {
  id: { _fn: 'cuid' },
  role: "USER",
  active: true,
  createdAt: { _fn: 'now' },
} as const

export const PostDefaults = {
  id: { _fn: 'uuid' },
  published: false,
  views: 0,
} as const
```

## What gets generated

For each model that has fields with `@default(...)`, a `{Model}Defaults` const object is exported. The `as const` assertion gives you full literal types out of the box.

### Default value representation

| Schema default | Generated value |
|---|---|
| `@default("VALUE")` | `"VALUE"` |
| `@default(true)` | `true` |
| `@default(0)` | `0` |
| `@default(ENUM_VALUE)` | `"ENUM_VALUE"` |
| `@default(now())` | `{ _fn: 'now' }` |
| `@default(uuid())` | `{ _fn: 'uuid' }` |
| `@default(cuid())` | `{ _fn: 'cuid' }` |
| `@default(autoincrement())` | `{ _fn: 'autoincrement' }` |
| `@default(dbgenerated("..."))` | `{ _fn: 'dbgenerated', _args: [...] }` |

Scalar and enum defaults become their literal value. Function-based defaults (like `now()`, `uuid()`) become `{ _fn: 'name' }` objects, with an `_args` array when the function has arguments.

## Usage examples

```ts
import { UserDefaults, PostDefaults } from './generated/defaults'

// Pre-fill a form
const newUser = {
  role: UserDefaults.role,       // "USER"
  active: UserDefaults.active,   // true
}

// Check if a field has a server-generated default
if (typeof UserDefaults.id === 'object' && '_fn' in UserDefaults.id) {
  // id is generated server-side (cuid), don't include in form
}
```

## License

MIT