I’d answered a question on StackOverflow about where to put “models” in a NodeJS project. I wanted to elaborate on the simple auto loader I use to load a folder full of models (and I use this pattern other places as well). Normally, I create a folder called models:
Inside the Models folder, I have a file called, models.js.
Inside another module (like app.js), I’ve got a line of code that looks like this:
var models = require('./models').initialize(app, services);
While the require (reference) by default looks for a file called index.js in the folder models, I’ve in this case added a simple package.json file with an override:
{ "main": "./models.js" }
The reason I do this is that in a tabbed source code editor, having several index.js files can be confusing to see. So, this allows me to name the file something that is more memorable and understandable at a glance.
Inside of the models.js class, I’ve written code to automatically load all of the models (and call an initialize function once for each module):
var initialize = function(app, services) { var models = {}; var currentFile = path.basename(__filename); // just file name var modelFiles = fs.readdirSync(__dirname); // loop through all of the files in the current directory for(var i= 0, len = modelFiles.length; i < len; i++) { // ignore this file (via global NodeJS variable) if(modelFiles[i] === currentFile || path.extname(modelFiles[i]).toLowerCase() !== '.js') { continue; // skip the current file and anything without a "JS" extension } // require it var model = require(path.join(__dirname, modelFiles[i])); // call its intialize model.initialize(services, models); } exports.models = models; return models; }; exports.initialize = initialize;
The logic is simple enough – grab all of the files in the current path, then loop through each, filtering the current file and any that don’t end with “js”. This logic could be adjusted of course to reflect other coding styles and requirements.
Finally, each module is loaded and then an initialize method is called (admittedly, I could make it more robust by checking for the existence of the initialize function before calling it (but in this case, I know each module should be initialized in a particular way).