Prisma is an Object Relational Mapping (ORM) tool that helps developers interact with database in type-safe manner.
We can call it better than traditional ORM, because it is type-safe and it is easy to use. It is also easy to integrate with any database.
What are the problems with Traditional ORM?
installing prisma and editing the schema
import {PrismaClient} from @prisma/client;
const prisma = new PrismaClient();
async function main() {
const allUsers = await prisma.user.findMany();
console.log(allUsers);
}
Omit and Partial are the two concepts that are used in prisma. Omit helps to omit the fields that are not required. Partial helps to update the fields that are only required. like partial update.
import { prisma } from "@/infrastructure";
import { User } from "@prisma/client";
const createUser = async (
payload: Omit<User, 'id' | 'createdAt' | 'deletedAt' | 'updatedAt'>
) => {
return await prisma.user.create({
data: payload,
})
}
const updateUser = async (payload: Partial<User>) => {
return await prisma.user.update({
data: {
...payload,
},
where: {
id: payload.id,
},
})
}