| name | database-cli |
| description | Database management with Prisma ORM. Use for migrations, schema management, seeding, and database queries. |
| allowed-tools | Bash, Read, Edit |
| user-invocable | true |
database-cli
Database management with Prisma ORM - migrations, schema management, seeding, and database operations.
Installation
npm install prisma --save-dev
npm install @prisma/client
npx prisma init
Quick Reference
npx prisma generate
npx prisma db push
npx prisma migrate dev
npx prisma migrate deploy
npx prisma studio
npx prisma db seed
npx prisma format
Schema Management
prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String?
password String
role Role @default(USER)
posts Post[]
profile Profile?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([email])
}
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String
tags Tag[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([authorId])
}
model Profile {
id String @id @default(cuid())
bio String?
avatar String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
}
model Tag {
id String @id @default(cuid())
name String @unique
posts Post[]
}
enum Role {
USER
ADMIN
}
Migrations
Development
npx prisma migrate dev --name init
npx prisma migrate dev --name add_user_role
npx prisma db push
npx prisma migrate reset
Production
npx prisma migrate deploy
npx prisma migrate status
Migration Commands
npx prisma migrate dev
npx prisma migrate deploy
npx prisma migrate reset
npx prisma migrate status
npx prisma migrate resolve --applied "migration_name"
Generate Client
npx prisma generate
After generating, restart your dev server or TypeScript server.
Prisma Studio
npx prisma studio
npx prisma studio --port 5556
Seeding
package.json
{
"prisma": {
"seed": "tsx prisma/seed.ts"
}
}
prisma/seed.ts
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
await prisma.post.deleteMany()
await prisma.user.deleteMany()
const admin = await prisma.user.create({
data: {
email: 'admin@example.com',
name: 'Admin User',
password: 'hashed_password',
role: 'ADMIN',
profile: {
create: {
bio: 'System administrator',
},
},
},
})
const user = await prisma.user.create({
data: {
email: 'user@example.com',
name: 'Regular User',
password: 'hashed_password',
posts: {
create: [
{
title: 'First Post',
content: 'Hello World!',
published: true,
},
{
title: 'Draft Post',
content: 'Work in progress...',
published: false,
},
],
},
},
})
console.log({ admin, user })
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
Run Seed
npx prisma db seed
Common Queries
Create
const user = await prisma.user.create({
data: {
email: 'test@example.com',
name: 'Test User',
password: 'hashed',
},
})
const userWithPosts = await prisma.user.create({
data: {
email: 'author@example.com',
name: 'Author',
password: 'hashed',
posts: {
create: [
{ title: 'Post 1', content: 'Content 1' },
{ title: 'Post 2', content: 'Content 2' },
],
},
},
include: { posts: true },
})
const users = await prisma.user.createMany({
data: [
{ email: 'a@test.com', name: 'A', password: 'hash' },
{ email: 'b@test.com', name: 'B', password: 'hash' },
],
})
Read
const user = await prisma.user.findUnique({
where: { email: 'test@example.com' },
})
const post = await prisma.post.findFirst({
where: { published: true },
})
const users = await prisma.user.findMany({
where: { role: 'USER' },
orderBy: { createdAt: 'desc' },
take: 10,
skip: 0,
})
const userWithPosts = await prisma.user.findUnique({
where: { id: userId },
include: {
posts: {
where: { published: true },
orderBy: { createdAt: 'desc' },
},
profile: true,
},
})
const emails = await prisma.user.findMany({
select: { email: true, name: true },
})
Update
const user = await prisma.user.update({
where: { id: userId },
data: { name: 'New Name' },
})
const user = await prisma.user.upsert({
where: { email: 'test@example.com' },
update: { name: 'Updated' },
create: { email: 'test@example.com', name: 'New', password: 'hash' },
})
const updated = await prisma.user.updateMany({
where: { role: 'USER' },
data: { role: 'ADMIN' },
})
Delete
const user = await prisma.user.delete({
where: { id: userId },
})
const deleted = await prisma.post.deleteMany({
where: { published: false },
})
Filtering
const posts = await prisma.post.findMany({
where: {
AND: [
{ published: true },
{ title: { contains: 'prisma', mode: 'insensitive' } },
],
OR: [
{ author: { email: { endsWith: '@company.com' } } },
{ tags: { some: { name: 'featured' } } },
],
NOT: { authorId: excludedUserId },
},
})
Database URL Examples
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
DATABASE_URL="mysql://user:password@localhost:3306/mydb"
DATABASE_URL="file:./dev.db"
DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/mydb"
Introspection
npx prisma db pull
Troubleshooting
Reset everything
npx prisma migrate reset --force
Client out of sync
npx prisma generate
Migration drift
npx prisma migrate diff \
--from-schema-datamodel prisma/schema.prisma \
--to-schema-datasource prisma/schema.prisma
Check database connection
npx prisma db execute --stdin <<< "SELECT 1"
Production Checklist
- Run
npx prisma migrate deploy in CI/CD
- Don't use
db push in production
- Set
DATABASE_URL in environment
- Enable connection pooling for serverless
- Use transactions for related operations