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) +})