运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

在 Bun 中使用 Prisma Postgres

  1. 创建新项目

    首先,创建一个目录并用 bun init 初始化它。

    terminal
    mkdir prisma-postgres-app
    cd prisma-postgres-app
    bun init
    
  2. 安装 Prisma 依赖

    然后将 Prisma CLI(prisma)、Prisma Client(@prisma/client)和 Postgres 驱动适配器(@prisma/adapter-pg)作为依赖项安装。

    terminal
    bun add -d prisma
    bun add @prisma/client @prisma/adapter-pg
    
  3. 使用 PostgreSQL 初始化 Prisma

    使用 bunx 和 Prisma CLI 初始化 schema 与迁移目录,并将 PostgreSQL 作为数据库。

    terminal
    bunx --bun prisma init --db
    

    这将创建一个基础 schema。将其更新为使用针对 Bun 优化的无 Rust 客户端:打开 prisma/schema.prisma 并修改 generator 块,然后添加一个 User 模型。

    prisma/schema.prisma
    generator client {
    	provider = "prisma-client"
    	output = "../generated/prisma" // [!code --]
    	output = "./generated" // [!code ++]
    	engineType = "client" // [!code ++]
    	runtime = "bun" // [!code ++]
    }
    
    datasource db {
    	provider = "postgresql"
    }
    
    model User { // [!code ++]
    	id    Int     @id @default(autoincrement()) // [!code ++]
    	email String  @unique // [!code ++]
    	name  String? // [!code ++]
    } // [!code ++]
    
  4. 配置数据库连接

    在 .env 文件中设置你的 Postgres 数据库 URL。

    .env
    DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"
    
  5. 创建并运行数据库迁移

    然后生成并运行初始迁移。

    该命令会将 .sql 迁移文件写入 prisma/migrations,并在你的 Postgres 数据库上执行该文件。Bun 使用 --bun 运行 CLI 时不会自动加载 .env。prisma init 生成的 prisma.config.ts 会从环境变量中读取 DATABASE_URL,因此请显式传入 --env-file=.env。

    terminal
    bun run --bun --env-file=.env prisma migrate dev --name init
    
    Text
    Loaded Prisma config from prisma.config.ts.
    
    Prisma schema loaded from prisma/schema.prisma.
    Datasource "db": PostgreSQL database "mydb", schema "public" at "localhost:5432"
    
    应用迁移 `20250114141233_init`
    
    以下迁移已根据新 schema 变更创建并应用:
    
    prisma/migrations/
      └─ 20250114141233_init/
        └─ migration.sql
    
    Your database is now in sync with your schema.
    
  6. 生成 Prisma Client

    prisma migrate dev 不会生成 Prisma client,因此请使用 Prisma CLI 生成它。该客户端提供了一个完全类型安全的 API,用于读取和写入数据库。

    terminal
    bun run --bun --env-file=.env prisma generate
    
  7. 使用 Postgres 适配器初始化 Prisma Client

    创建一个新文件 prisma/db.ts,使用 Postgres 适配器初始化 PrismaClient。

    prisma/db.ts
    import { PrismaClient } from "./generated/client";
    import { PrismaPg } from "@prisma/adapter-pg";
    
    const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
    export const prisma = new PrismaClient({ adapter });
    
  8. 创建测试脚本

    编写一个脚本,创建一个新用户,然后统计数据库中的用户数量。

    index.ts
    import { prisma } from "./prisma/db";
    
    // 创建新用户
    await prisma.user.create({
      data: {
        name: "John Dough",
        email: `john-${Math.random()}@example.com`,
      },
    });
    
    // 统计用户数量
    const count = await prisma.user.count();
    console.log(`数据库中共有 ${count} 个用户。`);
    
  9. 运行并测试应用

    使用 bun run 运行脚本。每次运行都会创建一个新用户。

    terminal
    bun run index.ts
    
    Text
    数据库中共有 1 个用户。
    
    terminal
    bun run index.ts
    
    Text
    数据库中共有 2 个用户。
    
    terminal
    bun run index.ts
    
    Text
    数据库中共有 3 个用户。
    

Prisma Postgres 现已在 Bun 中完成设置。继续开发应用时,请参考官方 Prisma Postgres 文档。