TL;DR
是什么:下一代 Node.js 和 TypeScript ORM。
为什么用:类型安全查询、自动生成客户端、可视化数据库浏览器、迁移。
Quick Start
安装:
npm install prisma --save-dev
npm install @prisma/client
npx prisma init
定义 schema(prisma/schema.prisma):
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id])
authorId Int
}
运行迁移:
npx prisma migrate dev --name init
Cheatsheet
| 命令 | 描述 |
|---|---|
npx prisma init | 初始化 Prisma |
npx prisma migrate dev | 创建迁移 |
npx prisma generate | 生成客户端 |
npx prisma studio | 打开数据库浏览器 |
npx prisma db push | 推送 schema(无迁移) |
npx prisma db pull | 从 DB 拉取 schema |
Gotchas
CRUD 操作
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// 创建
const user = await prisma.user.create({
data: { email: '[email protected]', name: 'John' }
})
// 读取
const users = await prisma.user.findMany()
const user = await prisma.user.findUnique({ where: { id: 1 } })
// 更新
await prisma.user.update({
where: { id: 1 },
data: { name: 'Johnny' }
})
// 删除
await prisma.user.delete({ where: { id: 1 } })
关系
// 创建带关系
await prisma.post.create({
data: {
title: 'Hello',
author: { connect: { id: 1 } }
}
})
// 查询带关系
const userWithPosts = await prisma.user.findUnique({
where: { id: 1 },
include: { posts: true }
})
过滤
const users = await prisma.user.findMany({
where: {
email: { contains: '@example.com' },
name: { not: null },
posts: { some: { title: { startsWith: 'Hello' } } }
},
orderBy: { name: 'asc' },
take: 10,
skip: 0
})
Next Steps
- Prisma 文档 - 官方文档
- Prisma 示例 - 代码示例
- Prisma Schema 参考 - Schema 语法
- Prisma Studio - 可视化编辑器