From f3789a39c4b41ef52502dc630954e690f3b1443e Mon Sep 17 00:00:00 2001 From: Rodrigo Mansueli Date: Thu, 21 May 2026 09:25:49 -0300 Subject: [PATCH] docs: move drizzle example to supabase (#45935) ## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES/NO ## What kind of change does this PR introduce? Bug fix, feature, docs update, ... ## What is the current behavior? Please link any relevant issues here. ## What is the new behavior? Feel free to include screenshots if it includes visual changes. ## Additional context Add any other context or screenshots. ## Summary by CodeRabbit * **Documentation** * Updated the Drizzle ORM guide to a Deno-native configuration, replacing the prior import-map example with embedded Deno examples and references to Edge Function samples. * **New Features** * Added an Edge Function example demonstrating Drizzle + PostgreSQL on Deno. * Added a shared database schema example including user and countries table definitions. [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45935?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) --------- Co-authored-by: Chris Chinchilla --- .../guides/functions/connect-to-postgres.mdx | 47 ++++++++----------- .../supabase/functions/_shared/schema.ts | 16 +++++++ .../supabase/functions/drizzle/deno.json | 7 +++ .../supabase/functions/drizzle/index.ts | 14 ++++++ 4 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 examples/edge-functions/supabase/functions/_shared/schema.ts create mode 100644 examples/edge-functions/supabase/functions/drizzle/deno.json create mode 100644 examples/edge-functions/supabase/functions/drizzle/index.ts diff --git a/apps/docs/content/guides/functions/connect-to-postgres.mdx b/apps/docs/content/guides/functions/connect-to-postgres.mdx index a96d6235fb3..15589f2bf81 100644 --- a/apps/docs/content/guides/functions/connect-to-postgres.mdx +++ b/apps/docs/content/guides/functions/connect-to-postgres.mdx @@ -68,40 +68,31 @@ language="typescript" ## Using Drizzle -You can use Drizzle together with [Postgres.js](https://github.com/porsager/postgres). Both can be loaded directly from npm: +You can use Drizzle together with [Postgres.js](https://github.com/porsager/postgres). Both can be loaded directly from npm. -**Set up dependencies in `import_map.json`**: +Declare the dependencies in a `deno.json` file inside the function directory (see [Managing functions dependencies](/docs/guides/functions/dependencies) for more details): -```json supabase/functions/import_map.json -{ - "imports": { - "drizzle-orm": "npm:drizzle-orm@0.29.1", - "drizzle-orm/": "npm:/drizzle-orm@0.29.1/", - "postgres": "npm:postgres@3.4.3" - } -} -``` +<$CodeSample +path="/edge-functions/supabase/functions/drizzle/deno.json" +title="supabase/functions/drizzle/deno.json" +language="json" +/> -**Use in your function**: +Then define your schema and query the database: -```ts supabase/functions/drizzle/index.ts -import { drizzle } from 'drizzle-orm/postgres-js' -import postgres from 'postgres' -import { countries } from '../_shared/schema.ts' +<$CodeSample +path="/edge-functions/supabase/functions/\_shared/schema.ts" +title="supabase/functions/\_shared/schema.ts" +language="typescript" +/> -const connectionString = Deno.env.get('SUPABASE_DB_URL')! +<$CodeSample +path="/edge-functions/supabase/functions/drizzle/index.ts" +title="supabase/functions/drizzle/index.ts" +language="typescript" +/> -Deno.serve(async (_req) => { - // Disable prefetch as it is not supported for "Transaction" pool mode - const client = postgres(connectionString, { prepare: false }) - const db = drizzle(client) - const allCountries = await db.select().from(countries) - - return Response.json(allCountries) -}) -``` - -You can find the full example on [GitHub](https://github.com/thorwebdev/edgy-drizzle). +You can find the full example on [GitHub](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/drizzle). --- diff --git a/examples/edge-functions/supabase/functions/_shared/schema.ts b/examples/edge-functions/supabase/functions/_shared/schema.ts new file mode 100644 index 00000000000..45170cdbbeb --- /dev/null +++ b/examples/edge-functions/supabase/functions/_shared/schema.ts @@ -0,0 +1,16 @@ +import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core' + +export const user = pgTable('user', { + id: serial('id'), + name: text('name'), + email: text('email'), + password: text('password'), + role: text('role').$type<'admin' | 'customer'>(), + createdAt: timestamp('created_at'), + updatedAt: timestamp('updated_at'), +}) + +export const countries = pgTable('countries', { + id: serial('id'), + name: text('name'), +}) diff --git a/examples/edge-functions/supabase/functions/drizzle/deno.json b/examples/edge-functions/supabase/functions/drizzle/deno.json new file mode 100644 index 00000000000..3657718dcee --- /dev/null +++ b/examples/edge-functions/supabase/functions/drizzle/deno.json @@ -0,0 +1,7 @@ +{ + "imports": { + "drizzle-orm": "npm:drizzle-orm@0.29.1", + "drizzle-orm/": "npm:/drizzle-orm@0.29.1/", + "postgres": "npm:postgres@3.4.3" + } +} diff --git a/examples/edge-functions/supabase/functions/drizzle/index.ts b/examples/edge-functions/supabase/functions/drizzle/index.ts new file mode 100644 index 00000000000..fede266f225 --- /dev/null +++ b/examples/edge-functions/supabase/functions/drizzle/index.ts @@ -0,0 +1,14 @@ +import { drizzle } from 'drizzle-orm/postgres-js' +import postgres from 'postgres' +import { countries } from '../_shared/schema.ts' + +const connectionString = Deno.env.get('SUPABASE_DB_URL')! +// Disable prefetch as it is not supported for "Transaction" pool mode +const client = postgres(connectionString, { prepare: false }) +const db = drizzle(client) + +Deno.serve(async (_req) => { + const allCountries = await db.select().from(countries) + + return Response.json(allCountries) +})