Performance: Schema definition
Schema definitions impact server response time and data validation speed, which indirectly affect frontend load and interaction times.
import { Schema } from 'mongoose'; const UserSchema = new Schema({ name: { type: String, required: true }, age: { type: Number, min: 0, max: 120 }, address: { street: { type: String }, city: { type: String }, zip: { type: String } } });
import { Schema } from 'mongoose'; const UserSchema = new Schema({ name: { type: String, required: true }, age: { type: Number }, address: { type: Object }, extra: { type: Schema.Types.Mixed } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Loose schema with Mixed/Object types | N/A (server-side) | N/A | N/A | [X] Bad |
| Strict schema with explicit nested types | N/A (server-side) | N/A | N/A | [OK] Good |
@Schema() in NestJS?@Schema()@Schema() is used to define how data should be structured and validated in NestJS models.title in a NestJS schema?title should be of type string to hold text data.@Prop() title: string; which declares a string property.@Schema()
export class Book {
@Prop()
author: string;
@Prop({ required: true })
pages: number;
}pages?required: true optionpages property is marked as required, so it must be provided when saving.pages causes validation to fail and the save operation to be rejected.pages is required -> Option D@Schema()
export class User {
@Prop({ type: String })
age: number;
}type: String but the property is declared as number.Product with a required name (string), optional price (number), and a default inStock (boolean) set to true?name as string@Prop({ required: true }) name: string; to make name mandatory.price as number@Prop() price?: number; to allow price to be optional.inStock with default true@Prop({ default: true }) inStock: boolean; to set default value.Pass the test and this pattern moves unknown