JSON को NestJS @nestjs/mongoose schema class में बदलता है: हर field पर @Prop() वाली @Schema() class, SchemaFactory.createForClass() से बना schema, और ProductDocument type, सब एक फ़ाइल में। हर field पर class-validator का @IsNotEmpty() या @IsOptional() भी होता है।
इसे तब इस्तेमाल करें जब किसी NestJS module को MongooseModule.forFeature() में register करने के लिए schema चाहिए, store होने वाले document का sample आपके पास है, और हर decorator हाथ से लिखना धीमा काम है।
मॉडल का नाम में class का नाम डालें (default MyModel)। product टाइप करने पर Product, ProductDocument और ProductSchema बनते हैं।product.schema.ts के रूप में सेव करें और MongooseModule.forFeature([{ name: Product.name, schema: ProductSchema }]) से register करें।{
"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);Sample एक अकेला, पूरी तरह भरा document है, इसलिए हर field को required: true और @IsNotEmpty() मिलता है। restocked_at तारीख के format वाली string है, इसलिए उसका type Date है, और property stored key नाम restocked_at ही रखती है।
@Prop({ type: MongooseSchema.Types.String, required: true })। Mongoose का Schema MongooseSchema के रूप में import होता है, ताकि Schema decorator से टकराव न हो।number, boolean, ISO 8601 date strings के लिए Date, और बाकी सबके लिए string, "0042" जैसी सिर्फ़ अंकों वाली strings समेत।MongooseSchema.Types.Mixed से declare होते हैं और उनका type Record<string, unknown> होता है; एक सादे type के arrays string[] के साथ [MongooseSchema.Types.String] जैसे बनते हैं।'home-url': string), इसलिए class collection में पहले से मौजूद documents से मेल खाती है।required: true और @IsNotEmpty() मिलता है; बाकी को सिर्फ़ @IsOptional()। इनपुट की _id key छोड़ दी जाती है, क्योंकि _id: MongooseSchema.Types.ObjectId पहले से declared है।@InjectModel(Product.name) model: Model<ProductDocument> जैसे injection के लिए export type ProductDocument = Product & Document बनता है।दूसरी टीम का भेजा event payload या scraper का JSON डालें, schema class पाएं, फिर उसे @InjectModel से inject करके तुरंत create या find call करें।
पुराने new Schema({...}) model से सेव हुए कुछ documents export करके यहां पेस्ट करें, ताकि हाथ से कॉपी करते समय fields छोड़े बिना उसे decorator-आधारित class के रूप में दोबारा लिखा जा सके।
@IsNotEmpty() और @IsOptional() schema class पर हैं, लेकिन ValidationPipe controller DTOs को validate करता है। Requests validate करने के लिए इन decorators को DTO class में ले जाएं या एक नई बनाएं।@Schema() में कोई options नहीं हैं, इसलिए collection का नाम model नाम को lowercase और बहुवचन करने के Mongoose नियम से तय होता है: products। खास नाम चाहिए तो @Schema({ collection: 'product' }) सेट करें।? के बिना आते हैं (जैसे memo: string)। strictNullChecks के साथ इन्हें memo?: string में बदलना ज़्यादा सुरक्षित है।Mixed के रूप में store होते हैं। उनके अंदर validation के लिए अलग @Schema() class define करें और @Prop({ type: SubSchema }) इस्तेमाल करें।Document को extend करने की बजाय & Document क्यों?यह NestJS docs के default pattern का पालन करता है: schema class सादी field definition रहती है और ProductDocument सिर्फ़ वहीं इस्तेमाल होता है जहां save() जैसे Mongoose document methods चाहिए। Mongoose 7+ पर HydratedDocument<Product> सीधा विकल्प है।
import { IsNotEmpty, IsOptional } from 'class-validator' line और हर field पर लगे दोनों decorators हटा दें। Schema का व्यवहार सिर्फ़ @Prop() के required से नियंत्रित होता है।
@Prop() में type बताना ज़रूरी है?emitDecoratorMetadata के साथ सादे types अनुमानित हो सकते हैं, लेकिन arrays और Date | null जैसे unions के लिए inference विफल होता है। बना हुआ कोड type पहले ही बता देता है, ताकि बाद में property का TypeScript type बदलने पर भी व्यवहार वही रहे।