forked from teamfa/sails-hook-mongoat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (58 loc) · 2.07 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
module.exports = function (sails) {
// store created indexes
var indexes = [];
var getIndexes = function (key) {
var model = sails.models[key];
// check for indexes
if (_.isArray(model.indexes) && model.indexes.length > 0) {
_.each(model.indexes, function (indexObject, i) {
model.indexes[i].model = key; // add model name to index
});
indexes = _.union(indexes, model.indexes);
}
};
var createIndex = function (modelName, fields, options, done) {
var Model = sails.models[modelName];
if (!Model) return done();
// check model adapter is sails-mongo by checking first connections adapter
if (Model._adapter.identity !== 'sails-mongo') return done();
var db = Model.getDatastore().manager;
db.collection(Model.tableName).createIndex(fields, options, function (err) {
if (err) {
sails.log.error('Mongoat: Error creating index', modelName, err);
return done(err);
}
sails.log.verbose('Mongoat: An index was created for model', modelName);
return done();
});
};
return {
defaults: {
__configKey__: {
_hookTimeout: 60 * 60 * 1000
}
},
initialize: function (cb) {
// Don't call it if we don't force it with config.models.createIndexes
// The app lift faster if we don't create the index each time
if(!sails.config.models.createIndexes) return cb();
if (!_ || !async){
sails.log.error('Set as globals _ and async to use mongoat hook.');
return cb();
} else if (!sails.hooks.orm){
sails.log.error('Waterline ORM hook has to be actived.');
return cb();
}
sails.after('hook:orm:loaded', function () {
sails.log.verbose('sails-hook-mongoat started.');
_.each(Object.keys(sails.models), getIndexes);
startIndexCreation();
});
var startIndexCreation = function () {
async.each(indexes, function createEachIndex(index, next) {
createIndex(index.model, index.attributes, index.options || {}, next);
}, cb);
};
}
};
};