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

189
Views
What's the best practice for reusing functions on different models in NestJS?

I have declared some functions for a MongoDB collection Model A, now I want to create another collection Model B and most of the functions in Model A can be reused for Model B. Currently, I fall into a situation where I have to repeat myself recreating these functions just for a different model. What's the best practice to reuse these functions on a different model (How to improve my code)? I am using NestJS framework. Example code that I have:

export class Service {
  constructor (
    @inject('ModelA')
    private modelA: Model<IModelA>,
    @inject('ModelB')
    private modelB: Model<IModelB>,
  ){}
  
 public async findModelA() {
   return await this.modelA.find().exec();
 }

 // How to avoid repeating creating the same functions here?

 public async findModelB() {
   return await this.modelB.find().exect();
 }
}

The repeated functions which are similar to the above example are all over the place. So I really want to get rid of this pain. Thank you in advance for the help!

about 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!