You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial demonstrates how to use Drizzle ORM with [Supabase Edge Functions](https://supabase.com/docs/guides/functions).
13
13
14
14
<Prerequisites>
15
15
- You should have the latest version of [Supabase CLI](https://supabase.com/docs/guides/cli/getting-started#installing-the-supabase-cli) installed.
16
-
- You should have installed Drizzle ORM and [Drizzle kit](/docs/kit-overview). You can do this by running the following command:
16
+
- You should have installed Drizzle ORM and [Drizzle kit](https://orm.drizzle.team/kit-docs/overview). You can do this by running the following command:
17
17
<Npm>
18
18
drizzle-orm
19
19
-D drizzle-kit
20
20
</Npm>
21
21
- You should have installed Docker Desktop. It is a prerequisite for local development. Follow the official [docs](https://docs.docker.com/desktop) to install.
22
22
</Prerequisites>
23
23
24
-
25
24
To learn how to create a basic Edge Function on your local machine and then deploy it, see the [Edge Functions Quickstart](https://supabase.com/docs/guides/functions/quickstart).
26
25
27
26
<Steps>
27
+
#### Create a table
28
+
29
+
Create a `schema.ts` file in your `src` directory and declare a table schema:
This file will be used to generate migrations for your database.
42
+
43
+
#### Setup Drizzle config file
44
+
45
+
**Drizzle config** - a configuration file that is used by [Drizzle Kit](https://orm.drizzle.team/kit-docs/overview) and contains all the information about your database connection, migration folder and schema files.
46
+
47
+
Create a `drizzle.config.ts` file in the root of your project and add the following content:
48
+
49
+
```typescript copy filename="drizzle.config.ts"
50
+
import { defineConfig } from"drizzle-kit";
51
+
52
+
exportdefaultdefineConfig({
53
+
schema: "./src/schema.ts",
54
+
out: "./supabase/migrations",
55
+
dialect: "postgresql",
56
+
});
57
+
```
58
+
59
+
In this tutorial we will use Drizzle kit to generate migrations for our schema.
60
+
28
61
#### Initialize a new Supabase project
29
62
30
63
Create a new Supabase project in a folder on your local machine:
@@ -42,6 +75,36 @@ It will create `supabase` folder with `config.toml` file:
42
75
43
76
If you are using Visual Studio Code, follow the [Supabase documentation](https://supabase.com/docs/guides/functions/local-development#deno-with-visual-studio-code) to setup settings for Deno.
44
77
78
+
#### Generate migrations
79
+
80
+
Run the `drizzle-kit generate` command to generate migrations:
81
+
82
+
```bash copy
83
+
npx drizzle-kit generate
84
+
```
85
+
86
+
It will create a new migration file in the `supabase/migrations` directory:
87
+
88
+
#### Apply migrations
89
+
90
+
To start the Supabase local development stack, run the following command:
91
+
92
+
```bash copy
93
+
supabase start
94
+
```
95
+
96
+
To apply migrations, run the following command:
97
+
98
+
```bash copy
99
+
supabase migration up
100
+
```
101
+
102
+
You can read more about Supabase migrations in the [documentation](https://supabase.com/docs/guides/deployment/database-migrations).
103
+
104
+
<Callouttype="warning">Don't forget to run Docker</Callout>
105
+
106
+
Alternatively, you can apply migrations using the `drizzle-kit migrate` command. Learn more about this migration process in the [documentation](https://orm.drizzle.team/docs/migrations).
107
+
45
108
#### Create a new Edge Function
46
109
47
110
Run the `supabase functions new [FUNCTION_NAME]` command to create a new Edge Function:
@@ -56,115 +119,101 @@ It will create a new folder with the function name in the `supabase/functions` d
When you create a new Edge Function, it will use TypeScript by default. However, it is possible write Edge Function in JavaScript. Learn more about it in the [documentation](https://supabase.com/docs/guides/functions/quickstart#not-using-typescript).
128
+
62
129
#### Setup imports
63
130
64
-
Create a `import_map.json` file in the `supabase/functions` directory and add import for Drizzle ORM:
131
+
Add the following imports to the `deno.json` file in the `supabase/functions/drizzle-tutorial` directory:
**Drizzle config** - a configuration file that is used by [Drizzle Kit](/docs/kit-overview) and contains all the information about your database connection, migration folder and schema files.
95
-
96
-
Create a `drizzle.config.ts` file in the root of your project and add the following content:
97
-
98
-
```typescript copy filename="drizzle.config.ts"
99
-
import { defineConfig } from"drizzle-kit";
100
-
101
-
exportdefaultdefineConfig({
102
-
schema: "./supabase/functions/common/schema.ts",
103
-
out: "./supabase/migrations",
104
-
dialect: "postgresql",
105
-
});
106
-
```
107
-
108
-
In this tutorial we will use Drizzle kit to generate migrations for our schema.
109
-
110
-
#### Generate migrations
111
-
112
-
Run the `drizzle-kit generate` command to generate migrations:
113
158
114
-
```bash copy
115
-
npx drizzle-kit generate
116
-
```
117
-
118
-
It will create a new migration file in the `supabase/migrations` directory:
119
-
120
-
#### Apply migrations
121
-
122
-
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](/docs/migrations).
172
+
<Callouttype="warning">
173
+
In the Deno ecosystem, each function should be treated as an independent project with its own set of dependencies and configurations.
174
+
For these reasons, Supabase recommend maintaining separate configuration files (`deno.json`, `.npmrc`, or `import_map.json`) within each function's directory, even if it means duplicating some configurations. Read more [here](https://supabase.com/docs/guides/functions/dependencies#managing-dependencies).
175
+
</Callout>
129
176
130
177
#### Connect Drizzle ORM to your database
131
178
132
-
Create a `db.ts` file in the `supabase/common` directory and set up your database configuration:
179
+
Update your edge function code with 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](https://supabase.com/docs/guides/functions/secrets).
`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](https://supabase.com/docs/guides/functions/secrets).
214
+
215
+
#### Test your code locally
216
+
168
217
Run the following command to test your function locally:
169
218
170
219
```bash copy
@@ -178,8 +227,7 @@ Navigate to the route `(e.g. /drizzle-tutorial)` in your browser:
178
227
{
179
228
"id": 1,
180
229
"name": "Alice",
181
-
"age": 25,
182
-
"email": "email"
230
+
"age": 25
183
231
}
184
232
]
185
233
```
@@ -188,10 +236,10 @@ Navigate to the route `(e.g. /drizzle-tutorial)` in your browser:
188
236
189
237
You can create new Supabase project in the [dashboard](https://supabase.com/dashboard) or by following this [link](https://database.new/).
190
238
191
-
Copy the `Reference ID` and use it to link your local development project to a hosted Supabase project by running the following command:
239
+
Copy the `Reference ID`from project settings and use it to link your local development project to a hosted Supabase project by running the following command:
192
240
193
241
```bash copy
194
-
supabase link --project-ref<REFERENCE_ID>
242
+
supabase link --project-ref=<REFERENCE_ID>
195
243
```
196
244
197
245
Push your schema changes to the hosted Supabase project by running the following command:
@@ -202,26 +250,27 @@ supabase db push
202
250
203
251
#### Setup environment variables
204
252
205
-
Navigate to [Database Settings](https://supabase.com/dashboard/project/_/settings/database) 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.
253
+
You can find `Project connect details` by clicking **Connect** in the top bar of the dashboard and copy the URI from the `Transaction pooler` section. Remember to replace the password placeholder with your actual database password.
206
254
207
255
Read more about Connection Pooler in the [documentation](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooler).
208
256
209
-
Run the following command to set the environment variable:
210
-
211
-
```bash copy
212
-
supabase secrets set DATABASE_URL=<CONNECTION_STRING>
213
-
```
214
-
215
-
Update the `db.ts` file in the `supabase/functions/common` directory to use the `DATABASE_URL` environment variable:
257
+
Update your edge function code to use the `DATABASE_URL` environment variable instead of `SUPABASE_DB_URL`:
0 commit comments