Databases with Prisma
⏱ 20 min read read
Setting Up Prisma:
npm install prisma @prisma/client
npx prisma init # creates prisma/schema.prisma + .env
# .env
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
# or SQLite for local dev:
DATABASE_URL="file:./dev.db"
Prisma Schema:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite" // or "postgresql"
url = env("DATABASE_URL")
}
model Student {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now())
grades Grade[]
}
model Grade {
id Int @id @default(autoincrement())
subject String
score Float
student Student @relation(fields: [studentId], references: [id])
studentId Int
}
npx prisma migrate dev --name init # run migration + generate
client
npx prisma studio # open visual DB browser
npx prisma db push # push schema without migration (dev)
Prisma Client --- CRUD:
import { PrismaClient } from '@prisma/client';
// Use a singleton in development
const globalForPrisma = global as unknown as { prisma: PrismaClient
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma =
prisma;
Always use a Prisma singleton in Next.js dev to avoid 'too many
connections'.
Put it in lib/prisma.ts and import from there everywhere.
Prisma works in Server Components and Route Handlers --- never in
Client Components.
Popular alternative: Drizzle ORM (lighter, SQL-first).
npm install prisma @prisma/client
npx prisma init # creates prisma/schema.prisma + .env
# .env
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
# or SQLite for local dev:
DATABASE_URL="file:./dev.db"
Prisma Schema:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite" // or "postgresql"
url = env("DATABASE_URL")
}
model Student {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now())
grades Grade[]
}
model Grade {
id Int @id @default(autoincrement())
subject String
score Float
student Student @relation(fields: [studentId], references: [id])
studentId Int
}
npx prisma migrate dev --name init # run migration + generate
client
npx prisma studio # open visual DB browser
npx prisma db push # push schema without migration (dev)
Prisma Client --- CRUD:
import { PrismaClient } from '@prisma/client';
// Use a singleton in development
const globalForPrisma = global as unknown as { prisma: PrismaClient
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma =
prisma;
Always use a Prisma singleton in Next.js dev to avoid 'too many
connections'.
Put it in lib/prisma.ts and import from there everywhere.
Prisma works in Server Components and Route Handlers --- never in
Client Components.
Popular alternative: Drizzle ORM (lighter, SQL-first).
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress