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入門 ORM

Posted at

Prisma入門

1. Prismaとは?

  • データベースとプログラムをつなぐORM(Object Relational Mapper)
  • SQLを書く代わりにTypeScriptで直感的にデータ操作できる
  • Prisma Client(使うもの)、Prisma Migrate(マイグレーション)、Prisma Studio(GUIツール)がある

2. セットアップ手順

  • プロジェクト作成
  • 必要パッケージインストール
    pnpm add @prisma/client
    pnpm add -D prisma
    
  • Prisma初期化
    npx prisma init
    

3. モデルを作ろう(例:Userテーブル)

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

4. マイグレーションでDBを作成

npx prisma migrate dev --name init

5. Prisma Clientでデータ操作

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  const user = await prisma.user.create({
    data: {
      name: '山田太郎',
      email: 'taro@example.com',
    },
  });
  console.log(user);
}

main();

6. Prisma StudioでGUI操作

npx prisma studio

7. まとめ

  • Prismaを使うと型安全・直感的にDB操作できる
  • マイグレーション管理も簡単
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?