猫鼬鉴别器无法访问静态方法和基本模型

时间:2021-02-03 10:56:16

标签: node.js mongodb mongoose

我对猫鼬很陌生。 使用 mongoose 5.11.14 我有一个基本的用户模式,我想用鉴别器扩展它。基本用户定义如下:

export type IBasicUser = mongoose.Document & IBasicUserProp;
const userSchema = new Schema<IBasicUser, IBasicUserModel>({
  ...
});

userSchema.statics.getSignKey = async function (id: string): Promise<string> {
  ...
};

export interface IBasicUserModel extends mongoose.Model<IBasicUser> {
  getSignKey(id: string): Promise<string>;
}

export const BasicUser = mongoose.model('User', userSchema);

这按预期工作。现在我想通过鉴别器扩展模式。我尝试了多种方法:

export type IUser = mongoose.Document & IUserProp & IBasicUserProp;

const userSchema = new Schema<IUser, mongoose.Model<any>>({
  ...
}, { timestamps: true, toObject: { virtuals: true }, toJSON: { virtuals: true } });

export const User: mongoose.Model<IUser> = BasicUser.discriminator('UserExtended', userSchema);

这有效,但静态方法 getSignKey() 不存在(Typescript 错误)。

我也试过如下:

export type IUser = mongoose.Document & IUserProp & IBasicUserProp;

const userSchema = new Schema<IUser, IUserModel>({
  ...
}, { timestamps: true, toObject: { virtuals: true }, toJSON: { virtuals: true } });

export interface IUserModel extends mongoose.Model<IUser> {
  getSignKey(id: string): Promise<string>;
}

export const User: mongoose.Model<IUser> = BasicUser.discriminator('UserExtended', userSchema);

这样我得到以下 TS 错误:

error TS2345: Argument of type 'Schema<IUser, IUserModel>' is not assignable to parameter of type 'Schema<Document<any>, Model<Document<any>>>'.
  Type 'IUserModel' is not assignable to type 'Model<Document<any>>'.
    The types returned by 'createCollection(...)' are incompatible between these types.
      Type 'Promise<Collection<IUser>>' is not assignable to type 'Promise<Collection<Document<any>>>'.
        Type 'Collection<IUser>' is not assignable to type 'Collection<Document<any>>'.
          Types of property 'bulkWrite' are incompatible.
            Type '{ (operations: BulkWriteOperation<IUser>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<...>): void; }' is not assignable to type '{ (operations: BulkWriteOperation<Document<any>>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<...>): void; }'.
              Types of parameters 'operations' and 'operations' are incompatible.
                Type 'BulkWriteOperation<Document<any>>[]' is not assignable to type 'BulkWriteOperation<IUser>[]'.
                  Type 'BulkWriteOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<IUser>'.
                    Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<IUser>'.
                      Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteInsertOneOperation<IUser>'.
                        The types of 'insertOne.document' are incompatible between these types.
                          Type 'Pick<Document<any>, "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | ... 40 more ... | "validateSync"> & { ...; }' is not assignable to type 'Pick<IUser, "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | "$where" | ... 75 more ... | "sikoQualification"> & { ...; }'.
                            Type 'Pick<Document<any>, "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | ... 40 more ... | "validateSync"> & { ...; }' is missing the following properties from type 'Pick<IUser, "populate" | "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | "$where" | "baseModelName" | ... 74 more ... | "sikoQualification">': buildingRights, globalRights, username, email, and 26 more.

export const User: mongoose.Model<IUser> = BasicUser.discriminator('UserExtended', userSchema);

如何让鉴别器工作并访问静态方法?

0 个答案:

没有答案