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

132
Views
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
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!