This repository demonstrates Prisma ORM CRUD operations with PostgreSQL using TypeScript.
Here is a structured reference of all the main Prisma queries.
const user = await prisma.user.create({
data: {
name: "Jhankar Mahbub",
email: "[email protected]",
profilePhoto: "https://programing-hero.com/level2/jkr.png"
}
});
Inserts a single user into the database and returns the created object.
const users = await prisma.user.createMany({
data: [
{ name: "Mir", email: "[email protected]" },
{ name: "Tanmoy", email: "[email protected]" },
{ name: "Mizan", email: "[email protected]" },
{ name: "Imun", email: "[email protected]" }
]
});
Inserts multiple users at once. Returns a count of inserted rows.
const users = await prisma.user.findMany({
where: { email: { contains: "ph.com", mode: "insensitive" } },
orderBy: { name: "asc" }
});
Retrieves multiple users as an array. Supports filtering, ordering, and case-insensitive search.
const user = await prisma.user.findUnique({
where: { id: 1 }
});
Retrieves a single user by a unique field (e.g., id
or email
). Returns null
if not found.
const user = await prisma.user.findUniqueOrThrow({
where: { id: 6 }
});
Retrieves a single user by a unique field. Throws an error if the user does not exist.
const updatedUser = await prisma.user.update({
where: { id: 1 },
data: { name: "Mezba Abedin", email: "[email protected]" }
});
Updates a single user and returns the updated object.
const result = await prisma.user.updateMany({
where: { id: { gt: 2 } },
data: { profilePhoto: "https://programing-hero.com/level2/default-image.png" }
});
Updates multiple users matching the filter. Returns the count of updated rows.
const updateProfilePhoto = await prisma.user.updateManyAndReturn({
where: {
id: {
gt: 2
}
},
data: {
profilePhoto: "https://programing-hero.com/level2/default-image.png"
}
})
const deletedUser = await prisma.user.delete({
where: { id: 1 }
});
Deletes a single user by unique field and returns the deleted object.
const result = await prisma.user.deleteMany({
where: { id: { lt: 3 } }
});
Deletes multiple users matching the filter. Returns the count of deleted rows.
const users = await prisma.user.findMany({
orderBy: { createdAt: "desc" }
});
Sorts the results based on a field in ascending (asc
) or descending (desc
) order.
const users = await prisma.user.findMany({
where: {
name: { contains: "john", mode: "insensitive" }
}
});
Performs a substring search in a string field with optional case-insensitivity.
This guide follows the exact order of Prisma queries used in the
hello-prisma
repository, making it easy to understand and practice CRUD operations with PostgreSQL.