forked from KSDaemon/sails-hook-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
97 lines (85 loc) · 3.18 KB
/
index.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
96
97
module.exports = function(sails) {
global['Sequelize'] = require('sequelize');
Sequelize.cls = require('continuation-local-storage').createNamespace('sails-sequelize-postgresql');
return {
initialize: function(next) {
this.initAdapters();
this.initModels();
this.reload(next);
},
reload: function(next) {
var hook = this;
var connection, migrate, sequelize;
sails.log.verbose('Using connection named ' + sails.config.models.connection);
connection = sails.config.connections[sails.config.models.connection];
if (connection == null) {
throw new Error('Connection \'' + sails.config.models.connection + '\' not found in config/connections');
}
if (connection.options == null) {
connection.options = {};
}
connection.options.logging = connection.options.logging || sails.log.verbose; //A function that gets executed everytime Sequelize would log something.
migrate = sails.config.models.migrate;
sails.log.verbose('Migration: ' + migrate);
if (connection.url) {
sequelize = new Sequelize(connection.url, connection.options);
} else {
sequelize = new Sequelize(connection.database, connection.user, connection.password, connection.options);
}
global['sequelize'] = sequelize;
return sails.modules.loadModels(function(err, models) {
var modelDef, modelName, ref;
if (err != null) {
return next(err);
}
for (modelName in models) {
modelDef = models[modelName];
sails.log.verbose('Loading model \'' + modelDef.globalId + '\'');
global[modelDef.globalId] = sequelize.define(modelDef.globalId, modelDef.attributes, modelDef.options);
sails.models[modelDef.globalId.toLowerCase()] = global[modelDef.globalId];
}
for (modelName in models) {
modelDef = models[modelName];
hook.setAssociation(modelDef);
hook.setDefaultScope(modelDef);
}
if(migrate === 'safe') {
return next();
} else {
var forceSync = migrate === 'drop';
sequelize.sync({ force: forceSync }).then(function() {
return next();
});
}
});
},
initAdapters: function() {
if(sails.adapters === undefined) {
sails.adapters = {};
}
},
initModels: function() {
if(sails.models === undefined) {
sails.models = {};
}
},
setAssociation: function(modelDef) {
if (modelDef.associations != null) {
sails.log.verbose('Loading associations for \'' + modelDef.globalId + '\'');
if (typeof modelDef.associations === 'function') {
modelDef.associations(modelDef);
}
}
},
setDefaultScope: function(modelDef) {
if (modelDef.defaultScope != null) {
sails.log.verbose('Loading default scope for \'' + modelDef.globalId + '\'');
var model = global[modelDef.globalId];
if (typeof modelDef.defaultScope === 'function') {
var defaultScope = modelDef.defaultScope() || {};
model.addScope('defaultScope',defaultScope,{override: true});
}
}
}
};
};