mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem There aren't much guides on how to use Supabase as just Postgres and use it with Drizzle. ## Solution Adds a simple tutorial article in the KB for how to create an app with Drizzle & TanStack, and use Supabase as just Postgres. ## Preview links https://kb-git-tylershukert-devrel-615-drizzle-supabase-1159ba-supabase.vercel.app/kb/guides/setup-drizzle-with-postgres-and-tanstack-start <!-- ## Additional context Optionally add any other context or screenshots. --> ## Review instructions Provide a clear numbered procedure that the PR reviewer can walk through. Make sure the tone and the content in general follow what we would want within KB. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --------- Co-authored-by: kodster28 <kody.jackson@supabase.io>
107 lines
2.8 KiB
Plaintext
107 lines
2.8 KiB
Plaintext
---
|
|
id: 'drizzle'
|
|
title: 'Drizzle'
|
|
description: 'Drizzle Quickstart'
|
|
breadcrumb: 'ORM Quickstarts'
|
|
hideToc: true
|
|
---
|
|
|
|
## Connecting with Drizzle
|
|
|
|
[Drizzle ORM](https://github.com/drizzle-team/drizzle-orm) is a TypeScript ORM for SQL databases designed with maximum type safety in mind. You can use their ORM to connect to your database.
|
|
|
|
<Admonition type="note">
|
|
|
|
If you plan on solely using Drizzle instead of the Supabase Data API (PostgREST), you can turn off the latter in the [API Settings](/dashboard/project/_/settings/api).
|
|
|
|
</Admonition>
|
|
<StepHikeCompact>
|
|
|
|
<StepHikeCompact.Step step={1}>
|
|
|
|
<StepHikeCompact.Details title="Install">
|
|
|
|
Install Drizzle and related dependencies.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```shell
|
|
npm i drizzle-orm postgres
|
|
npm i -D drizzle-kit
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={2}>
|
|
|
|
<StepHikeCompact.Details title="Create your models">
|
|
|
|
Create a `schema.ts` file and define your models.
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```ts schema.ts
|
|
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
|
|
|
|
export const users = pgTable('users', {
|
|
id: serial('id').primaryKey(),
|
|
fullName: text('full_name'),
|
|
phone: varchar('phone', { length: 256 }),
|
|
});
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
<StepHikeCompact.Step step={3}>
|
|
|
|
<StepHikeCompact.Details title="Connect">
|
|
|
|
Connect to your database using the Connection Pooler.
|
|
|
|
From the project [**Connect** panel](/dashboard/project/_?showConnect=true), copy the URI from the "Shared pooler" option and save it as the `DATABASE_URL` environment variable. Remember to replace the password placeholder with your actual database password.
|
|
|
|
In local SUPABASE_DB_URL require to be adapted to work with Docker resolver
|
|
|
|
</StepHikeCompact.Details>
|
|
|
|
<StepHikeCompact.Code>
|
|
|
|
```ts db.ts
|
|
import 'dotenv/config'
|
|
|
|
import { drizzle } from 'drizzle-orm/postgres-js'
|
|
import postgres from 'postgres'
|
|
|
|
const databaseUrl = process.env.DATABASE_URL
|
|
if (!databaseUrl) throw new Error('DATABASE_URL is not set')
|
|
|
|
let connectionString = databaseUrl
|
|
if (connectionString.includes('postgres:postgres@supabase_db_')) {
|
|
const url = new URL(connectionString)
|
|
url.hostname = url.hostname.split('_')[1]
|
|
connectionString = url.href
|
|
}
|
|
|
|
// Disable prefetch as it is not supported for "Transaction" pool mode
|
|
export const client = postgres(connectionString, { prepare: false })
|
|
export const db = drizzle(client)
|
|
```
|
|
|
|
</StepHikeCompact.Code>
|
|
|
|
</StepHikeCompact.Step>
|
|
|
|
</StepHikeCompact>
|
|
|
|
## Related Resources
|
|
|
|
- [Build a TanStack and Drizzle app with Postgres Database](/kb/guides/setup-drizzle-with-postgres-and-tanstack-start)
|