CRUD Operations with Prisma
Learn how to perform basic CRUD operations with the Prisma ORM.
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.
Press + to interact
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?
}
...