HomeAbout

Prisma

What is Prisma

Prisma is an Object-Relational Mapping (ORM) tool that provides a way to interact with databases using an object-oriented approach.

// Query public async getProfileName(id: string): Promise<string> { const profileName = await this.prisma.user.findFirst({ where: { id }, select: { name: true, }, }); if (!profileName) { throw new NotFoundError(); } return JSON.stringify(profileName); } // Mutations // create const newUser = await prisma.user.create({ data: { name: "Alice", email: "alice@example.com", }, }); // update const updatedUser = await prisma.user.update({ where: { id: 1 }, data: { name: "Updated Name", }, }); // delete const deletedUser = await prisma.user.delete({ where: { id: 1 }, });

Transactions

Create transactions

await prisma.$transaction([ prisma.user.create({ data: { name: "Bob" } }), prisma.post.create({ data: { title: "New Post", content: "Content" } }), ]);

Raw Queries

$queryRaw

Accepts template literal string to execute as a SQL query

const result = await prisma.$queryRaw`SELECT * FROM User`

Because it accepts a template literal string, you can pass variables to the query

const email = 'emelie@prisma.io'; const result = await prisma.$queryRaw`SELECT * FROM User WHERE email = ${email}`;

Logging Prisma results need Json Stringify

console.log(JSON.stringify(result));
AboutContact