Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda