本页还没有译文
本页 12 段还没译成日本語,下面显示的是英文原文。Drizzle
还没翻译译自英文(supabase.com)本页 12 段都还没译
Connecting with Drizzle
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.
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.
-
Install
Install Drizzle and related dependencies.
npm i drizzle-orm postgres npm i -D drizzle-kit -
Create your models
Create a
schema.tsfile and define your models.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 }), }); -
Connect
Connect to your database using the Connection Pooler.
From the project Connect panel, copy the URI from the "Shared Pooler" option and save it as the
DATABASE_URLenvironment 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
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)