npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-generator-drizzle

v0.7.6

Published

A Prisma generator for generating Drizzle schema with ease

Downloads

5,012

Readme

prisma-generator-drizzle

Test

prisma-generator-drizzle is a Prisma generator that lets you generate a Drizzle schema. It is specifically designed for (existing) projects that are using Prisma and want to migrate to Drizzle, or for projects that want to use both Prisma and Drizzle together.

In this version, the focus is still on the query and mutation capabilities of the generated Drizzle schema.

Features

https://github.com/farreldarian/prisma-generator-drizzle/assets/62016900/35b57135-614e-4e07-920b-9e9a487eb6cb

  • 🤝 1:1 Prisma to Drizzle schema generation
  • ✨ Compatible with all *scalars, enums, and *constraints
  • 📦 Supports drizzle relational query
  • 🚀 Generates drizzle-specific features like the .$type<..>() method

*Only supports default scalar for now and more constraints will be added in future

This project is still considered as experimental, but you can safely use it for production. Follow the progress on v1.

Get started

Installation

1. Install the package

npm install -D prisma-generator-drizzle
npm install drizzle-orm

2. Add to your Prisma schema

generator drizzle {
  provider = "prisma-generator-drizzle"

  // Specify the output directory
  // output = "./lib/drizzle/models"
}

See configuration for more options.

3. Run the generator

prisma generate

Usages

Note: This generator will use the default Prisma field mapping, meaning any @db.* modifiers will be ignored for now.

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.

In addition to the Prisma features, you can also generate Drizzle-specific features:

Directive syntax is still experimental, feel free to suggest a better approach.

  1. Generate .$defaultFn<..>()
  2. Generate .$type<..>()

Configuration

| Key | Description | Default | Example | | --------------- | --------------------------------- | ----------- | ----------- | | output | Change the output | "./drizzle" | "../models" | | | Generate single output file | | "drizzle.ts" | | formatter | Run prettier 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/*',
})

Generate .$defaultFn() Custom Default Initializer

⚠️ DEPRECATED , will be replace by drizzle.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

⚠️ DEPRECATED , will be replace by drizzle.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>(),
  ...
})

Example

  1. with-drizzle-prisma: using drizzle's prisma extension

Gotchas

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

SqliteError: NOT NULL constraint failed: <table-name>.id

Currently having @default(autoincrement()) only work for postgres and mysql.