라벨이 schemas인 게시물 표시

Prisma Many to Many(다대다) schema 작성

이미지
 해당 스키마는 아래 글을 참고로 만들었습니다. 링크 : SQL 관계도 Many to Many(다대다 관계) 해당 링크의 스키마를 작성하기 위해서 아래와 갖이 코드를 작성합니다. // prisma/schema.prisma // This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env ( "DATABASE_URL" ) } model users { id Int @default ( autoincrement ()) @id full_name String @unique created_at DateTime @default ( now ()) @db.Timestamptz ( 3 ) comments joinTable [] } model joinTable { users users @relation ( fields : [userId], references : [id] ) // users와joinTable은 1:N이다 userId Int comment comments @relation ( fields : [commentId], references : [id] ) // comments와 joinTable은 1:N이다. commentId Int @@id ( [userId, commentId] ) // userId, commentId는 Primary key이다 } model comments { id Int @default ( autoincrement ()) @id conten...

NestJS MongoDB 사용하기4- DB Update Delete

 안녕하세요. 알렉스입니다. 저번 포스팅에 이어 이번에는 MongoDB에 CRUD기능중 Update, Delete을 넣도록 하겠습니다. 처음 들어오셨다면 이전글을 참고해 주시기 바랍니다. 이전글 : NestJS MongoDB 사용하기3 - DB Create Read // src/users/dto/update-user.dto.ts import { ApiProperty , PartialType } from '@nestjs/swagger' ; import { IsEmpty , IsString } from 'class-validator' ; import { CreateUserDto } from './create-user.dto' ; export class UpdateUserDto extends PartialType ( CreateUserDto ) { @ IsString () @ IsEmpty () @ ApiProperty ({ readOnly : true , }) id : string ; } 코드1) UpdateUserDto 작성 코드1에서 id를 다시 정의한 이유는 'extends PartialType'이 CreateUserDto이기 때문입니다. id는 필수값으로 넣어야 하는에 업데이트 할때는 파라메터로 넣기 때문에 필수가 아닙니다. 따라서 재 설정을 하여 id는 비어도 class-validator에서 걸리지 않도록 수정한 것입니다. // src/users/users.service.ts import { Injectable , NotFoundException } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import mongoose from 'mongoose' ; import { CreateUserDto ...

NestJS MongoDB 사용하기3- DB Create Read

이미지
 안녕하세요. 알렉스입니다. 저번 포스팅에 이어 이번에는 MongoDB에 CRUD기능을 넣도록 하겠습니다. 처음 들어오셨다면 이전글을 참고해 주시기 바랍니다. 이전글 : NestJS MongoDB 사용하기2 - DB 스키마 생성 // src/users/dto/create-user.dto.ts import { ApiProperty } from '@nestjs/swagger' ; import { IsInt , IsNotEmpty , IsObject , IsOptional , IsString , Max , Min , } from 'class-validator' ; export class CreateUserDto { @ IsString () @ IsNotEmpty () @ ApiProperty ({ description : '유저 고유ID' , type : String , example : 'abc123' , }) id : string ; @ IsString () @ IsNotEmpty () @ ApiProperty ({ description : '유저 이름' , type : String , example : 'alex' , }) name : string ; @ IsString () @ IsNotEmpty () @ ApiProperty ({ description : '유저 전화번호' , type : String , example : '01035451268' , }) mobile : string ; @ IsInt () @ Min ( 20 ) @ Max ( 99 ) @ IsNotEmpty () @ ApiProperty ({ description : ...

NestJS MongoDB 사용하기2 - DB 스키마 생성

이미지
 안녕하세요. 알렉스입니다. 이번에는 NestJS을 이용하여 MongoDB의 스키마를 작성해서 collection을 만드는 글을 작성할려고 합니다.  mongodb셋팅은 이전글을 참고해 주시기 바랍니다. 이전글 : NestJS MongoDB 사용하기1 - DB 접속 이제 NestJS안에 users resource를 생성하도록 하겠습니다. NestJS resource 패키지 생성 NestJS CLI를 이용하여 controller, service, dto, entity를 자동으로 생성 $ nest g res copy 사진1) 생성할 resource 명칭 입력(users입력) 사진2) layer선택(REST API) 사진3) CRUD자동 생성 유무(Y) 사진1 ~ 3까지 진행하면 src폴더 안에 users이 생성된 것을 확인할수 있습니다. 먼저 /src/users/entities 폴더를 삭제하고 대신 schemas폴더를 생성합니다. 그리고 그안에 user.schema.ts 파일을 생성하고 아래와 같이 코드를 작성합니다. // src/users/schemas/user.schema.ts import { Prop , Schema , SchemaFactory } from '@nestjs/mongoose' ; @ Schema ({ timestamps : true , // document 작성시 시간 기록 }) export class Users { @ Prop () // 유저 id id : string ; @ Prop () // 유저 이름 name : string ; @ Prop () // 유저 전화번호 mobile : string ; @ Prop () // 유저 나이 age : number ; @ Prop ({ type : 'Array' }) // 유저 취미 hobby : string []; @ Prop ({ type : 'Object' })...