1. Prisma 패키지와 @prisma/client 패키지 설치하기
npm install prisma --save-dev
npm install @prisma/client
  1. prisma 초기화
npx prisma init --datasource-provider postgresql
  1. prisma schema 작성하기
// This is your Prisma schema file,
// learn more about it in the docs: <https://pris.ly/d/prisma-schema>

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        String   @id @default(uuid())
  email     String   @unique
  firstName String
  lastName  String
  address   String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Product {
  id          String   @id @default(uuid())
  name        String
  description String?
  category    Category
  price       Float
  stock       Int
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

enum Category {
  FASHION
  BEAUTY
  SPORTS
  ELECTRONICS
  HOME_INTERIOR
  HOUSEHOLD_SUPPLIES
  KITCHENWARE
}

  1. Prisma Migration하기
npx prisma migrate dev
  1. PrismaClient 호출한 후 DB 접근하기
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const id = 'b8f11e76-0a9e-4b3f-bccf-8d9b4fbf331e';

const userData = {
  email: 'yjkim@example.com',
  firstName: '유진',
  lastName: '김',
  address: '충청북도 청주시 북문로 210번길 5',
};;

// 여러 유저 조회
const users = await prisma.user.findMany();

// 유저 1명 조회
const user = await prisma.user.findUnique({
  where: {
    id,
  },
});

// 유저 생성
const user = await prisma.user.create({
  data: userData
});

// 유저 수정
const user = await prisma.user.update({
  where: {
    id,
  },
  data: {
    email: 'yjkim2@example.com',
  },
});

// 유저 삭제
const user = await prisma.user.delete({
  where: {
    id,
  },
});

  1. 데이터 시딩하기
import { PrismaClient } from '@prisma/client';
import { USERS, PRODUCTS } from './mock.js';

const prisma = new PrismaClient();

async function main() {
  // 기존 데이터 삭제
  await prisma.user.deleteMany();
  await prisma.product.deleteMany();

  // 목 데이터 삽입
  await prisma.user.createMany({
    data: USERS,
    skipDuplicates: true,
  });

  await prisma.product.createMany({
    data: PRODUCTS,
    skipDuplicates: true,
  });
}

main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });