Drizzle with Vercel Edge Functions
This tutorial demonstrates how to use Drizzle ORM with Vercel Functions in Edge runtime.
- You should have the latest version of Vercel CLI installed.
- You should have an existing Next.js project or create a new one using the following command:
- You should have installed Drizzle ORM and Drizzle kit. You can do this by running the following command:
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 toVercel 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. - libSQL client allows you to access Turso database.
Navigation
- Navigate directly to the PostgreSQL client section.
- Navigate directly to the Vercel Postgres client section.
- Navigate directly to the MySQL client section.
- Navigate directly to the Turso section.
Any PostgreSQL client
Setup Drizzle config file
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:
Configure your database connection string in the .env
file:
Create a table
Create a schema.ts
file in the app/db
directory and declare a table schema:
Push your changes to the database
Run the drizzle-kit push
command to push your changes to the database:
Learn more about push command here. Alternatively, apply your changes through the migration process.
Connect Drizzle ORM to your database
Install the @neondatabase/serverless
driver:
Create a db.ts
file in the app/db
directory and set up your database configuration:
Create an API route
Create /api/hello/route.ts
in app
directory. To learn more about how to write a function, see the Functions API Reference and Vercel Functions Quickstart.
Test your code locally
Run the next dev
command to start your local development server:
Navigate to the route you created (e.g. /api/hello)
in your browser:
Deploy your project
Create a new project in the dashboard or run the vercel
command to deploy your project:
Add POSTGRES_URL
environment variable:
Redeploy your project to update your environment variables:
Finally, you can use URL of the deployed project and navigate to the route you created (e.g. /api/hello)
to access your edge function.
Vercel Postgres client
You can check quickstart guide for Drizzle with Vercel Postgres client in the documentation.
Setup Drizzle config file
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:
Configure your database connection string in the .env
file:
Create a table
Create a schema.ts
file in the app/db
directory and declare a table schema:
Push your changes to the database
Run the drizzle-kit push
command to push your changes to the database:
Learn more about push command here. Alternatively, apply your changes through the migration process.
Connect Drizzle ORM to your database
Install the @vercel/postgres
driver:
Create a db.ts
file in the app/db
directory and set up your database configuration:
Create an API route
Create /api/hello/route.ts
in app
directory. To learn more about how to write a function, see the Functions API Reference and Vercel Functions Quickstart.
Test your code locally
Run the next dev
command to start your local development server:
Navigate to the route you created (e.g. /api/hello)
in your browser:
Deploy your project
Create a new project in the dashboard or run the vercel
command to deploy your project:
Add POSTGRES_URL
environment variable:
Redeploy your project to update your environment variables:
Finally, you can use URL of the deployed project and navigate to the route you created (e.g. /api/hello)
to access your edge function.
Any MySQL client
In this tutorial we use PlanetScale MySQL.
Setup Drizzle config file
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:
Configure your database connection string in the .env
file:
Create a table
Create a schema.ts
file in the app/db
directory and declare a table schema:
Push your changes to the database
Run the drizzle-kit push
command to push your changes to the database:
Learn more about push command here. Alternatively, apply your changes through the migration process.
Connect Drizzle ORM to your database
Install the @planetscale/database
driver:
Create a db.ts
file in the app/db
directory and set up your database configuration:
Create an API route
Create /api/hello/route.ts
in app
directory. To learn more about how to write a function, see the Functions API Reference and Vercel Functions Quickstart.
Test your code locally
Run the next dev
command to start your local development server:
Navigate to the route you created (e.g. /api/hello)
in your browser:
Deploy your project
Create a new project in the dashboard or run the vercel
command to deploy your project:
Add MYSQL_URL
environment variable:
Redeploy your project to update your environment variables:
Finally, you can use URL of the deployed project and navigate to the route you created (e.g. /api/hello)
to access your edge function.
Turso
You can check quickstart guide or tutorial for Drizzle with Turso in the documentation.
Setup Drizzle config file
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:
Configure your database connection string in the .env
file:
Create a table
Create a schema.ts
file in the app/db
directory and declare a table schema:
Push your changes to the database
Install the @libsql/client
driver:
Run the drizzle-kit push
command to push your changes to the database:
Learn more about push command here. Alternatively, apply your changes through the migration process.
Connect Drizzle ORM to your database
Create a db.ts
file in the app/db
directory and set up your database configuration:
Create an API route
Create /api/hello/route.ts
in app
directory. To learn more about how to write a function, see the Functions API Reference and Vercel Functions Quickstart.
Test your code locally
Run the next dev
command to start your local development server:
Navigate to the route you created (e.g. /api/hello)
in your browser:
Deploy your project
Create a new project in the dashboard or run the vercel
command to deploy your project:
Add TURSO_CONNECTION_URL
and TURSO_AUTH_TOKEN
environment variables:
Redeploy your project to update your environment variables:
Finally, you can use URL of the deployed project and navigate to the route you created (e.g. /api/hello)
to access your edge function.