UNPKG

sails-util-mvcsloader

Version:

Load models, controllers, services, policies and config from specified directories and inject them into the main Sails app.

37 lines (31 loc) 1.09 kB
/** * Load models from a directory into a Sails app */ var buildDictionary = require('sails-build-dictionary'); module.exports = function (sails, dir, cb) { buildDictionary.optional({ dirname: dir, filter: /^([^.]+)\.(js|coffee|litcoffee)$/, replaceExpr: /^.*\//, flattenDirectories: true }, function (err, models) { if (err) { return cb(err); } // Get any supplemental files buildDictionary.optional({ dirname: dir, filter: /(.+)\.attributes.json$/, replaceExpr: /^.*\//, flattenDirectories: true }, function (err, supplements) { if (err) { return cb(err); } var finalModels = sails.util.merge(models, supplements); sails.hooks.orm.models = sails.util.merge(finalModels || {}, sails.hooks.orm.models || {}); sails.models = sails.util.merge(finalModels || {}, sails.models || {}); cb(); }); }); };