Nest Js MongoDB Schema 공식 문서: https://docs.nestjs.com/techniques/mongodb
DB Schema 설계 - cats.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, SchemaOptions } from 'mongoose';
const options: SchemaOptions = {
timestamps: true,
};
@Schema(options) // schema 정의
export class Cat extend Document{
@Prop({
required: true,
unique: true,
})
email: string;
@Prop({
required: true,
})
name: string;
@Prop({
required: true,
})
password: string;
}
export const CatSchema = SchemaFactory.createForClass(Cat);
@Prop() 데코레이터는 document의 속성을 정의
DB Schema Validation 설정 - cats.schema.ts
Nest Js Validation 공식 문서: https://docs.nestjs.com/pipes#class-validator
$ npm i --save class-validator class-transformer
@IsEmail() @IsNotEmpty() @IsString() 과 같이 호출하여 간단히 사용이 가능하다.
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
@Schema(options)
export class Cat extends Document {
@Prop({
required: true,
unique: true,
})
@IsEmail()
@IsNotEmpty()
email: string;
@Prop({
required: true,
})
@IsString()
@IsNotEmpty()
name: string;
}