Drizzle extension for Prisma (beta)

If you have an existing project with Prisma and want to try Drizzle or gradually adopt it, you can use our first-class extension that will add Drizzle API to your Prisma client. It will allow you to use Drizzle alongside your Prisma queries reusing your existing DB connection.

How to use

Install dependencies

You need to install Drizzle itself and a generator package that will create Drizzle schema from the Prisma schema.

npm
yarn
pnpm
bun
npm i drizzle-orm@beta
npm i -D drizzle-prisma-generator

Update your Prisma schema

Add Drizzle generator to your Prisma schema. output is the path where generated Drizzle schema TS files will be placed.

schema.prisma
generator client {
  provider = "prisma-client-js"
}

generator drizzle {
  provider = "drizzle-prisma-generator"
  output   = "./drizzle" // Where to put generated Drizle tables
}

// Rest of your Prisma schema

datasource db {
  provider = "postgresql"
  url      = env("DB_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

...

Generate Drizzle schema

prisma generate

Add Drizzle extension to your Prisma client

PostgreSQL
MySQL
SQLite
import { PrismaClient } from '@prisma/client';
import { drizzle } from 'drizzle-orm/prisma/pg';

const prisma = new PrismaClient().$extends(drizzle());
import { PrismaClient } from '@prisma/client';
import { drizzle } from 'drizzle-orm/prisma/mysql';

const prisma = new PrismaClient().$extends(drizzle());
import { PrismaClient } from '@prisma/client';
import { drizzle } from 'drizzle-orm/prisma/sqlite';

const prisma = new PrismaClient().$extends(drizzle());

Run Drizzle queries via prisma.$drizzle

In order to use Drizzle query builder, you need references to Drizzle tables. You can import them from the output path that you specified in the generator config.

import { User } from './drizzle';

await prisma.$drizzle.insert().into(User).values({ email: '[email protected]', name: 'Søren' });
const users = await prisma.$drizzle.select().from(User);

Limitations

  • Relational queries are not supported due to a Prisma driver limitation. Because of it, Prisma unable to return query results in array format, which is required for relational queries to work.
  • In SQLite, .values() (e.g. await db.select().from(table).values()) is not supported, because of the same reason as above.
  • Prepared statements support is limited - .prepare() will only build the SQL query on Drizzle side, because there is no Prisma API for prepared queries.