시작하기(1일 차)
chatGPT를 이용하여 이름 짓기
추천 해 달라고 해서 사용 중이지 않은 이름으로 추천 받음.
개인적으로 맘에 드는 이름을 발견
Quiz Galaxy Planet : 퀴즈 은하 행성 - 다양한 퀴즈가 있는 거대한 은하 같은 공간을 의미합니다.
Quiz Galaxy Planet로 결정!
ERD 설계하기
아래 링크로 들어가 설계할 수 있음.
ERDCloud
Draw ERD with your team members. All states are shared in real time. And it's FREE. Database modeling tool.
www.erdcloud.com
간단하게 아래처럼 설계함.(처음 설계해서 이상할 수 있음)
nest + prisma 선택한 이유
- typescript로 백엔드 개발 가능
- 빠르고 쉽게 개발 가능
- 한 번 사용해본 적이 있음.
설치하기
아래 링크에 잘 나와있음.
Prisma | NestJS - A progressive Node.js framework
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea
docs.nestjs.com
혹시 처음 설치해본다면 아래처럼 터미널에서 설치하기
npm i -g @nestjs/cli
프로젝트 시작하기
nest new project-name
- 주로 npm을 많이 사용하기에 npm으로 설치
nest에서 지원해주는 명령어들 보기
nest -h
prisma 설치하기
npm install prisma --save-dev
prisma 명령어 볼 수 있음
npx prisma
prisma 초기 셋팅하기
npx prisma init
.env 파일 가서 DB설정하기
// [DB종류]://[유저이름]:[비밀번호]@[url]/[DB 이름]?[스키마]
DATABASE_URL="mysql://root:password@localhost/quizdb?schema=public"
- 깃허브 올릴 거라면 .gitignore에 .env 넣기
- mysql 워크벤치 사용하고 있음. (본인에게 맞는 툴 사용하면 될 듯)
모델 작성하기
ERD 설계했던 것을 바탕으로 모델 설정하기
// /prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql" // 중요! .env db종류와 같아야 함!
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
nickname String
password String
profile_image String?
introduction String?
scores Score[]
quizzes Quiz[]
sentRequests FriendRequest[] @relation("sentRequests")
receivedRequests FriendRequest[] @relation("receivedRequests")
friend FriendList[] @relation("Friend")
onwer FriendList[] @relation("onwer")
}
model Quiz {
id Int @id @default(autoincrement())
category String
question String
answer String
incorrect_answers String?
quiz_type String
visibility String
user_id Int
user User @relation(fields: [user_id], references: [id])
}
model FriendList {
id Int @id @default(autoincrement())
user_id Int
friend_id Int
user User @relation("onwer", fields: [user_id], references: [id])
friend User @relation("Friend", fields: [friend_id], references: [id])
@@unique([user_id, friend_id])
}
model Score {
id Int @id @default(autoincrement())
user_score Int?
category String
user_id Int
user User @relation(fields: [user_id], references: [id])
}
model FriendRequest {
id Int @id @default(autoincrement())
sender_id Int
receiver_id Int
sender User @relation("sentRequests", fields: [sender_id], references: [id])
receiver User @relation("receivedRequests", fields: [receiver_id], references: [id])
@@unique([sender_id, receiver_id])
}
마이그레이션 하기
npx prisma migrate dev
Prisma Client 설치 및 사용하기
설치
npm install @prisma/client
prisma.service.ts 파일 생성
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
}
}
오늘은 여기까지~ 다음에 이어서 하겠습니다.
'퀴즈앱 개발기 > BE' 카테고리의 다른 글
퀴즈앱 백엔드 4일차 (cogntio, 코드 재전송, 비밀번호 찾기) (0) | 2024.06.24 |
---|---|
뉴비개발자의 퀴즈앱 3일차 스웨거 만들기! (0) | 2024.06.20 |
백엔드 퀴즈앱 2일차! cognito 로그인, 회원가입, 이메일 인증 간단 구현! (2) | 2024.06.19 |
aws cognito 생성하기 (0) | 2024.06.19 |