Loading Blog Article...
Loading Blog Article...
Loading Blog Article...
Prisma is a next-generation Object-Relational Mapping (ORM) that makes working with databases easier and more reliable. It consists of three main components: Prisma Client, Prisma Schema, and Prisma Studio.
# Initialize a new Node.js project
npm init -y
# Install Prisma
npm install prisma --save-dev
# Initialize Prisma
npx prisma init
// 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
content String?
author User @relation(fields: [authorId], references: [id])
authorId Int
}
// Create a new user
const user = await prisma.user.create({
data: {
email: 'user@example.com',
name: 'John Doe',
},
})
// Read all users
const users = await prisma.user.findMany()
// Update a user
const updatedUser = await prisma.user.update({
where: { id: 1 },
data: { name: 'Jane Doe' },
})
// Delete a user
const deletedUser = await prisma.user.delete({
where: { id: 1 },
})
This guide covers the basics of Prisma ORM. As you become more comfortable with these concepts, you can explore more advanced features like middleware, raw queries, and complex relations.
Back