Its Pets 프로젝트를 Sequelize에서 Prisma로
type module
MySQL을 사용
Access Token과 Refresh Token 구현해보기
npm i prisma
npm i @prisma/client
npx prisma init
npx prisma db push
// prisma/schema.prisma
model Users {
userId Int @id @default(autoincrement()) @map("userId")
name String
email String @unique
password String
description String? @default("안녕하세요")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @default(now()) @map("updatedAt")
Posts Posts[]
}
model Posts {
postId Int @id @default(autoincrement()) @map("postId")
createdId Int @map("createdId")
title String
content String
imgUrl String @map("imgUrl")
petName String? @map("petName")
category String
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @default(now()) @map("updatedAt")
user Users @relation(fields: [createdId], references: [userId], onDelete:Cascade)
@@map("Posts")
}
model Comments {
commentId Int @id @default(autoincrement()) @map("commentId")
userId Int @map("userId")
postId Int @map("postId")
content String
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @default(now()) @map("updatedAt")
user Users @relation(fields: [userId], references: [userId])
post Posts @relation(fields: [postId], references: [postId])
@@map("Comments")
}
? 표시는 해당 필드가 null 값을 허용함을 의미.
@map은 Prisma가 데이터베이스 테이블과 열 이름을 소문자로 변환하는 것을 방지합니다.
import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient({
// Prisma를 이용해 데이터베이스를 접근할 때, SQL을 출력해줍니다.
log: ['query', 'info', 'warn', 'error'],
// 에러 메시지를 평문이 아닌, 개발자가 읽기 쉬운 형태로 출력해줍니다.
errorFormat: 'pretty',
}); // PrismaClient 인스턴스를 생성합니다.
npx prisma db push --force-reset <<<<<< db데이터삭제
findone > findFrist
create<< data{name:'example'} 형식
const express = require('express');
import express from "express";
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
import path, { dirname } from "path";
import { fileURLToPath } from "url";
const dirname = dirname(fileURLToPath(import.meta.url));
app.set("views", path.join(dirname, "views"));
app.set("view engine", "ejs");