Why Prisma
Prisma gives you auto-generated, type-safe database client from a schema file. Query your database with TypeScript autocomplete, no raw SQL, no query strings — just typed methods.
Install
npm install prisma @prisma/client
npx prisma init
Define 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[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
tags Tag[]
}
model Tag {
id Int @id @default(autoincrement())
name String @unique
posts Post[]
}
npx prisma migrate dev --name init
npx prisma generate
Type-Safe Queries
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
// Create
const user = await prisma.user.create({
data: { email: 'alice@example.com', name: 'Alice' },
});
// Find with relations
const userWithPosts = await prisma.user.findUnique({
where: { email: 'alice@example.com' },
include: { posts: { where: { published: true } } },
});
// Complex query
const results = await prisma.post.findMany({
where: {
AND: [
{ published: true },
{ author: { email: { contains: '@example.com' } } },
{ tags: { some: { name: 'typescript' } } },
],
},
orderBy: { createdAt: 'desc' },
take: 10,
include: { author: true, tags: true },
});
Prisma Studio
npx prisma studio
# Opens visual database browser at localhost:5555
Key Features
- Auto-generated client — from schema to typed queries
- Migrations — schema-driven, version-controlled
- Relations — include, select, nested writes
- Prisma Studio — visual database browser
- Multi-database — Postgres, MySQL, SQLite, MongoDB, SQL Server
- Edge ready — Prisma Accelerate for serverless
Resources
Need to extract database schemas, query patterns, or ORM configs? Check out my Apify tools or email spinov001@gmail.com for custom solutions.
Top comments (0)