Typescript 中的猫鼬静态模型定义 [英] Mongoose static Model definitions in Typescript

查看:62
本文介绍了Typescript 中的猫鼬静态模型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Mongoose Schema 并为模型添加了一些静态方法,命名为 Campaign.

I created a Mongoose Schema and added some static methods for the Model, named Campaign.

如果我在 console.log 活动中我可以看到它上面的方法.问题是我不知道在哪里添加这些方法,以便 Typescript 也知道它们.

If I console.log Campaign I can see the methods present on it. The problem is I don't know where to add those methods so that Typescript is also aware of them.

如果我将它们添加到我的 CampaignModelInterface,它们仅可用于模型的实例(或至少 TS 认为它们是).

If I add them to my CampaignModelInterface, they are only available for instances of the model (or at least TS thinks they are).

campaignSchema.ts

  export interface CampaignModelInterface extends CampaignInterface, Document {
      // will only show on model instance
  }

  export const CampaignSchema = new Schema({
      title: { type: String, required: true },
      titleId: { type: String, required: true }
      ...etc
  )}

  CampaignSchema.statics.getLiveCampaigns = Promise.method(function (){
      const now: Date = new Date()
      return this.find({
           $and: [{startDate: {$lte: now} }, {endDate: {$gte: now} }]
      }).exec()
  })

  const Campaign = mongoose.model<CampaignModelInterface>('Campaign', CampaignSchema)
  export default Campaign

我也尝试通过 Campaign.schema.statics 访问它,但没有成功.

I also tried accessing it via Campaign.schema.statics, but without luck.

谁能建议如何让 TS 知道模型上存在的方法,而不是模型实例?

Can anyone advise how to let TS know about the methods present on the Model, not the Model instances?

推荐答案

我在这里上回答了一个非常相似的问题,虽然我会回答你的(主要是我其他答案的第三部分),因为你提供了一个不同的模式.Mongoose 类型有一个有用的自述文件,它相当隐蔽,但有一个部分关于 静态方法.

I answered a very similar question over here, although I'll answer yours (mostly with the third section of my other answer) as you've provided a different schema. There is a helpful readme with the Mongoose typings which is fairly hidden away, but there's a section on static methods.

您描述的行为完全正常 - Typescript 被告知 Schema(描述单个文档的对象)具有名为 getLiveCampaigns 的方法.

The behaviour that you described is perfectly normal - Typescript is being told that the Schema (the object which describes individual documents) has the method called getLiveCampaigns.

相反,您需要告诉 Typescript 该方法在模型上,而不是在架构上.完成后,您可以按照普通 Mongoose 方法访问静态方法.您可以通过以下方式做到这一点:

Instead, you need to tell Typescript that the method is on the model, not the schema. Once done, you can access static methods as per the normal Mongoose method. You can do that by the following:

// CampaignDocumentInterface should contain your schema interface,
// and should extend Document from mongoose.
export interface CampaignInterface extends CampaignDocumentInterface {
    // declare any instance methods here
}

// Model is from mongoose.Model
interface CampaignModelInterface extends Model<CampaignInterface> {
    // declare any static methods here
    getLiveCampaigns(): any; // this should be changed to the correct return type if possible.
}

export const CampaignSchema = new Schema({
    title: { type: String, required: true },
    titleId: { type: String, required: true }
    // ...etc
)}

CampaignSchema.statics.getLiveCampaigns = Promise.method(function (){
    const now: Date = new Date()
    return this.find({
        $and: [{startDate: {$lte: now} }, {endDate: {$gte: now} }]
    }).exec()
})

// Note the type on the variable, and the two type arguments (instead of one).
const Campaign: CampaignModelInterface = mongoose.model<CampaignInterface, CampaignModelInterface>('Campaign', CampaignSchema)
export default Campaign

这篇关于Typescript 中的猫鼬静态模型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆