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

187
Views
Best practice to create basics models in NodeJS

I'm working on a NodeJS API project. I have a lot of models (users, projects, team, ...) and for each of this model I have:

  • Models methods (getters and setters)
  • DB requests (I use Couchdb)
  • Schema validation

My first question is:

Should I create multiple modules for DB request and Class methods (in order to separate domain layer from persistance layer)?

My second question is: "How should I manage the creation of a model?"
Multiple cases:

case 1 (init constructor with Id):

var user = new User({id: 1}); 
user.fetch(function(err, user) {
   user.get('name'); // George
});

case 2 (No constructor method):

user.fetchById({id: 1}, function(err, user) { 
   user.get('name'); // George
}

case3 (Differents modules DB and domain layer):

userDB.fetchById({id: 1}, function(err, data) { 
   var user = new User(data);
   user.get('name'); // George
}

Or maybe there is other best practice? Note: I often see people making modules that represents models and that only expose DB requests.

Best regards.

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!