prisma-generator-drizzle is a Prisma generator that allows you to generate Drizzle schema definitions from Prisma schema. It provides 1:1 functionality mapping, allowing you to use Drizzle as a drop-in replacement for querying and mutating your database.
This tool is ideal for projects transitioning from Prisma to Drizzle, or for those wanting to use both Prisma and Drizzle together. Leveraging the DX of Prisma for defining your schema while still getting the performance benefits and flexibility of Drizzle.
CleanShot.2023-12-25.at.01.11.37.mp4
- ๐ค Compatibility: 1:1 Prisma to Drizzle schema generation*
- โ Relational Query Support: Generates relational query definitions.
- ๐ฆ Cutomizability: Includes tools for customizing drizzle-specific features.
*Only supports default scalar for now and more constraints will be added in future
Note: This project is still considered experimental, but you can safely use it for production. Follow the progress on v1.
npm install -D prisma-generator-drizzle
npm install drizzle-orm
generator drizzle {
provider = "prisma-generator-drizzle"
// Specify the output file (or directory)
output = "./infra/database/drizzle.ts"
}
See configuration for more options.
prisma generate
Key | Description | Default | Example |
---|---|---|---|
output | Generate output directory | "./drizzle" | "../models" |
Generate single output file | "drizzle.ts" | ||
formatter | Run formatter after generation | - | "prettier" |
relationalQuery | Flag to generate relational query | true | false |
moduleResolution | Specify the module resolution that will affect the import style | *auto | nodenext |
verbose | Flag to enable verbose logging | - | true |
abortOnFailedFormatting | Flag to throw exception when formatting fails | true | false |
**dateMode | Change the generated mode for date | "date" |
* It will find the closest tsconfig from the current working directory. Note that extends is not supported
**Does not work with sqlite
Setting up relational query
import { drizzle } from 'drizzle-orm/node-postgres'
// `schema` contains all table and relation definitions
import { schema } from 'prisma/drizzle/schema'
const client = ... // database client
const db = drizzle(client, { schema })
Setting up drizzle-kit
Use the glob pattern (see example 3) to reference the generated table definitions.
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
// Using the default output path
schema: './prisma/drizzle/*',
})
These syntaxes might change in the future, we encourage your feedback.
We will make sure to keep the syntaxes compatible with future versions.
Customize the generated field definition using drizzle.custom
directive in a json format.
This will override any built-in mapping such as @default(...)
or @db.*
modifiers.
Available options:
model Table {
/// drizzle.custom {
/// "imports": [{ "name": ["UserId"], "module": "~/models/user", "type": true }],
/// "$type": "UserId",
/// "default": "() => 1",
/// "field": { "mode": "number" }
/// }
column BigInt
}
-
imports
: Specify the import path for the custom code.Array of imports that will be added to each schema file.
- named import:
[{ "name": ["<exported-names>"], "module": "<path-or-module>>" }, ...]
- default import:
[{ "name": "<exported-default>", "module": "<path-or-module>>" }, ...]
- type import:
[{ ..., "type": true }, ...]
- named import:
-
$type
: Specify the type to use for the field. -
default
: Specify the default value for the field. -
๐๏ธ
field
: Specify the field options.
NOTE: All options are optional. Items marked with ๐๏ธ are incomplete
Generate .$defaultFn()
Custom Default Initializer
โ ๏ธ This may potentially be removed and replaced bydrizzle.custom
directive
Add /// drizzle.default <module>::<named-function-import>
directive above the field definition to generate a custom default initializer.
NOTE: This will override any
@default(...)
attribute from the schema.
model User {
/// drizzle.default @paralleldrive/cuid2::createId
id String @id
...
}
This will result to:
import { createId } from '@paralleldrive/cuid2'
...
export const users = pgTable('User', {
id: text('id')
.$defaultFn(() => createId())
.primaryKey(),
...
})
Or with a custom code
model User {
/// drizzle.default crypto::randomBytes `() => randomBytes(16).toString('hex')`
salt String?
...
}
import { randomBytes } from 'node:crypto'
...
export const users = pgTable('User', {
salt: text('salt')
.$defaultFn(() => randomBytes(16).toString('hex'))
.notNull(),
...
})
Generate .$type<..>()
Type Customization
โ ๏ธ This may potentially be removed and replaced bydrizzle.custom
directive
Add /// drizzle.type <module>::<named-import>
directive above the field definition.
model Wallet {
/// drizzle.type viem::Address
address String?
...
}
This will result to:
import { Wallet } from 'viem'
...
export const wallets = pgTable('Wallet', {
address: text('address').$type<Address>(),
...
})
Or with a relative import
model User {
/// drizzle.type ../my-type::Email
email String?
...
}
import { Email } from '../my-type'
...
export const users = pgTable('User', {
email: text('email').$type<Email>(),
...
})
prisma-generator-drizzle aims for 1:1 compatibility with Prisma, this means that you can use the generated Drizzle schema as a complete and familiar drop-in replacement for the Prisma client.
Note: This generator will use the default Prisma field mapping, meaning any
@db.*
modifiers will be ignored for now.
- Unsupported (๐๏ธ soon)
todo docs
todo docs
- with-drizzle-prisma: using drizzle's prisma extension
Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'.
By default, the generator will try to find the closest tsconfig from the current working directory to determine the import style, whether to add .js
or not. When there's no config found, it will use the common import (e.g. import { users } from './users'
).
You can explicitly set the moduleResolution
option in the generator configuration.
Check also the discussion
Currently having @default(autoincrement())
only work for postgres and mysql.