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

119
Vistas
What is the best method (if there is one) to import a set of functions in a module?

I am trying to solve this:

I have a module, let's say "users", for which I have a service with basic database operations (find, findMany, save etc).

I want to create more modules which all have the same basic database operations but I don't want to have to write them all again

To make things easy, I have these 2 functions in an utils file:

export async function find<T extends Model>(model: T, query, options?): Promise<T> {
    return model.find(query)
}

and

export async function findOne<T extends Model>(model: T, query, options?): Promise<T> {
    return model.findOne(query)
}

How can I make it so I import each of this functions to a class (using TypeScript decorators or other TS magic)

I want to have something like this

import { attachAllBasicDatabaseMethods } from "./model-utils/operations"
export class UserController {
    @attachAllBasicDatabaseMethods()
    private constructor() {}
}

so in my controller i can use a method like this:

async getSomeUsers() {
    const users = await this.findMany(UserModel, this.query, this.options);
    return users;
}

The decorator idea is just something I have thought of, but I know there are some other methods of solving this

Or better: using that decorator with the model as the parameter automatically binds the model to the methods, so I can use this.find(this.query, this.options)

about 4 years ago · Juan Pablo Isaza
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