본문 바로가기

[IT/Programming]/Algorithm/Database

DATABASE setup of Panda Market by kipid

728x90
반응형
# DATABASE setup of Panda Market by kipid 판다마켓을 위한 DATABASE 를 구축해 봅시다. ## PH
  • 2024-09-24 : First posting.
## TOC ## 기본: npx prisma init ```[.scrollable] npx prisma init --datasource-provider postgresql ```/ 명령을 실행해서 다음과 같은 파일을 얻자. ```[.scrollable] generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } ```/
.env 파일 설정을 다음과 같이 하자.
```[.scrollable] DATABASE_URL="postgresql://postgres:[password]@localhost:5432/panda_market_dev?schema=public" PORT=3000 ```/ panda_market_dev DATABASE 가 없으면 알아서 만들어 준다. ## Schema 모음. Product 관련: , User 관련: Article 관련: , ### Struct 제약조건 모음. Product 관련: User 관련: Article 관련: Comments 관련: , ## Schema 짜기.
User
```[.scrollable] model User { id String @id @default(uuid()) // * Can be used by salt in encryption. email String @unique nickname String password String pwdIter Int @default(10000) ssnIter Int @default(1000) articles Article[] articleComments ArticleComment[] productComments ProductComment[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } ```/
Product
```[.scrollable] model Product { id String @id @default(uuid()) name String description String price Float tags String[] images String[] favoriteCount Int productComments ProductComment[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } ```/
Article
```[.scrollable] model Article { id String @id @default(uuid()) title String authorId String author User @relation(fields: [authorId], references: [id], onDelete: Restrict) content String articleComments ArticleComment[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } ```/
ProductComment
```[.scrollable] model ProductComment { id String @id @default(uuid()) content String productId String product Product @relation(fields: [productId], references: [id], onDelete: Restrict) commenterId String commenter User @relation(fields: [commenterId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } ```/
ArticleComment
```[.scrollable] model ArticleComment { id String @id @default(uuid()) content String articleId String article Article @relation(fields: [articleId], references: [id]) commenterId String commenter User @relation(fields: [commenterId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } ```/
### DATA seeding 적당한 mock data 를 가지고 DATA 를 seeding 해주자. ```[.scrollable] import { PrismaClient } from "@prisma/client"; import data from "./mock.js"; import * as dotenv from 'dotenv'; dotenv.config(); const prisma = new PrismaClient(); async function main() { await prisma.product.deleteMany(); await prisma.product.createMany({ data, skipDuplicates: true, }) } main() .then(async () => { await prisma.$disconnect(); }) .catch(async (e) => { console.error(e); await prisma.$disconnect(); process.exit(1); }); ```/ ### prisma superstruct 다음과 같은 제약 조건을 걸어주자.
struct-Product
```[.scrollable] import * as s from 'superstruct'; import isUuid from 'is-uuid'; const Uuid = s.define('Uuid', (value) => isUuid.v4(value)); export const CreateProduct = s.object({ name: s.size(s.string(), 1, 10), description: s.size(s.string(), 10, 100), price: s.min(s.number(), 0), tags: s.array(s.string()), images: s.array(s.string()), favoriteCount: s.min(s.integer(), 0), }); export const PatchProduct = s.object({ id: Uuid, ...s.partial(CreateProduct), }); ```/
s.set(s.string()) 은 어떻게 데이터를 보내는지 모르겠어서 못썼음. 그냥 array 방식으로 묶으면 될줄 알았는데... =ㅇ=;; ["가", "나", "다"] 는 Set 가 아니라고 함.
사용하는 define 은 ```[.scrollable] import * as s from 'superstruct'; import isEmail from 'is-email'; import isUuid from 'is-uuid'; const Uuid = s.define('Uuid', (value) => isUuid.v4(value)); const email = s.define('email', (value) => isEmail(value)); ```/
struct-User
```[.scrollable] export const CreateUser = s.object({ email: email, nickname: s.size(s.string(), 1, 20), password: s.string() }); export const PatchUser = s.partial(CreateUser); // * User id 는 따로 받아야 함. ```/
struct-Product
```[.scrollable] export const CreateProduct = s.object({ name: s.size(s.string(), 1, 10), description: s.size(s.string(), 10, 100), price: s.min(s.number(), 0), tags: s.size(s.array(s.string()), 0, 15), images: s.size(s.array(s.string()), 1, 2), favoriteCount: s.min(s.integer(), 0), }); export const PatchProduct = s.partial(CreateProduct); // * Product id 는 따로 받아야 함. ```/
struct-Article
```[.scrollable] export const CreateArticle = s.object({ title: s.size(s.string(), 1, 20), authorId: Uuid, content: s.size(s.string(), 10, 500), }); export const PatchArticle = s.partial(CreateArticle); // * Article id 는 따로 받아야 함. ```/
struct-ProductComment
```[.scrollable] export const CreateProductComment = s.object({ productId: Uuid, commenterId: Uuid, content: s.size(s.string(), 1, 255), }); // * Patch 는 위 데이터에 id 추가. ```/
struct-ArticleComment
```[.scrollable] export const CreateArticleComment = s.object({ articleId: Uuid, commenterId: Uuid, content: s.size(s.string(), 1, 255), }); // * Patch 는 위 데이터에 id 추가. ```/
## RRA
  1. https://panda-market-by-kipid.netlify.app/
  2. kipid's blog :: prisma with PostgreSQL 를 배워봅시다.
728x90
반응형