# Model Decorator

Module
import { Model } from "@tsed/mongoose"
Source/packages/mongoose/src/decorators/model.ts

# Overview

function Model(options?: MongooseModelOptions): (target: any) => void;

# Description

Define a class as a Mongoose Model. The model can be injected to the Service, Controller, Middleware, Converters or Filter with @Inject annotation.

# Example

@Model()
export class EventModel {
  @Property()
  field: string;
}
1
2
3
4
5

Then inject the model into a service:

class MyService {
   constructor(@Inject(EventModel) eventModel: MongooseModel<EventModel>) {
       eventModel.find().exec();
   }
}
1
2
3
4
5

# Options

  • schemaOptions (mongoose.SchemaOptions): Option to configure the schema behavior.
  • name (String): model name.
  • collection (String): collection (optional, induced from model name).
  • skipInit (Boolean): skipInit whether to skip initialization (defaults to false).