Skip to content

Commit 4a55bb6

Browse files
committed
Seems to have made hook work with waterline
Closes #31
2 parents 54608f1 + 8f98c87 commit 4a55bb6

File tree

6 files changed

+2463
-1360
lines changed

6 files changed

+2463
-1360
lines changed

index.js

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module.exports = sails => {
22
const Sequelize = require('sequelize');
33

4+
// keep a ref to the original sails model loader function
5+
const originalLoadModels = sails.modules.loadModels;
6+
47
return {
58
defaults: {
69
__configKey__: {
@@ -14,11 +17,52 @@ module.exports = sails => {
1417
if (typeof cls === 'string' && cls !== '') {
1518
Sequelize.useCLS(require('continuation-local-storage').createNamespace(cls));
1619
}
20+
21+
if (sails.config[this.configKey].exposeToGlobal) {
22+
sails.log.verbose('Exposing Sequelize globally');
23+
global['Sequelize'] = Sequelize;
24+
}
25+
26+
// Override sails internal loadModels function
27+
// needs to be done in configure()
28+
sails.modules.loadModels = function load (cb) {
29+
30+
// call the original sails loadModels function so we have access to it's returned models
31+
originalLoadModels((err, modelDefs) => {
32+
// modelDefs = all the model files from models directory - sails does this
33+
// now modify / return own models for sails to boot
34+
const models = {};
35+
36+
sails.log.verbose('Detecting Waterline models');
37+
Object.entries(modelDefs).forEach((entry) => {
38+
const [key, model] = entry;
39+
40+
if (typeof (model.options) === 'undefined' || typeof (model.options.tableName) === 'undefined') {
41+
sails.log.verbose('Loading Waterline model \'' + model.globalId + '\'');
42+
models[key] = model;
43+
}
44+
});
45+
46+
// return the models that the sails orm hook will bootstrap
47+
cb(err, models);
48+
});
49+
};
1750
},
1851
initialize (next) {
19-
this.initAdapters();
20-
this.initModels();
21-
this.reload(next);
52+
53+
if (sails.config.hooks.orm === false) {
54+
this.initAdapters();
55+
this.initModels();
56+
this.reload(next);
57+
} else {
58+
sails.on('hook:orm:loaded', () => {
59+
60+
this.initAdapters();
61+
this.initModels();
62+
this.reload(next);
63+
64+
});
65+
}
2266
},
2367

2468
reload (next) {
@@ -28,12 +72,11 @@ module.exports = sails => {
2872
connections = this.initConnections();
2973

3074
if (sails.config[this.configKey].exposeToGlobal) {
31-
sails.log.verbose('Exposing Sequelize and Sequelize connections globally');
32-
global['Sequelize'] = Sequelize;
75+
sails.log.verbose('Exposing Sequelize connections globally');
3376
global['SequelizeConnections'] = connections;
3477
}
3578

36-
return sails.modules.loadModels((err, models) => {
79+
return originalLoadModels((err, models) => {
3780

3881
if (err) {
3982
return next(err);
@@ -118,7 +161,7 @@ module.exports = sails => {
118161
continue;
119162
}
120163

121-
sails.log.verbose('Loading model \'' + modelDef.globalId + '\'');
164+
sails.log.verbose('Loading Sequelize model \'' + modelDef.globalId + '\'');
122165
connectionName = modelDef.connection || modelDef.datastore || defaultConnection;
123166
modelClass = connections[connectionName].define(modelDef.globalId, modelDef.attributes, modelDef.options);
124167

@@ -172,7 +215,7 @@ module.exports = sails => {
172215
},
173216

174217
migrateSchema (next, connections, models) {
175-
let connectionDescription, connectionName, migrate, forceSync;
218+
let connectionDescription, connectionName, migrate, forceSyncFlag, alterFlag;
176219
const syncTasks = [];
177220

178221
// Try to read settings from old Sails then from the new.
@@ -186,7 +229,19 @@ module.exports = sails => {
186229
if (migrate === 'safe') {
187230
return next();
188231
} else {
189-
forceSync = migrate === 'drop';
232+
switch (migrate) {
233+
case 'drop':
234+
forceSyncFlag = true;
235+
alterFlag = false;
236+
break;
237+
case 'alter':
238+
forceSyncFlag = false;
239+
alterFlag = true;
240+
break;
241+
default:
242+
forceSyncFlag = false;
243+
alterFlag = false;
244+
}
190245

191246
for (connectionName in datastores) {
192247
connectionDescription = datastores[connectionName];
@@ -213,11 +268,11 @@ module.exports = sails => {
213268
}
214269
}
215270

216-
return connections[connectionName].sync({ force: forceSync });
271+
return connections[connectionName].sync({ force: forceSyncFlag, alter: alterFlag });
217272
}));
218273

219274
} else {
220-
syncTasks.push(connections[connectionName].sync({ force: forceSync }));
275+
syncTasks.push(connections[connectionName].sync({ force: forceSyncFlag, alter: alterFlag }));
221276
}
222277
}
223278

0 commit comments

Comments
 (0)