title: NodeJS: how to implement repository pattern
body: <p>I would like to implement the Repository pattern in my NodeJS app, but I'm running into troubles with circular requires (I guess...).</p>
  
  <p>How I'm trying to implement it:</p>
  
  <ul>
  <li>PersonRepository class with methods: getAll, getById, create, update, delete</li>
  <li>Person class with methods: init, createAccount, showRelations, addRelation,</li>
  </ul>
  
  <p><strong>First of all: Is my repository pattern design correct?</strong> </p>
  
  <p>My classes:</p>
  
  <p><strong>personRepository.js</strong></p>
  
  <pre><code>const PersonModel = require('./model');
  const Person = require('./person');
  
  class PersonRepository {
  
      constructor() {
          this._persons = new Set();
      }
  
      getAll( cb ) { // To Do: convert to promise
          let results = new Set();
  
          PersonModel.find({}, 'firstName lastName', (err, people) =&gt; {
              if (err) { 
                  console.error(err);
              }
              people.forEach((person, index) =&gt; {
                  let foundPerson = new Person(person._id.toString(), person.firstName, person.lastName, person.email, person.birthday);
                  results.add(foundPerson);
              });
              this._persons = results;
              if (cb) cb(this._persons);
          });
      }
  
      getById(id) {
  
          return PersonModel.findOne({ _id: id });
  
      }
  
      getByEmail(email) {
          throw new Error("Method not implemented");
      }
  
      create( person ) {
          throw new Error("Method not implemented");
      }
  
      update ( person ) {
          throw new Error("Method not implemented");
      }
  
      delete ( person ) {
          throw new Error("Method not implemented");
      }
  }
  
  module.exports = new PersonRepository();
  </code></pre>
  
  <p><strong>person.js</strong></p>
  
  <pre><code>const PersonModel = require('./model');
  const personRepository = require('./personRepository');
  
  class Person {
  
      constructor(personId, first, last, email, birthday) {
          this._id = personId ? personId : undefined;
          this._firstName = first ? first : undefined;
          this._lastName = last ? last : undefined;
          this._email = email ? email : undefined;
          this._birthday = birthday ? new Date(birthday) : undefined;
          this._relations = new Map();
      }
      init() { // Get all data from database
          personRepository.getById(this._id)
              .then(console.log)
              .catch(console.error);
      }
  
  }
  
  module.exports = Person;
  </code></pre>
  
  <p><strong>tests.js</strong></p>
  
  <pre><code>console.log("--- GET ALL : results--- ");
  personRepository.getAll( (persons) =&gt; {
      for (let person of persons) {
          person.loadAllData()
              .then(() =&gt; {
                  console.log(person);
              })
              .catch((e) =&gt; {
                  console.log(e);
              });
      }
  
  });
  console.log("--- INIT : results--- ");
  var personInit = new Person("59c18a9029ef510012312995");
  console.log("before init");
  console.log(personInit);
  personInit.init();
  console.log("after init");
  console.log(personInit);
  </code></pre>
  
  <p><strong>Problem:</strong>
  When running the "Get all" test (without the INIT tests), it works.
  When I add the INIT tests, I get the error:</p>
  
  <pre><code>personRepository.getById(this._id)
                           ^
  
  TypeError: personRepository.getById is not a function
      at Person.init
  </code></pre>
  
  <p>How can I prevent this from happening?
   - Change the way I require my modules?
   - Change my design? (eg. don't require Person class in personRepository and just create a Set of ids in "getAll" instead of a Set of persons)
   - Other ideas?</p>
  
  <p>Thanks for helping me! I'm trying to solve this for hours now...</p>