title: Store models in folder, use index.js to require them all
body: <p>I have a decent size project and I need to do a little restructuring.</p>
  
  <p>I am using mongoose as my ORM for node.  I would like to put all my mongoose models in a folder called 'models'.  I have read that when I do that I can put an index.js file in the models folder such that is will pull in all models and store them.</p>
  
  <p>app.js:</p>
  
  <pre><code>...
  var mongoose = require('mongoose');
  var models = require('./models')(mongoose);
  
  app.configure(function () {
    mongoose.connect(dbPath, function(err) {
      if (err) throw err;
    });
    ...
  });
  
  // include models in my routes so I need access
  ...
  </code></pre>
  
  <p>I am getting stuck on exactly what I need to do in the index.js to return all my models</p>
  
  <p>index.js (this is what I have tried, not even close)</p>
  
  <pre><code>function Models(mongoose) {
      var Counters = require('./counters')(mongoose);
      var User = require('./user')(mongoose);
      var Token = require('./token')(mongoose);
      var Team = require('./team')(mongoose);
      var Role =  require('./role')();
      var Layer = require('./layer')(mongoose, counters);
      var Feature = require('./feature')(mongoose, counters, async);
  
  
  }
  
  module.exports = Models;
  </code></pre>
  
  <p>Also should I pass in mongoose from the app.js since I needed there to connect to mongo?  I.E. I can require it again in the index.js but I am not sure if requiring the same module in different files is bad practice.</p>
  
  <p>EDIT: (here is on of my models)</p>
  
  <p>Sorry forgot to mention that I add 'accessor' type functions in my model classes.  I.E. I would like to present a public interface for each model.</p>
  
  <p>user.js:</p>
  
  <pre><code>module.exports = function(mongoose) {
  
    // Creates a new Mongoose Schema object
    var Schema = mongoose.Schema; 
  
    // Collection to hold users
    var UserSchema = new Schema({
        username: { type: String, required: true },
        password: { type: String, required: true },
      },{ 
        versionKey: false 
      }
    );
  
    // Creates the Model for the User Schema
    var User = mongoose.model('User', UserSchema);
  
    var getUserById = function(id, callback) {
      User.findById(id, callback);
    }
  
    var getUserByUsername = function(username, callback) {
      var query = {username: username};
      User.findOne(query, callback);
    }
  
  
    return {
      getUserById: getUserById,
      getUserByUsername: getUserByUsername
    }
  } 
  </code></pre>