prisma client

  1. Install Prisma Client using npm or yarn: npm install @prisma/client yarn add @prisma/client

  2. Set up the Prisma schema file (schema.prisma) to define your data model:

// schema.prisma model User { id Int @id @default(autoincrement()) name String email String @unique posts Post[] }

model Post { id Int @id @default(autoincrement()) title String content String published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int }

  1. Run the Prisma migration commands to create the initial database schema based on the Prisma schema file:

npx prisma migrate dev npx prisma db push

  1. Initialize Prisma Client in your code to access the database:

// index.js const { PrismaClient } = require('@prisma/client'); const prisma = new PrismaClient();

  1. Use Prisma Client to perform database operations:

// index.js const main = async () => { const allUsers = await prisma.user.findMany({ include: { posts: true }, }); console.log(allUsers); };

main() .catch((e) => { throw e; }) .finally(async () => { await prisma.$disconnect(); });

  1. Handle errors and disconnect from the database when finished:

// index.js main() .catch((e) => { throw e; }) .finally(async () => { await prisma.$disconnect(); });