implement typegoose, model definitions using decorators
bellaabdelouahab opened this issue · comments
Consider using Typegoose for a better support between TypeScript and Mongoose
@muttaqin1 remind me why this is important ?
Typegoose is used to easily define mongoose models. As we have already defined some models, do we need this now?
It would be better if we have used it earlier. what do you think about this?
Mongoose
const UserModel = mongoose.model('User', {
name: { type: String },
age: { type: Number, required: true },
preferences: [{ type: String }],
mainJob: { type: JobSchema },
jobs: [{ type: JobSchema }],
mainCar: { type: Schema.Types.ObjectId, ref: 'Car' },
cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }],
});
Typegoose
class User {
@prop()
public name?: string;
@prop({ required: true })
public age!: number; // This is a single Primitive
@prop({ type: () => [String] })
public preferences?: string[]; // This is a Primitive Array
@prop()
public mainJob?: Job; // This is a single SubDocument
@prop({ type: () => [Job] })
public jobs?: Job[]; // This is a SubDocument Array
@prop({ ref: () => Car })
public mainCar?: Ref<Car>; // This is a single Reference
@prop({ ref: () => [Car] })
public cars?: Ref<Car>[]; // This is a Reference Array
}