본문 바로가기

[IT/Programming]/Algorithm/Database

DATABASE setup of Panda Market by kipid

반응형
m.logPrint() is working!

<eq> and <eqq> tags are rendered to MathJax format, being enclosed by \ ( \ ) and \ [ \ ].

docuK1 scripts started!
If this log is not closed automatically, there must be an error somewhere in your document or scripts.

Table of Contents is filled out.

Auto numberings of sections (div.sec>h2, div.subsec>h3, div.subsubsec>h4), <eqq> tags, and <figure> tags are done.

<cite> and <refer> tags are rendered to show bubble reference.

<codeprint> tags are printed to corresponding <pre> tags, only when the tags exist in the document.


Current styles (dark/bright mode, font-family, font-size, line-height) are shown.

disqus.js with id="disqus-js" is loaded.

kakao.js with id="kakao-js-sdk" is loaded.

New ShortKeys (T: Table of Contents, F: Forward Section, D: Previous Section, L: To 전체목록/[Lists]) are set.

m.delayPad=0;
m.wait=1024;
wait 1318ms.
▼ Hide
Toggle a mess
Go (FS)
TofC
DocuK Log
Backward
Forward
RRA
Lists
CmtZ
CmtX
Handle CmtZ
Log in
out focus
이 글이 도움이 되셨다면, 광고 클릭 한번씩만 부탁드립니다 =ㅂ=ㅋ. (If this article was helpful, please click the ad once. Thank you. ;)
Mode: Bright; Font: Noto Sans KR; font-size: 18.0px (10.0); line-height: 1.6;
width: 1280, height: 720, version: 3.1.1
Canonical URI: https://kipid.tistory.com/entry/DATABASE-setup-of-Panda-Market-by-kipid
dg:plink (Document Global Permanent Link): https://kipid.tistory.com/378
document.referrer: Empty
This document is rendered by docuK (See also SEE (Super Easy Edit) of docuK and pure SEE).

DATABASE setup of Panda Market by kipid

판다마켓을 위한 DATABASE 를 구축해 봅시다.

TPH1.Posting History

▼ Show/Hide

T1.기본: npx prisma init

▼ Show/Hide
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
npx prisma init --datasource-provider postgresql
명령을 실행해서 다음과 같은 파일을 얻자.
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
.env 파일 설정을 다음과 같이 하자.
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
DATABASE_URL="postgresql://postgres:[password]@localhost:5432/panda_market_dev?schema=public"
PORT=3000
panda_market_dev DATABASE 가 없으면 알아서 만들어 준다.
▲ Hide

T2.Schema 모음.

▼ Show/Hide
Product 관련:
Product
Product
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}
,
ProductComment
ProductComment
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}
User 관련:
User
User
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}
Article 관련:
Article
Article
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}
,
ArticleComment
ArticleComment
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}

T2.1.Struct 제약조건 모음.

Product 관련:
struct-Product
struct-Product
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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),
});
User 관련:
struct-User
struct-User
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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 는 따로 받아야 함.
Article 관련:
struct-Article
struct-Article
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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 는 따로 받아야 함.
Comments 관련:
struct-ProductComment
struct-ProductComment
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
export const CreateProductComment = s.object({
	productId: Uuid,
	commenterId: Uuid,
	content: s.size(s.string(), 1, 255),
});
	// * Patch 는 위 데이터에 id 추가.
,
struct-ArticleComment
struct-ArticleComment
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
export const CreateArticleComment = s.object({
	articleId: Uuid,
	commenterId: Uuid,
	content: s.size(s.string(), 1, 255),
});
	// * Patch 는 위 데이터에 id 추가.
▲ Hide

T3.Schema 짜기.

▼ Show/Hide
User
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
}

T3.1.DATA seeding

적당한 mock data 를 가지고 DATA 를 seeding 해주자.
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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);
	});

T3.2.prisma superstruct

다음과 같은 제약 조건을 걸어주자.
struct-Product
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
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
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
export const CreateProductComment = s.object({
	productId: Uuid,
	commenterId: Uuid,
	content: s.size(s.string(), 1, 255),
});
	// * Patch 는 위 데이터에 id 추가.
struct-ArticleComment
On the left side of codes is there a hiden button to toggle/switch scrollability ({max-height:some} or {max-height:none}).
export const CreateArticleComment = s.object({
	articleId: Uuid,
	commenterId: Uuid,
	content: s.size(s.string(), 1, 255),
});
	// * Patch 는 위 데이터에 id 추가.
▲ Hide
이 글이 도움이 되셨다면, 광고 클릭 한번씩만 부탁드립니다 =ㅂ=ㅋ. (If this article was helpful, please click the ad once. Thank you. ;)
반응형
Get page views