Skip to content
Database

Prisma

本指南展示了如何将你的 Prisma 应用连接到 Supabase Postgres。如果遇到任何问题,请参考 Prisma 故障排除文档。

🌐 This guide shows how to connect your Prisma application to Supabase Postgres. If you encounter any problems, reference the Prisma troubleshooting docs.

1
Create a custom user for Prisma
  • 在 SQL 编辑器 中,创建一个在 public 模式下拥有全部权限的 Prisma 数据库用户。
  • 这可以让你更好地控制 Prisma 的访问权限,并且使用 Supabase 工具(如 查询性能仪表板 和 日志)进行监控也更方便。
-- Create custom user
create user "prisma" with password 'custom_password' bypassrls createdb;
-- extend prisma's privileges to postgres (necessary to view changes in Dashboard)
grant "prisma" to "postgres";
-- Grant it necessary permissions over the relevant schemas (public)
grant usage on schema public to prisma;
grant create on schema public to prisma;
grant all on all tables in schema public to prisma;
grant all on all routines in schema public to prisma;
grant all on all sequences in schema public to prisma;
alter default privileges for role postgres in schema public grant all on tables to prisma;
alter default privileges for role postgres in schema public grant all on routines to prisma;
alter default privileges for role postgres in schema public grant all on sequences to prisma;
-- alter prisma password if needed
alter user "prisma" with password 'new_password';
2
Create a Prisma Project

在你的电脑上创建一个新的 Prisma 项目

创建一个新目录

mkdir hello-prisma
cd hello-prisma

启动一个新的 Prisma 项目

npm init -y
npm install prisma tsx @types/pg --save-dev
npm install @prisma/client @prisma/adapter-pg dotenv pg
npx tsc --init
npx prisma init
3
Add your connection information to your .env file
  • 在你的项目仪表板上,点击 连接
  • 找到你的 Supavisor Session pooler 字符串。它应该以 5432 结尾。它将用于你的 .env 文件。
  • 如果你打算把 Prisma 部署到无服务器或自动扩展的环境,你还需要你的 Supavisor 事务模式字符串。
  • 这个字符串和会话模式字符串一样,但最后使用的是端口 6543。

在你的 .env 文件中,将 DATABASE_URL 变量设置为你的连接字符串

# Used for Prisma Migrations and within your application
DATABASE_URL="postgres://[DB-USER].[PROJECT-REF]:[PRISMA-PASSWORD]@[DB-REGION].pooler.supabase.com:5432/postgres"

把你的字符串中的 [DB-USER] 改成 prisma,然后添加你在第一步创建的密码

postgres://prisma.[PROJECT-REF]...
4
Configure prisma.config.ts

将 import "dotenv/config" 添加到生成的 prisma.config.ts。如果你使用无服务器环境,请将数据源 URL 改为 DIRECT_URL。

import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});
5
Migrate and generate your Prisma client

如果你已经修改了你的 Supabase 数据库,就把它和迁移文件同步。否则,就为你的数据库创建新表,然后生成 Prisma 客户端。

在你的 prisma.schema 文件中创建新表

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}

commit your migration

npx prisma migrate dev --name first_prisma_migration
npx prisma generate
6
Test your API

创建一个 index.ts 文件并运行它以测试你的连接

import "dotenv/config";
import { PrismaClient } from "./generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
export const prisma = new PrismaClient({ adapter });
async function main() {
const val = await prisma.user.findMany({
take: 10,
});
console.log(val);
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});