Need to set correct type for mongoose document

328 views Asked by At

I have an abstract class that takes a mongoose model and does some manipulation.

export default class ItemManager {
  private _req: Request;
  private _res: Response;
  private _dbModel: mongoose.Model<any> | any; // **can't set correct type here**

  constructor(req: Request, res: Response, dbModel: any) {
    this._req = req;
    this._res = res;
    this._dbModel = dbModel;
  }

  public getItem(condition: object) {
    const promise: mongoose.DocumentQuery<any, any, {}> = this._dbModel.findOne(condition)
    return promise.then(async(item: any) => {return ...}).catch((err) => ...)
  }
}

UserModel

class UserSchema extends Typegoose {
  @prop()
  firstName: string;
  ...
  @staticMethod
  public static myMethod ....
}

export const UserModel = new UserSchema().getModelForClass(UserSchema, {
  schemaOptions: {
    collection: 'users'
  }
});

Usage:

import { UserModel } from '';
import ItemManager from '';
const UserManager = (req: Request, res: Response) => new ItemManager(req, res, UserModel);

router.post('/', (req: Request, res: Response) => UserManager(req, res).getItem({ my_condition }));

Please tell me how to correctly specify the type for _dbModel so that requests are processed correctly, and it is possible to work with the obtained abstract results (like: .then(item) => item.prop = 1) inside the ItemManager methods, and also use your own static methods there (this._dbModel.myMethod())

0

There are 0 answers