Prisma - the ultimate tool that will revolutionize your backend development journey! This comprehensive guide will take you through every step, from the initial setup to mastering the art of database management with Prisma.
Table of contents
Open Table of contents
- π Initializing the Project π
- π Creating a Basic Model π
- π Sending Queries to the Database π
- π Adding Nodemon for Automatic Refresh π
- β¨ Advanced Schema with Relations and Enums β¨
- π οΈ CRUD Operations with Prisma Client π οΈ
- π Connecting and Disconnecting Relations π
- π Fetching Posts with Relations π
- π Conclusion π
π Initializing the Project π
To kick off our journey with Prisma, letβs start by creating a new project and installing the necessary dependencies. Open your terminal and run the following commands:
yarn init -y
yarn add --dev typescript ts-node @types/node nodemon
Next, letβs initialize TypeScript in our project and add the Prisma CLI:
yarn tsc --init
yarn add --dev prisma
Now, we need to set up Prisma to work with PostgreSQL:
yarn prisma init --datasource-provider postgresql
π Creating a Basic Model π
Once Prisma is set up, letβs create a simple model in the _schema.prisma
file. In this example, weβll create a User
model with an id
and a name
:
model User {
id Int @id @default(autoincrement())
name String
}
After defining the model, itβs time to create the corresponding database tables using Prisma Migrate:
yarn prisma migrate dev --name init
π Sending Queries to the Database π
Now, letβs explore how to interact with the database using Prisma Client. Create a file called script.ts
and add the following code:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
// ... you will write your Prisma Client queries here
}
main()
.catch(e => {
console.log(e.message);
})
.finally(async () => {
await prisma.$disconnect();
});
In the script.ts
file, you can use Prisma Client to send queries to the database. For example, to add a new user, you can use the following code:
const user = await prisma.user.create({ data: { name: "Steve Rogers" } });
And to retrieve all users from the database:
const users = await prisma.user.findMany();
console.log(users);
π‘ Tip: Donβt forget to install the Prisma Client and generate the Prisma Client code before running the script:
yarn add @prisma/client
yarn prisma generate
π Adding Nodemon for Automatic Refresh π
To make development more efficient, letβs set up Nodemon to automatically refresh the script when changes are made. Add the following script to your package.json
:
"scripts": {
"devStart": "nodemon script.ts"
}
Now, you can run the following command to start your development server:
yarn devStart
β¨ Advanced Schema with Relations and Enums β¨
Now, letβs dive into more advanced concepts in Prisma and create a schema with relations and an enum. You can use this as a reference for building more complex data models.
// SCHEMA
model User {
id String @id @default(uuid())
name String
age Int
email String @unique
isAdmin Role @default(BASIC)
authoredPosts Post[] @relation("AuthoredPosts") // One to Many
bookmarkedPosts Post[] @relation("BookmarkedPosts")
userPreference UserPreference? @relation(fields: [userPreferenceId], references: [id])
userPreferenceId String? @unique
@@unique([email, name])
@@index([email])
}
// One to One Relation
model UserPreference {
id String @id @default(uuid())
user User?
emailUpdates Boolean @default(false)
}
model Post {
id String @id @default(uuid())
title String
rating Float
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User @relation("AuthoredPosts", fields: [authorId], references: [id])
authorId String
bookmarkedBy User? @relation("BookmarkedPosts", fields: [bookmarkedById], references: [id])
bookmarkedById String?
categories Category[]
}
// Many to Many Relation
model Category {
id String @id @default(uuid())
posts Post[]
}
enum Role {
BASIC
ADMIN
}
π οΈ CRUD Operations with Prisma Client π οΈ
To perform CRUD (Create, Read, Update, Delete) operations using Prisma Client, letβs dive into some code examples.
Creating a User:
async function create() {
const user = await prisma.user.create({
data: {
name: "Lizard Zuck",
email: "zuckiscuck@gmail.com",
userPreference: {
create: {
emailUpdates: true,
},
},
},
select: {
name: true,
userPreference: { select: { id: true } },
},
});
return user;
}
Reading a User by Email:
const user = await prisma.user.findUnique({
where: {
email: "ankit1042002@gmail.com",
},
});
console.log(user);
Filtering Users with Complex Conditions:
const user = await prisma.user.findMany({
where: {
name: { not: "Ankit" },
age: { lt: 20 }, // lt -> less than
email: { contains: "@gmail.com", startsWith: "an" },
},
});
Updating User Data:
const updatedUser = await prisma.user.update({
where: {
email_name: {
email: "ankit.ks@yahoo.com",
name: "Ankit",
},
},
data: {
email: "ankit.ks@yahoo.com",
},
});
console.log(updatedUser);
Deleting a User:
const deletedUser = await prisma.user.delete({
where: {
email_name: {
email: "ankit.ks@yahoo.com",
name: "Ankit",
},
},
});
console.log(deletedUser);
π Connecting and Disconnecting Relations π
To connect and disconnect relations, such as connecting a User
to their UserPreference
, you can use the following examples:
Connecting UserPreference:
const preference = await prisma.userPreference.create({
data: {
emailUpdates: true,
},
});
const user = await prisma.user.update({
where: {
email_name: {
email: "ankit.ks@yahoo.com",
name: "Ankit",
},
},
data: {
userPreference: {
connect: {
id: preference.id,
},
},
},
});
Disconnecting UserPreference:
const user = await prisma.user.update({
where: {
email_name: {
name: "Ankit",
email: "ankit.ks@yahoo.com",
},
},
data: {
userPreference: {
disconnect: true,
},
},
});
π Fetching Posts with Relations π
To fetch posts related to users, you can use Prisma Client to perform queries like this:
async function main() {
const posts = await prisma.user.findMany({
where: {
authoredPosts: {
every: {
title: {
startsWith: "T",
},
},
},
},
});
console.log(posts);
}
π Conclusion π
Congratulations! Youβve now learned the essentials of using Prisma. Now, my coding Jedi, may the Prisma force be with you. Remember, even Darth Bugs will fear your skills!.