forked from mohemohe/sails-hook-mongoat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
138 lines (129 loc) · 4.59 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Created by Mike on 26/05/15.
*/
module.exports = function (sails) {
// store created indexes
var indexes = [],
// see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#options-for-all-index-types
// UNUSED - TODO check for valid options.
validProperties = [
'unique', // boolean
'name', // string
'expiresAfterSeconds', // integer
'sparse', // boolean
'background', // boolean
// text index options
'weights', // object
'default_language', // string
'language_override', // string
'textIndexVersion', // integer
// options for for 2dsphere Indexes,
'2dsphereIndexVersion', // integer
// options for 2d indexes
'bits', // integer
'min', // number
'max', // number
// options for geoHaystack Indexes
'bucketSize' // number
];
if (!_)
var _ = require('lodash');
if (!async)
var async = require('async');
var getIndexes = function (key, done) {
var model = sails.models[key];
// check for indexes
if (_.isArray(model.indexes) && model.indexes.length > 0) {
async.forEachOf(model.indexes, function (indexObject, i, done) {
model.indexes[i].model = key; // add model name to index
done();
}, function () {
indexes = _.union(indexes,model.indexes);
done();
});
} else {
done();
}
};
return {
createIndex: function (modelName, fields, options, next) {
var model = sails.models[modelName];
// check model adapter is sails-mongo by checking first connections adapter -- is this the best way?
if (model && _.isFunction(model.getDatastore)) {
var db = model.getDatastore().manager;
var collection = db.collection(modelName);
if(_.isFunction(collection.ensureIndex)) {
collection.ensureIndex(fields, options, function (err) {
if (err) {
sails.log.error('Mongoat: Error creating index for model', modelName);
sails.log.error(fields);
sails.log.error(err);
} else {
sails.log.verbose('Mongoat: An index was created for model', modelName, fields, options);
}
if (_.isFunction(next)) {
next(err);
}
});
} else {
sails.log.error('Mongoat: Native collection is not function', modelName);
if (_.isFunction(next)) {
next(err);
}
}
} else if (model && _.isFunction(model.native)) {
model.native(function (err, collection) {
if (err) {
sails.log.error('Mongoat: Could not connect to MongoDB', modelName);
if (_.isFunction(next)) {
next(err);
}
} else {
if(_.isFunction(collection.ensureIndex)) {
collection.ensureIndex(fields, options, function (err) {
if (err) {
sails.log.error('Mongoat: Error creating index for model', modelName);
sails.log.error(fields);
sails.log.error(err);
} else {
sails.log.verbose('Mongoat: An index was created for model', modelName, fields, options);
}
if (_.isFunction(next)) {
next(err);
}
});
} else {
sails.log.error('Mongoat: Native collection is not function', modelName);
if (_.isFunction(next)) {
next(err);
}
}
}
});
} else {
if (_.isFunction(next))
next('Model not provided or model adapter is not provided native collection.')
}
},
initialize: function (cb) {
var self = this;
var eventsToWaitFor = [];
if (sails.hooks.orm)
eventsToWaitFor.push('hook:orm:loaded');
sails.after(eventsToWaitFor, function () {
sails.log.verbose('sails mongoat started');
if(sails.config.models.migrate !== 'alter' && sails.config.models.migrate !== 'drop') {
sails.log.verbose('sails mongoat skipping index creation (according to "' + sails.config.models.migrate + '" migration strategy)');
return cb();
}
async.each(Object.keys(sails.models), getIndexes, function () {
async.each(indexes, function (index, done) {
self.createIndex(index.model, index.attributes, index.options || {}, done);
}, function () {
cb();
});
});
});
}
};
};