We use cookies to enhance your experience on the site
CodeWorlds

Database - Prisma ORM

Database integration.

Prisma Setup 💾

1npm install prisma @prisma/client
2npx prisma init

Schema

1// prisma/schema.prisma
2model User {
3  id    Int     @id @default(autoincrement())
4  name  String
5  email String  @unique
6  posts Post[]
7}
8
9model Post {
10  id       Int    @id @default(autoincrement())
11  title    String
12  content  String
13  author   User   @relation(fields: [authorId], references: [id])
14  authorId Int
15}

Usage

1import { PrismaClient } from '@prisma/client';
2
3const prisma = new PrismaClient();
4
5// Create
6await prisma.user.create({
7  data: {
8    name: 'Jack',
9    email: 'jack@pirates.com'
10  }
11});
12
13// Read
14const users = await prisma.user.findMany({
15  include: { posts: true }
16});

See you there! 🚀

Go to CodeWorlds