Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Need to set correct type for mongoose document

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())

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!