Skip to content

NOTE

**注意** — 現在、Prisma は特定の生成コードを実行するために Node.js のインストールを必要とします。`bunx prisma` コマンドを実行する環境に Node.js がインストールされていることを確認してください。

新しいプロジェクトを作成

まず、ディレクトリを作成し、bun init で初期化します。

bash
mkdir prisma-postgres-app
cd prisma-postgres-app
bun init

Prisma 依存関係をインストール

次に Prisma CLI(prisma)、Prisma Client(@prisma/client)、および accelerate 拡張機能を依存関係としてインストールします。

bash
bun add -d prisma
bun add @prisma/client @prisma/extension-accelerate

PostgreSQL で Prisma を初期化

bunx で Prisma CLI を使用してスキーマとマイグレーションディレクトリを初期化します。データベースとして PostgreSQL を使用します。

bash
bunx --bun prisma init --db

これにより、基本的なスキーマが作成されます。Bun 最適化で新しい Rust フリーのクライアントを使用するように更新する必要があります。prisma/schema.prisma を開き、ジェネレーターブロックを変更し、シンプルな User モデルを追加します。

prisma
generator client {
  provider = "prisma-client"
  output = "./generated"
  engineType = "client"
  runtime = "bun"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User { 
  id    Int     @id @default(autoincrement()) 
  email String  @unique
  name  String?
} 

データベース接続を設定

.env ファイルで PostgreSQL データベース URL を設定します。

ini
DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"

データベースマイグレーションを作成して実行

初期マイグレーションを生成して実行します。

これにより、prisma/migrations.sql マイグレーションファイルが生成され、PostgreSQL データベースに対してマイグレーションが実行されます。

bash
bunx --bun prisma migrate dev --name init
txt
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydb", schema "public" at "localhost:5432"

Applying migration `20250114141233_init`

The following migration(s) have been created and applied from new schema changes:

prisma/migrations/
  └─ 20250114141233_init/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (6.17.1) to ./generated in 18ms

Prisma Client を生成

出力に示されているように、Prisma は新しいマイグレーションを実行するたびに Prisma クライアント を再生成します。クライアントは、データベースからの読み取りと書き込みのための完全に型付けされた API を提供します。Prisma CLI を使用してクライアントを手動で再生成できます。

sh
bunx --bun prisma generate

Accelerate で Prisma Client を初期化

Prisma クライアントインスタンスを作成する必要があります。Postgres アダプターで PrismaClient を初期化するために、新しいファイル prisma/db.ts を作成します。

ts
import { PrismaClient } from "./generated/client";
import { withAccelerate } from '@prisma/extension-accelerate'

export const prisma = new PrismaClient().$extends(withAccelerate())

テストスクリプトを作成

新しいユーザーを作成し、データベース内のユーザー数をカウントするシンプルなスクリプトを作成しましょう。

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} 人のユーザーがいます。`);

アプリケーションを実行してテスト

bun run でこのスクリプトを実行しましょう。実行するたびに新しいユーザーが作成されます。

bash
bun run index.ts
txt
データベースには 1 人のユーザーがいます。
bash
bun run index.ts
txt
データベースには 2 人のユーザーがいます。
bash
bun run index.ts
txt
データベースには 3 人のユーザーがいます。

これで完了です!Bun を使用して Prisma Postgres を設定したので、アプリケーションの開発を続ける際に 公式 Prisma Postgres ドキュメント を参照することをお勧めします。

Bun by www.bunjs.com.cn 編集