supabase-prisma-database-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSupabase + Prisma Database Management
Supabase + Prisma 数据库管理
Overview
概述
Manage database schema, migrations, and seed data using Prisma ORM with Supabase PostgreSQL, including shadow database configuration, seed files, and automated schema checks in CI.
使用Prisma ORM与Supabase PostgreSQL管理数据库架构、迁移和种子数据,包括影子数据库配置、种子文件以及CI中的自动化架构检查。
Installation and Setup
安装与设置
1. Install Prisma
1. 安装Prisma
Install Prisma CLI and client:
bash
npm install -D prisma
npm install @prisma/client安装Prisma CLI和客户端:
bash
npm install -D prisma
npm install @prisma/client2. Initialize Prisma
2. 初始化Prisma
Initialize Prisma in your project:
bash
npx prisma initThis creates:
- - Database schema definition
prisma/schema.prisma - - Environment variables (add
.env)DATABASE_URL
在项目中初始化Prisma:
bash
npx prisma init此命令会创建:
- - 数据库架构定义文件
prisma/schema.prisma - - 环境变量文件(需添加
.env)DATABASE_URL
3. Configure Supabase Connection
3. 配置Supabase连接
Get your Supabase database URL from:
- Supabase Dashboard > Project Settings > Database > Connection String > URI
Add to :
.envenv
undefined从以下位置获取Supabase数据库URL:
- Supabase控制台 > 项目设置 > 数据库 > 连接字符串 > URI
将其添加到文件中:
.envenv
undefinedTransaction pooler for Prisma migrations
用于Prisma迁移的事务池
DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"
DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"
Session pooler for queries (with pgBouncer)
用于查询的会话池(搭配pgBouncer)
DIRECT_URL="postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:6543/postgres?pgbouncer=true"
Update `prisma/schema.prisma` to use both URLs:
```prisma
datasource db {
provider = "postgresql"
url = env("DIRECT_URL")
directUrl = env("DATABASE_URL")
}Why two URLs?
- : Direct connection for migrations (required)
DATABASE_URL - : Pooled connection for application queries (optional, better performance)
DIRECT_URL
DIRECT_URL="postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:6543/postgres?pgbouncer=true"
更新`prisma/schema.prisma`以使用这两个URL:
```prisma
datasource db {
provider = "postgresql"
url = env("DIRECT_URL")
directUrl = env("DATABASE_URL")
}为什么需要两个URL?
- :用于迁移的直接连接(必填)
DATABASE_URL - :用于应用查询的池化连接(可选,性能更优)
DIRECT_URL
4. Configure Shadow Database (Required for Migrations)
4. 配置影子数据库(迁移必填)
For migration preview and validation, configure a shadow database in :
prisma/schema.prismaprisma
datasource db {
provider = "postgresql"
url = env("DIRECT_URL")
directUrl = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}Add to :
.envenv
SHADOW_DATABASE_URL="postgresql://postgres:[PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"Note: Supabase free tier allows using the same database for shadow. For production, use a separate database.
为了迁移预览和验证,在中配置影子数据库:
prisma/schema.prismaprisma
datasource db {
provider = "postgresql"
url = env("DIRECT_URL")
directUrl = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}将其添加到文件中:
.envenv
SHADOW_DATABASE_URL="postgresql://postgres:[PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"注意:Supabase免费套餐允许使用同一个数据库作为影子数据库。生产环境建议使用独立的数据库。
Schema Definition
架构定义
1. Define Your Schema
1. 定义你的架构
Edit using the example from . This example includes:
prisma/schema.prismaassets/example-schema.prisma- User profiles with auth integration
- Timestamps with and
@default(now())@updatedAt - Relations between entities
- Indexes for performance
- Unique constraints
Key Prisma features:
- - Auto-generated UUIDs
@id @default(uuid()) - - Automatic timestamps
@default(now()) - - Auto-update on modification
@updatedAt - - Database indexes
@@index([field]) - - Define relationships
@relation
使用中的示例编辑。该示例包含:
assets/example-schema.prismaprisma/schema.prisma- 与认证集成的用户配置文件
- 带和
@default(now())的时间戳@updatedAt - 实体间的关联关系
- 用于性能优化的索引
- 唯一约束
Prisma核心特性:
- - 自动生成UUID
@id @default(uuid()) - - 自动生成时间戳
@default(now()) - - 修改时自动更新
@updatedAt - - 数据库索引
@@index([field]) - - 定义关联关系
@relation
2. Link to Supabase Auth
2. 关联Supabase认证
To integrate with Supabase Auth, reference the table:
auth.usersprisma
model Profile {
id String @id @db.Uuid
email String @unique
// Other fields...
// This doesn't create a foreign key, just documents the relationship
// The actual user exists in auth.users (managed by Supabase)
}Important: Don't create a foreign key to as it's in a different schema. Handle the relationship in application logic.
auth.users要与Supabase认证集成,引用表:
auth.usersprisma
model Profile {
id String @id @db.Uuid
email String @unique
// 其他字段...
// 这不会创建外键,仅用于记录关联关系
// 实际用户存储在auth.users表中(由Supabase管理)
}重要提示:不要创建指向的外键,因为它位于不同的架构中。请在应用逻辑中处理关联关系。
auth.usersMigrations
迁移
1. Create Migration
1. 创建迁移
After defining/modifying schema, create a migration:
bash
npx prisma migrate dev --name add_profiles_tableThis:
- Generates SQL migration in
prisma/migrations/ - Applies migration to database
- Regenerates Prisma Client
- Runs seed script (if configured)
定义/修改架构后,创建迁移:
bash
npx prisma migrate dev --name add_profiles_table此命令会:
- 在目录中生成SQL迁移文件
prisma/migrations/ - 将迁移应用到数据库
- 重新生成Prisma Client
- 运行种子脚本(如果已配置)
2. Review Migration SQL
2. 审核迁移SQL
Always review generated SQL in :
prisma/migrations/[timestamp]_[name]/migration.sqlsql
-- CreateTable
CREATE TABLE "Profile" (
"id" UUID NOT NULL,
"email" TEXT NOT NULL,
-- ...
CONSTRAINT "Profile_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Profile_email_key" ON "Profile"("email");Make manual adjustments if needed before applying to production.
务必审核中生成的SQL:
prisma/migrations/[timestamp]_[name]/migration.sqlsql
-- CreateTable
CREATE TABLE "Profile" (
"id" UUID NOT NULL,
"email" TEXT NOT NULL,
-- ...
CONSTRAINT "Profile_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Profile_email_key" ON "Profile"("email");在应用到生产环境前,如有需要可手动调整。
3. Apply Migrations in Production
3. 在生产环境中应用迁移
For production deployments:
bash
npx prisma migrate deployThis applies pending migrations without prompts or seeds.
CI/CD Integration: Add to your deployment pipeline:
yaml
undefined对于生产环境部署:
bash
npx prisma migrate deploy此命令会应用待处理的迁移,无提示且不运行种子脚本。
CI/CD集成:将以下步骤添加到部署流水线中:
yaml
undefinedExample GitHub Actions step
GitHub Actions示例步骤
- name: Run migrations run: npx prisma migrate deploy env: DATABASE_URL: ${{ secrets.DATABASE_URL }}
undefined- name: Run migrations run: npx prisma migrate deploy env: DATABASE_URL: ${{ secrets.DATABASE_URL }}
undefined4. Reset Database (Development Only)
4. 重置数据库(仅开发环境)
To reset database to clean state:
bash
npx prisma migrate resetThis:
- Drops database
- Creates database
- Applies all migrations
- Runs seed script
Warning: This deletes all data. Only use in development.
要将数据库重置为干净状态:
bash
npx prisma migrate reset此命令会:
- 删除数据库
- 创建新数据库
- 应用所有迁移
- 运行种子脚本
警告:此操作会删除所有数据。仅在开发环境中使用。
Seeding Data
数据播种
1. Create Seed Script
1. 创建种子脚本
Create using the template from . This script:
prisma/seed.tsassets/seed.ts- Uses Prisma Client to insert data
- Creates initial users, settings, or reference data
- Can be run manually or after migrations
- Supports idempotent operations (safe to run multiple times)
使用中的模板创建。该脚本:
assets/seed.tsprisma/seed.ts- 使用Prisma Client插入数据
- 创建初始用户、设置或参考数据
- 可手动运行或在迁移后自动运行
- 支持幂等操作(可安全多次运行)
2. Configure Seed in package.json
2. 在package.json中配置种子脚本
Add seed configuration to :
package.jsonjson
{
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
}Install ts-node for TypeScript execution:
bash
npm install -D ts-node将种子配置添加到:
package.jsonjson
{
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
}安装ts-node以执行TypeScript:
bash
npm install -D ts-node3. Run Seed Manually
3. 手动运行种子脚本
Execute seed script:
bash
npx prisma db seedSeed runs automatically after and .
prisma migrate devprisma migrate reset执行种子脚本:
bash
npx prisma db seed种子脚本会在和后自动运行。
prisma migrate devprisma migrate reset4. Idempotent Seeding
4. 幂等播种
Make seeds safe to run multiple times using upsert:
typescript
await prisma.user.upsert({
where: { email: 'admin@example.com' },
update: {}, // No updates if exists
create: {
email: 'admin@example.com',
name: 'Admin User',
},
});使用upsert确保种子脚本可安全多次运行:
typescript
await prisma.user.upsert({
where: { email: 'admin@example.com' },
update: {}, // 若存在则不更新
create: {
email: 'admin@example.com',
name: 'Admin User',
},
});Prisma Client Usage
Prisma Client 使用
1. Generate Client
1. 生成Client
After schema changes, regenerate Prisma Client:
bash
npx prisma generateThis updates with types matching your schema.
node_modules/@prisma/client架构变更后,重新生成Prisma Client:
bash
npx prisma generate此命令会更新,使其包含与架构匹配的类型。
node_modules/@prisma/client2. Use in Next.js Server Components
2. 在Next.js Server Components中使用
Create a Prisma client singleton using :
assets/prisma-client.tstypescript
import { prisma } from '@/lib/prisma';
export default async function UsersPage() {
const users = await prisma.profile.findMany();
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}使用创建Prisma Client单例:
assets/prisma-client.tstypescript
import { prisma } from '@/lib/prisma';
export default async function UsersPage() {
const users = await prisma.profile.findMany();
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}3. Use in Server Actions
3. 在Server Actions中使用
typescript
'use server';
import { prisma } from '@/lib/prisma';
import { revalidatePath } from 'next/cache';
export async function createProfile(formData: FormData) {
const name = formData.get('name') as string;
await prisma.profile.create({
data: {
name,
email: formData.get('email') as string,
},
});
revalidatePath('/profiles');
}typescript
'use server';
import { prisma } from '@/lib/prisma';
import { revalidatePath } from 'next/cache';
export async function createProfile(formData: FormData) {
const name = formData.get('name') as string;
await prisma.profile.create({
data: {
name,
email: formData.get('email') as string,
},
});
revalidatePath('/profiles');
}CI/CD Integration
CI/CD集成
1. Add Schema Validation to CI
1. 为CI添加架构验证
Create using the template from . This workflow:
.github/workflows/schema-check.ymlassets/github-workflows-schema-check.yml- Runs on pull requests
- Validates schema syntax
- Checks for migration drift
- Ensures migrations are generated
- Verifies Prisma Client generation
使用中的模板创建。该工作流:
assets/github-workflows-schema-check.yml.github/workflows/schema-check.yml- 在拉取请求时运行
- 验证架构语法
- 检查迁移漂移
- 确保已生成迁移文件
- 验证Prisma Client的生成
2. Migration Deployment
2. 迁移部署
Add migration step to deployment workflow:
yaml
- name: Apply database migrations
run: npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}将迁移步骤添加到部署工作流中:
yaml
- name: Apply database migrations
run: npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}3. Environment-Specific Databases
3. 环境专属数据库
Use different database URLs for each environment:
env
undefined为每个环境使用不同的数据库URL:
env
undefinedDevelopment
开发环境
DATABASE_URL="postgresql://localhost:5432/dev"
DATABASE_URL="postgresql://localhost:5432/dev"
Staging
预发布环境
DATABASE_URL="postgresql://staging-db.supabase.co:5432/postgres"
DATABASE_URL="postgresql://staging-db.supabase.co:5432/postgres"
Production
生产环境
DATABASE_URL="postgresql://prod-db.supabase.co:5432/postgres"
undefinedDATABASE_URL="postgresql://prod-db.supabase.co:5432/postgres"
undefinedBest Practices
最佳实践
Schema Design
架构设计
- Use UUIDs for IDs: Better for distributed systems
- Add Timestamps: Track and
createdAtupdatedAt - Define Indexes: Improve query performance on filtered fields
- Use Enums: Type-safe status/role fields
- Validate at DB Level: Use unique constraints and checks
- 使用UUID作为ID:更适合分布式系统
- 添加时间戳:跟踪和
createdAtupdatedAt - 定义索引:提升过滤字段的查询性能
- 使用枚举:类型安全的状态/角色字段
- 数据库层验证:使用唯一约束和检查
Migration Management
迁移管理
- Review Before Applying: Always check generated SQL
- Name Descriptively: Use clear migration names
- Keep Atomic: One logical change per migration
- Test Locally First: Verify migrations work before production
- Never Modify Applied Migrations: Create new ones instead
- 应用前审核:务必检查生成的SQL
- 命名清晰:使用明确的迁移名称
- 保持原子性:每个迁移对应一个逻辑变更
- 先本地测试:在生产环境前验证迁移效果
- 不要修改已应用的迁移:如需变更,请创建新迁移
Prisma Client
Prisma Client
- Use Singleton Pattern: Prevent connection exhaustion
- Close in Serverless: Disconnect after operations
- Type Everything: Leverage Prisma's TypeScript types
- Use Select: Only fetch needed fields
- Batch Operations: Use ,
createManyfor bulk opsupdateMany
- 使用单例模式:避免连接耗尽
- 无服务器环境中关闭连接:操作完成后断开连接
- 全类型化:利用Prisma的TypeScript类型
- 使用Select:仅获取所需字段
- 批量操作:使用、
createMany处理批量操作updateMany
Troubleshooting
故障排除
Migration fails with "relation already exists": Reset development database with . For production, manually fix conflicts.
npx prisma migrate resetPrisma Client out of sync: Run after schema changes.
npx prisma generateConnection pool exhausted: Use connection pooling via with pgBouncer.
DIRECT_URLShadow database errors: Ensure shadow database URL is correct and accessible. For Supabase free tier, same DB can be used.
Type errors after schema changes: Restart TypeScript server in IDE after .
prisma generate迁移失败并提示"relation already exists":使用重置开发数据库。生产环境需手动修复冲突。
npx prisma migrate resetPrisma Client与架构不同步:架构变更后运行。
npx prisma generate连接池耗尽:使用搭配pgBouncer进行连接池管理。
DIRECT_URL影子数据库错误:确保影子数据库URL正确且可访问。Supabase免费套餐可使用同一数据库。
架构变更后出现类型错误:运行后重启IDE中的TypeScript服务器。
prisma generateResources
资源
scripts/
scripts/
No executable scripts needed for this skill.
此技能无需可执行脚本。
references/
references/
- - Comprehensive guide to Prisma patterns, performance optimization, and common pitfalls
prisma-best-practices.md - - Specific considerations for using Prisma with Supabase, including RLS integration
supabase-integration.md
- - Prisma模式、性能优化和常见陷阱的综合指南
prisma-best-practices.md - - 使用Prisma与Supabase集成的特定注意事项,包括RLS集成
supabase-integration.md
assets/
assets/
- - Complete schema example with common patterns (auth, timestamps, relations, indexes)
example-schema.prisma - - Idempotent seed script template for initial data
seed.ts - - Singleton Prisma Client for Next.js to prevent connection exhaustion
prisma-client.ts - - CI workflow for schema validation and migration checks
github-workflows-schema-check.yml
- - 包含常见模式(认证、时间戳、关联、索引)的完整架构示例
example-schema.prisma - - 用于初始化数据的幂等种子脚本模板
seed.ts - - 用于Next.js的Prisma Client单例,防止连接耗尽
prisma-client.ts - - 用于架构验证和迁移检查的CI工作流
github-workflows-schema-check.yml