Search⌘ K
AI Features

CRUD Operations with Prisma

Explore how to use PrismaClient methods to implement CRUD operations in TypeScript for Node.js. Understand creating, reading, updating, and deleting database records including filtering and batch updates to manage backend data efficiently.

CRUD operations are an important building block of back-end applications. They allow us to Create, Read, Update and Delete data, usually from a database or data structure.

Create

To create a new record in the database, we use the create method in the PrismaClient class.

TypeScript 3.3.4
import express, { Request, Response, Express } from "express";
import { employee, PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const app: Express = express();
const port: number = 3000;
app.get("/", async (req: Request, res: Response) => {
try {
await prisma.employee.create({
data: {
email: "alice@wondermail.com",
name: "Alice Wonderland"
},
});
let foundEmployees: employee[] = await prisma.employee.findMany();
console.log(foundEmployees)
res.send(foundEmployees);
}
catch (e) {
console.log((e as Error).message)
}
});
app.listen(port, (): void => {
console.log(`Example app listening on port ${port} 🔥`);
});

The data key in the create method maps to the model that we created earlier.

...
model employee {
    id Int @default(autoincrement()) @id
    email String  @unique
    name  String?
}

Read

To retrieve data from the database, we have ...