@blaasvaer/frmwrk
Version:
My personal Node framework
51 lines (41 loc) • 1.13 kB
JavaScript
const utils = require('./utils');
/**
* Install Models
*/
function installModels ( config ) {
let models_dir = config.root + '/models/';
return utils.readdirAsync( models_dir )
// .then( utils.sleep( Math.floor( ( Math.random() * 1000 ) + 1000 ) ) ) // DEBUGGING
.then(function( files ) {
const model_files = [];
files.forEach( file => {
// Skip file if name start with an underscore
if ( file.charAt(0) === '_' || file === '.DS_Store' )
return;
let promise = new Promise( function ( resolve, reject ) {
let model = require(models_dir + file);
if ( model ) {
if ( typeof model.install === 'function' ) {
/**
* Add model if it exports install
*/
this.models = {};
this.models[ file.split('.')[0] ] = model;
/**
* Trigger install script for controller
*/
model.install.call();
}
}
if ( model ) {
resolve( file );
} else {
reject( "Loading of file" + file + " failed!" );
}
});
model_files.push( promise );
});
return Promise.all( model_files );
});
}
module.exports = installModels;