แปลง JSON เป็นคลาส schema ของ NestJS @nestjs/mongoose: คลาส @Schema() ที่มี @Prop() ทุกฟิลด์ schema ที่สร้างด้วย SchemaFactory.createForClass() และไทป์ ProductDocument ทั้งหมดในไฟล์เดียว ทุกฟิลด์ยังมี @IsNotEmpty() หรือ @IsOptional() ของ class-validator ด้วย
ใช้เมื่อโมดูล NestJS ต้องการ schema สำหรับลงทะเบียนกับ MongooseModule.forFeature() และคุณมีตัวอย่างเอกสารที่จะเก็บอยู่แล้ว แต่การเขียน decorator ทุกตัวเองเป็นส่วนที่เสียเวลา
ชื่อโมเดล (ค่าเริ่มต้น MyModel) พิมพ์ product จะได้ Product, ProductDocument และ ProductSchemaproduct.schema.ts แล้วลงทะเบียนด้วย MongooseModule.forFeature([{ name: Product.name, schema: ProductSchema }]){
"sku": "KB-87-RED",
"title": "Mechanical keyboard",
"price": 89.5,
"stock": 42,
"discontinued": false,
"restocked_at": "2024-03-18"
}import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as MongooseSchema } from 'mongoose';
import { IsNotEmpty, IsOptional } from 'class-validator';
export type ProductDocument = Product & Document;
@Schema()
export class Product {
_id: MongooseSchema.Types.ObjectId;
@Prop({ type: MongooseSchema.Types.String, required: true })
@IsNotEmpty()
sku: string;
@Prop({ type: MongooseSchema.Types.String, required: true })
@IsNotEmpty()
title: string;
@Prop({ type: MongooseSchema.Types.Number, required: true })
@IsNotEmpty()
price: number;
@Prop({ type: MongooseSchema.Types.Number, required: true })
@IsNotEmpty()
stock: number;
@Prop({ type: MongooseSchema.Types.Boolean, required: true })
@IsNotEmpty()
discontinued: boolean;
@Prop({ type: MongooseSchema.Types.Date, required: true })
@IsNotEmpty()
restocked_at: Date;
}
export const ProductSchema = SchemaFactory.createForClass(Product);ตัวอย่างเป็นเอกสารเดียวที่มีค่าครบทุกฟิลด์ ทุกฟิลด์จึงได้ required: true และ @IsNotEmpty() restocked_at เป็นสตริงรูปแบบวันที่จึงมีไทป์เป็น Date และพร็อพเพอร์ตียังใช้ชื่อคีย์ที่เก็บไว้คือ restocked_at
@Prop({ type: MongooseSchema.Types.String, required: true }) และ import Schema ของ Mongoose เป็น MongooseSchema เพื่อไม่ให้ชนกับ decorator Schemanumber, boolean, Date สำหรับสตริงวันที่ ISO 8601 และ string สำหรับที่เหลือทั้งหมด รวมถึงสตริงที่มีแต่ตัวเลขอย่าง "0042"MongooseSchema.Types.Mixed และมีไทป์ Record<string, unknown> อาร์เรย์ของไทป์ธรรมดาชนิดเดียวจะเป็นเช่น [MongooseSchema.Types.String] คู่กับ string[]'home-url': string) คลาสจึงตรงกับเอกสารที่มีอยู่ในคอลเลกชันrequired: true และ @IsNotEmpty() ที่เหลือได้แค่ @IsOptional() คีย์ _id ในอินพุตจะถูกข้ามเพราะประกาศ _id: MongooseSchema.Types.ObjectId ไว้แล้วexport type ProductDocument = Product & Document ไว้สำหรับ inject แบบ @InjectModel(Product.name) model: Model<ProductDocument>ใส่ payload ของ event ที่ทีมอื่นส่งมาให้ หรือ JSON จาก scraper เพื่อให้ได้คลาส schema แล้ว inject ด้วย @InjectModel และเรียก create หรือ find ได้ทันที
export เอกสารสักสองสามตัวที่บันทึกด้วยโมเดล new Schema({...}) เดิม แล้ววางที่นี่ เพื่อเขียนใหม่เป็นคลาสแบบ decorator โดยไม่ทำฟิลด์หล่นหายระหว่างคัดลอกเอง
@IsNotEmpty() และ @IsOptional() อยู่บนคลาส schema แต่ ValidationPipe ตรวจ DTO ของ controller ถ้าจะ validate request ให้ย้าย decorator เหล่านี้ไปที่คลาส DTO หรือสร้าง DTO ขึ้นมา@Schema() ไม่มี option ชื่อคอลเลกชันจึงเป็นไปตามกฎของ Mongoose ที่แปลงชื่อโมเดลเป็นตัวพิมพ์เล็กและพหูพจน์: products ถ้าต้องการชื่อเฉพาะให้ตั้ง @Schema({ collection: 'product' })? (เช่น memo: string) เมื่อเปิด strictNullChecks การเปลี่ยนเป็น memo?: string จะปลอดภัยกว่าMixed ไม่ใช่ schema ที่มีไทป์ ถ้าต้องการ validate ข้างใน ให้นิยามคลาส @Schema() แยกแล้วใช้ @Prop({ type: SubSchema })& Document แทนการ extend Document?เป็นไปตามรูปแบบมาตรฐานในเอกสารของ NestJS: คลาส schema เป็นแค่การนิยามฟิลด์ธรรมดา และใช้ ProductDocument เฉพาะตรงที่ต้องการเมธอดของ Mongoose document อย่าง save() บน Mongoose 7+ ใช้ HydratedDocument<Product> แทนกันได้เลย
ลบบรรทัด import { IsNotEmpty, IsOptional } from 'class-validator' และ decorator สองตัวนั้นบนแต่ละฟิลด์ พฤติกรรมของ schema ถูกควบคุมด้วย required ใน @Prop() อย่างเดียว
type ใน @Prop() ไหม?เมื่อเปิด emitDecoratorMetadata ไทป์ธรรมดาอนุมานได้ แต่จะอนุมานไม่ได้สำหรับอาร์เรย์และ union อย่าง Date | null โค้ดที่สร้างจึงระบุ type ไว้ตั้งแต่แรก เพื่อให้พฤติกรรมเหมือนเดิมแม้ภายหลังจะเปลี่ยนไทป์ TypeScript ของพร็อพเพอร์ตี