I'm currently learning NestJS and Mongoose. While looking for the answer to my other question, I've came up with this post How to write down nested schemas for mongoose using NestJS nomenclature
So in the answer, it has the following piece of codes:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type CatDocument = Cat & Document;
@Schema()
export class Owners {
@Prop()
names: [string];
}
@Schema()
export class Cat {
@Prop()
name: string;
@Prop()
age: number;
@Prop()
breed: string;
@Prop()
owners: Owners;//schema for owner
}
export const CatSchema = SchemaFactory.createForClass(Cat);
From my understanding, if we don't use SchemaFactory.createForClass() function for the created schema (Owners in the above example), NestJS won't generate a Mongoose Schema for us.
So I want to know what's the use of the Schema decorator in the above code? Should we just write something like this
export class Owners {
names: [string];
}
And then define it in the Cat class like this
@Prop(type: Owners)
owners: Owners;