You should have the latest version of Vercel CLI installed.
npm
yarn
pnpm
bun
npm i -g vercel
yarn add -g vercel
pnpm add -g vercel
bun add -g vercel
You should have an existing Next.js project or create a new one using the following command:
npx create-next-app@latest --typescript
You should have installed Drizzle ORM and Drizzle kit. You can do this by running the following command:
npm
yarn
pnpm
bun
npm i drizzle-ormnpm i -D drizzle-kit
yarn add drizzle-ormyarn add -D drizzle-kit
pnpm add drizzle-ormpnpm add -D drizzle-kit
bun add drizzle-ormbun add -D drizzle-kit
IMPORTANT
In case you face the issue with resolving dependencies during installation:
If you’re not using React Native, forcing the installation with --force or --legacy-peer-deps should resolve the issue. If you are using React Native, then you need to use the exact version of React which is compatible with your React Native version.
Edge-compatible driver
When using Drizzle ORM with Vercel Edge functions you have to use edge-compatible drivers because the functions run in Edge runtime not in Node.js runtime, so there are some limitations of standard Node.js APIs.
You can choose one of these drivers according to your database dialect:
Neon serverless driver allows you to query your Neon Postgres databases from serverless and edge environments over HTTP or WebSockets in place of TCP. We recommend using this driver for connecting to Neon Postgres.
Vercel Postgres driver is built on top of the Neon serverless driver. We recommend using this driver for connecting to Vercel Postgres.
PlanetScale serverless driver allows you access any MySQL client and execute queries over an HTTP connection, which is generally not blocked by cloud providers.
Drizzle config - a configuration file that is 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 root of your project and add the following content:
You can generate migrations using drizzle-kit generate command and then run them using the drizzle-kit migrate command.
Generate migrations:
npx drizzle-kit generate
These migrations are stored in the drizzle directory, as specified in your drizzle.config.ts. This directory will contain the SQL files necessary to update your database schema and a meta folder for storing snapshots of the schema at different migration stages.
Example of a generated migration:
CREATE TABLE IF NOT EXISTS "users_table" ( "id" serial PRIMARY KEY NOT NULL, "name" text NOT NULL, "age" text NOT NULL, "email" text NOT NULL, CONSTRAINT "users_table_email_unique" UNIQUE("email"));
Push command is good for situations where you need to quickly test new schema designs or changes in a local development environment, allowing for fast iterations without the overhead of managing migration files.
Connect Drizzle ORM to your database
Create a index.ts file in the src/db directory and set up your database configuration:
src/db/index.ts
import { drizzle } from 'drizzle-orm/neon-serverless';export constdb = drizzle(process.env.POSTGRES_URL!)
Drizzle config - a configuration file that is 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 root of your project and add the following content:
You can generate migrations using drizzle-kit generate command and then run them using the drizzle-kit migrate command.
Generate migrations:
npx drizzle-kit generate
These migrations are stored in the drizzle directory, as specified in your drizzle.config.ts. This directory will contain the SQL files necessary to update your database schema and a meta folder for storing snapshots of the schema at different migration stages.
Example of a generated migration:
CREATE TABLE IF NOT EXISTS "users_table" ( "id" serial PRIMARY KEY NOT NULL, "name" text NOT NULL, "age" text NOT NULL, "email" text NOT NULL, CONSTRAINT "users_table_email_unique" UNIQUE("email"));
Push command is good for situations where you need to quickly test new schema designs or changes in a local development environment, allowing for fast iterations without the overhead of managing migration files.
Connect Drizzle ORM to your database
Create a index.ts file in the src/db directory and set up your database configuration:
src/db/index.ts
import { drizzle } from 'drizzle-orm/vercel-postgres';export constdb = drizzle()
Drizzle config - a configuration file that is 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 root of your project and add the following content:
You can generate migrations using drizzle-kit generate command and then run them using the drizzle-kit migrate command.
Generate migrations:
npx drizzle-kit generate
These migrations are stored in the drizzle directory, as specified in your drizzle.config.ts. This directory will contain the SQL files necessary to update your database schema and a meta folder for storing snapshots of the schema at different migration stages.
Example of a generated migration:
CREATE TABLE `users_table` ( `id` serial AUTO_INCREMENT NOT NULL, `name` text NOT NULL, `age` text NOT NULL, `email` text NOT NULL, CONSTRAINT `users_table_id` PRIMARY KEY(`id`), CONSTRAINT `users_table_email_unique` UNIQUE(`email`));
Push command is good for situations where you need to quickly test new schema designs or changes in a local development environment, allowing for fast iterations without the overhead of managing migration files.
Connect Drizzle ORM to your database
Create a index.ts file in the src/db directory and set up your database configuration:
src/db/index.ts
import { drizzle } from "drizzle-orm/planetscale-serverless";export constdb = drizzle(process.env.MYSQL_URL!)
Drizzle config - a configuration file that is 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 root of your project and add the following content:
You can generate migrations using drizzle-kit generate command and then run them using the drizzle-kit migrate command.
Generate migrations:
npx drizzle-kit generate
These migrations are stored in the drizzle directory, as specified in your drizzle.config.ts. This directory will contain the SQL files necessary to update your database schema and a meta folder for storing snapshots of the schema at different migration stages.
Example of a generated migration:
CREATE TABLE `users_table` ( `id` integer PRIMARY KEY NOT NULL, `name` text NOT NULL, `age` text NOT NULL, `email` text NOT NULL);--> statement-breakpointCREATE UNIQUE INDEX `users_table_email_unique` ON `users_table` (`email`);
Push command is good for situations where you need to quickly test new schema designs or changes in a local development environment, allowing for fast iterations without the overhead of managing migration files.
Connect Drizzle ORM to your database
Create a index.ts file in the src/db directory and set up your database configuration: