1. 스키마 설계
- Nest에서 mongoDB의 스키마 설게는 다음과 같이 Class와 decoration 패턴을 이용한다.
- Prop에는 각 데이터의 옵션을 설정한다.
- Option에는 생성된 스키마와 관련된 옵션을 넣는다.
2. Class-validator
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new HttpExceptionFilter());
await app.listen(process.env.PORT);
}
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, SchemaOptions } from 'mongoose';
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
const options: SchemaOptions = {
timestamps: true,
};
@Schema(options)
export class Cat extends Document {
@Prop({
required: true,
unique: true,
})
@IsEmail()
@IsNotEmpty()
email: string;
@Prop({
required: true,
})
@IsString()
@IsNotEmpty()
name: string;
@Prop({
required: true,
})
@IsString()
@IsNotEmpty()
password: string;
@Prop()
@IsString()
imgUrl: string;
}
export const CatSchema = SchemaFactory.createForClass(Cat);
참고자료