0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Prismaで始めるデータベース管理

0
Last updated at Posted at 2025-04-27

はじめに

本記事では、TypeScript + Prisma を使って、Node.jsアプリケーションのデータベース管理を型安全かつ効率的に行う方法を紹介します。

TypeScriptの静的型チェックと、Prismaの直感的なデータモデリング・クエリ機能を組み合わせることで、開発速度の向上実行時エラーの削減が期待できます。

🛠️ 必要な環境

  • Node.js(v16以上推奨)
  • TypeScriptの基本的な知識
  • PostgreSQLやMySQLなど、任意のRDBMSへのアクセス権

1. プロジェクトのセットアップ

  • プロジェクト作成と初期化
mkdir my-prisma-ts-app
cd my-prisma-ts-app
npm init -y
  • TypeScriptのセットアップ

TypeScript環境が未構築の場合は、以下の記事を参考にしてください:

👉 TypeScript 開発環境のセットアップ手順(Qiita)

  • Prisma の導入
npm install prisma --save-dev
npm install @prisma/client
npx prisma init

以下のような構成が生成されます:

my-prisma-ts-app/
├── prisma/
│   └── schema.prisma ← スキーマ定義ファイル
└── .env              ← DB接続文字列など

2. データベーススキーマの定義

例として、User モデルを定義:

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

.env ファイルに接続情報を記述(例: PostgreSQL):

DATABASE_URL="postgresql://user:password@localhost:5432/mydb"

3. Prisma Migrateでマイグレーション

npx prisma migrate dev --name init

これによりマイグレーション履歴が作成され、データベースが User テーブルで初期化されます。

4. PrismaClientのSingleton化

PrismaClientはインスタンスごとにDB接続を生成するため、毎回 new PrismaClient() すると接続が無駄に増えます。特に開発中やサーバレス環境では深刻な問題になります。

そこで、Singletonパターンで1インスタンスだけを使い回す方法が推奨されます。

  • 📁 lib/prisma.ts を作成
lib/prisma.ts
import { PrismaClient } from '@prisma/client';

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined;
};

export const prisma =
  globalForPrisma.prisma ??
  new PrismaClient({
    log: ['query'], // 必要に応じて省略
  });

if (process.env.NODE_ENV !== 'production') {
  globalForPrisma.prisma = prisma;
}

✅ これでHot Reload時の再接続を防げます。

5. Prismaを使ったデータ操作例

index.t
import { prisma } from './lib/prisma';

async function main() {
  const newUser = await prisma.user.create({
    data: {
      name: 'Alice',
      email: 'alice@example.com',
    },
  });

  console.log('Created user:', newUser);
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

まとめ

TypeScriptとPrismaの組み合わせにより、Node.jsアプリケーションのデータベース管理がより安全で効率的になります。このセットアップは、開発プロセスを加速させ、エラーのリスクを最小化するため、特に大規模なアプリケーション開発において非常に有効です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?