且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用@nestjs/mongoose 时如何在文档界面中定义静态 mongoose 方法?

更新时间:2023-02-15 17:02:55

除了 IUser,你可能想要一个额外的接口 IUserModel 并从 IUserModel 扩展它代码>模型.示例代码段可能如下所示:

In addition to IUser, you might want to have an extra interface IUserModel and extends it from Model<T>. Sample snippet might look as follow:

export interface IUserModel extends Model<IUser> {
     // Model is imported from mongoose
     // you can put your statics methods here
     someAction: () => string;
}

然后,无论您在何处使用 @InjectModel() 注入模型,都可以键入 IUserModel 类型的注入.

Then in wherever you're injecting the Model using @InjectModel(), you can type your injection of type IUserModel.

constructor(@InjectModel('UserModel') private readonly userModel: IUserModel) {}

现在您的 this.userModel 将可以访问 someAction() 方法.

Now your this.userModel will have access to someAction() method.

快乐编码!