서버 설정 및 간단한 가지고 놀아보기

들어가면서

필요한 기술 스텍을 사용하는 방법을 간단하게 적용해보면서 학습해본다.

0. 밑작업 : 몽고 DB Docker 설정

  • 도커파일을 구성 및 도커 컨테이너로 미리 DB를 설치해준다
# Dockerfile
FROM mongo:latest

ENV MONGO_INITDB_ROOT_USERNAME=admin
ENV MONGO_INITDB_ROOT_PASSWORD=admin

EXPOSE 27017
# docker-compose.yml
version: '3.1'

services:
  mongodb:
    image: mongo
    container_name: mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    ports:
      - 27017:27017
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:
  • 데이터베이스 설치
docker compose up -d

1. Prisma 설치

  • Prisma 를 설치 한다
yarn add prisma --dev # 의존성 추가 옵션 
  • Prisma 최초 초기화
> npx prisma init

✔ Your Prisma schema was created at prisma/schema.prisma
  You can now open it in your favorite editor.

warn You already have a .gitignore file. Don't forget to add `.env` in it to not commit any private information.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql, sqlite, sqlserver, mongodb or cockroachdb.    
3. Run prisma db pull to turn your database schema into a Prisma schema.
4. Run prisma generate to generate the Prisma Client. You can then start querying your database.

More information in our documentation:
https://pris.ly/d/getting-started

┌────────────────────────────────────────────────────────────────┐
│  Developing real-time features?                                │
│  Prisma Pulse lets you respond instantly to database changes.  │
│  https://pris.ly/cli/pulse                                     │
└────────────────────────────────────────────────────────────────┘

2. Prisma Connect to DB

  • schema.prisma 파일 설정
datasource db {  
provider = "mongodb"  
url = env("DATABASE_URL")  
}
  • 환경변수 설정
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"

3. DB 구조를 설정한다


model Post {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
  comments Comment[]
}

model User {
  id       String   @id @default(auto()) @map("_id") @db.ObjectId
  email    String   @unique
  password String
  name     String?
  address  Address?
  posts    Post[]
}

model Comment {
  id      String @id @default(auto()) @map("_id") @db.ObjectId
  comment String
  post    Post   @relation(fields: [postId], references: [id])
  postId  String @db.ObjectId
}

// Address is an embedded document
type Address {
  street String
  city   String
  state  String
  zip    String
}