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 工具进行监控,比如 查询性能仪表板日志浏览器
1
-- Create custom user
2
create user "prisma" with password 'custom_password' bypassrls createdb;
3
4
-- extend prisma's privileges to postgres (necessary to view changes in Dashboard)
5
grant "prisma" to "postgres";
6
7
-- Grant it necessary permissions over the relevant schemas (public)
8
grant usage on schema public to prisma;
9
grant create on schema public to prisma;
10
grant all on all tables in schema public to prisma;
11
grant all on all routines in schema public to prisma;
12
grant all on all sequences in schema public to prisma;
13
alter default privileges for role postgres in schema public grant all on tables to prisma;
14
alter default privileges for role postgres in schema public grant all on routines to prisma;
15
alter default privileges for role postgres in schema public grant all on sequences to prisma;
1
-- alter prisma password if needed
2
alter user "prisma" with password 'new_password';
2
Create a Prisma Project

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

创建一个新目录

1
mkdir hello-prisma
2
cd hello-prisma

启动一个新的 Prisma 项目

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

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

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

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

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

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

1
import "dotenv/config";
2
import { defineConfig, env } from "prisma/config";
3
4
export default defineConfig({
5
schema: "prisma/schema",
6
migrations: {
7
path: "prisma/migrations",
8
},
9
datasource: {
10
url: env("DATABASE_URL"),
11
},
12
});
5
Migrate and generate your Prisma client

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

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

1
model Post {
2
id Int @id @default(autoincrement())
3
title String
4
content String?
5
published Boolean @default(false)
6
author User? @relation(fields: [authorId], references: [id])
7
authorId Int?
8
}
9
10
model User {
11
id Int @id @default(autoincrement())
12
email String @unique
13
name String?
14
posts Post[]
15
}

commit your migration

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

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

1
import "dotenv/config";
2
import { PrismaClient } from "./generated/prisma/client";
3
import { PrismaPg } from "@prisma/adapter-pg";
4
5
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
6
export const prisma = new PrismaClient({ adapter });
7
8
async function main() {
9
const val = await prisma.user.findMany({
10
take: 10,
11
});
12
console.log(val);
13
}
14
15
main()
16
.then(async () => {
17
await prisma.$disconnect();
18
})
19
.catch(async (e) => {
20
console.error(e);
21
await prisma.$disconnect();
22
process.exit(1);
23
});