Introduction

OverviewPhilosophyStructureUpdatesFAQ

Usage

Other

Switch to Drizzle

How to change the ORM to Drizzle.

Drizzle is a brilliant, type-safe ORM growing quickly in popularity. If you want to switch to Drizzle, you have two options:

  1. Keep Prisma and add the Drizzle API to the Prisma client. Drizzle have a great guide on how to do this.
  2. Go all-in and switch to Drizzle.

Here, we'll assume you have a working Neon database and cover the second option.

Before starting, make sure your Prisma schema has been pushed to your database (e.g. by running npx prisma db push in packages/database). The drizzle-kit pull command in Step 4 introspects the live database — if no tables exist yet, it will generate an empty schema file.

1. Swap out the required dependencies in @repo/database

Uninstall the existing dependencies...

npm uninstall @prisma/adapter-neon @prisma/client prisma --filter @repo/database

...and install the new ones:

npm install drizzle-orm @neondatabase/serverless --filter @repo/database
npm install -D drizzle-kit --filter @repo/database

2. Update the database connection code

Delete everything in @repo/database/index.ts and replace it with the following:

packages/database/index.ts
import 'server-only';

import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';
import { env } from '@repo/env';

const client = neon(env.DATABASE_URL);

export const database = drizzle({ client });

3. Create a drizzle.config.ts file

Next we'll create a Drizzle configuration file, used by Drizzle Kit and contains all the information about your database connection, migration folder and schema files. Create a drizzle.config.ts file in the packages/database directory with the following contents:

packages/database/drizzle.config.ts
import { defineConfig } from 'drizzle-kit';
import { env } from '@repo/env';

export default defineConfig({
  schema: './schema.ts',
  out: './',
  dialect: 'postgresql',
  dbCredentials: {
    url: env.DATABASE_URL,
  },
});

4. Generate the schema file

Drizzle uses a schema file to define your database tables. Rather than create one from scratch, you can generate it from the existing database. In the packages/database folder, run the following command to generate the schema file:

npx drizzle-kit pull

This should pull the schema from the database, creating a schema.ts file containing the table definitions and some other files.

If the generated schema.ts is empty, your database likely has no tables yet. You can either push your Prisma schema first (npx prisma db push) and re-run the pull, or create the schema manually. For example, the default next-forge Page model translates to:

packages/database/schema.ts
import { pgTable, serial, text } from 'drizzle-orm/pg-core';

export const page = pgTable('Page', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
});

5. Update your queries

Now you can update your queries to use the Drizzle ORM.

For example, here's how we can update the page query in app/(authenticated)/page.tsx:

apps/app/app/(authenticated)/page.tsx {2, 7}
import { database } from '@repo/database';
import { page } from '@repo/database/schema';

// ...

const App = async () => {
  const pages = await database.select().from(page);

  // ...
};

export default App;

6. Remove Prisma Studio

You can also delete the now unused Prisma Studio app located at apps/studio:

Terminal
rm -fr apps/studio

7. Update the migration script in the root package.json

Change the migration script in the root package.json from Prisma to Drizzle. Update the migrate script to use Drizzle commands:

"scripts": {
  "db:migrate": "cd packages/database && npx drizzle-kit migrate"
  "db:generate": "cd packages/database && npx drizzle-kit generate"
  "db:pull": "cd packages/database && npx drizzle-kit pull"
}