-
Notifications
You must be signed in to change notification settings - Fork 2
/
initialize-waterline.js
95 lines (83 loc) · 2.8 KB
/
initialize-waterline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Modules for working with Waterline
var ormHook = require('sails-hook-orm');
var _ = require('@sailshq/lodash');
var path = require('path');
var fs = require('fs');
module.exports = function(callback) {
global.sails = global.sails || {};
defaultsDeep(sails, {
config: {
appPath: path.resolve(__dirname),
globals: {
adapters: true,
models: true
},
models: <%= modelsConfig %>,
datastores: <%= datastoresConfig %>,
orm: {
skipProductionWarnings: false,
moduleDefinitions: {
models: {},
}
},
},
modules: {
loadModels: (cb) => {
let modelFiles = fs.readdirSync(path.resolve('.', 'api', 'models'));
let modelFileNames = _.reduce(modelFiles, (memo, fileName) => {
let match = fileName.match(/^(\w+)\.js/);
if (match && match[1]) {
memo.push(match[1]);
}
return memo;
}, []);
return cb(null, _.reduce(modelFileNames, (memo, modelName) => {
let model = require('./api/models/' + modelName);
model.globalId = modelName;
memo[modelName.toLowerCase()] = model;
return memo;
}, {}));
},
loadAdapters: (cb) => cb(null, {})
},
log: {
info: console.log,
debug: console.log,
warn: console.log,
error: console.log,
verbose: process.env.sails_log__level==='verbose' || process.env.sails_log__level==='silly' ? console.log : () => {},
silly: process.env.sails_log__level==='silly' ? console.log : () => {},
blank: () => {}
},
on: () => {},
once: () => {}
});
// If the ORM hook has adapters and they have datastores already, bail. This probably means that
// a previous run did not tear down properly (probably because it timed out).
if (_.any(_.get(sails, 'hook.orm.adapters') || {}, (adapter, adapterName) => {
return _.keys(adapter.datastores || {}).length > 0;
})) {
return;
}
// Load the sails ORM hook.
_.set(sails, 'hooks.orm', ormHook(sails));
// Apply sails-hook-orm defaults to sails confog.
var defaults = (_.isFunction(sails.hooks.orm.defaults) ?
sails.hooks.orm.defaults(sails.config) :
sails.hooks.orm.defaults) || {};
defaultsDeep(sails.config, defaults);
// Run the hook's "configure" function.
sails.hooks.orm.configure();
// Run the hook's "initialize" function.
sails.hooks.orm.initialize(callback);
// Save the hook's models on `sails.models`.
sails.models = sails.hooks.orm.models;
return;
};
const defaultsDeep = _.partialRight(_.merge, function recursiveDefaults (dest,src) {
// Ensure dates and arrays are not recursively merged
if (_.isArray(arguments[0]) || _.isDate(arguments[0])) {
return arguments[0];
}
return _.merge(dest, src, recursiveDefaults);
});