Drizzle with Supabase Edge Functions
This tutorial demonstrates how to use Drizzle ORM with Supabase Edge Functions.
- You should have the latest version of Supabase CLI installed.
- You should have installed Drizzle ORM and Drizzle kit. You can do this by running the following command:
- You should have installed Docker Desktop. It is a prerequisite for local development. Follow the official docs to install.
To learn how to create a basic Edge Function on your local machine and then deploy it, see the Edge Functions Quickstart.
Initialize a new Supabase project
Create a new Supabase project in a folder on your local machine:
It will create supabase
folder with config.toml
file:
If you are using Visual Studio Code, follow the Supabase documentation to setup settings for Deno.
Create a new Edge Function
Run the supabase functions new [FUNCTION_NAME]
command to create a new Edge Function:
It will create a new folder with the function name in the supabase/functions
directory:
Setup imports
Create a import_map.json
file in the supabase/functions
directory and add import for Drizzle ORM:
Learn more in the documentation.
Create a table
Create a schema.ts
file in the supabase/functions/common
directory and declare a table schema:
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:
In this tutorial we will use Drizzle kit to generate migrations for our schema.
Generate migrations
Run the drizzle-kit generate
command to generate migrations:
It will create a new migration file in the supabase/migrations
directory:
Apply migrations
To apply migrations and start the Supabase local development stack, run the following command:
Alternatively, you can apply migrations using the migrate()
helper, available for every supported driver. Learn more about this migration process in the documentation.
Connect Drizzle ORM to your database
Create a db.ts
file in the supabase/common
directory and set up your database configuration:
SUPABASE_DB_URL
is default environment variable for the direct database connection. Learn more about managing environment variables in Supabase Edge Functions in the documentation.
Test your code locally
Run the following command to test your function locally:
Navigate to the route (e.g. /drizzle-tutorial)
in your browser:
Link your local project to a hosted Supabase project
You can create new Supabase project in the dashboard or by following this link.
Copy the Reference ID
and use it to link your local development project to a hosted Supabase project by running the following command:
Push your schema changes to the hosted Supabase project by running the following command:
Setup environment variables
Navigate to Database Settings and copy the URI from the Connection String
section. Make sure to use connection pooling
and Transaction
mode to connect to your database. Remember to replace the password placeholder with your actual database password.
Read more about Connection Pooler in the documentation.
Run the following command to set the environment variable:
Update the db.ts
file in the supabase/functions/common
directory to use the DATABASE_URL
environment variable:
Learn more about managing environment variables in Supabase Edge Functions in the documentation.
Deploy your function
Deploy your function by running the following command:
Finally, you can use URL of the deployed project and navigate to the route you created (e.g. /drizzle-tutorial)
to access your edge function.