queryhelpers.js
6.11 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*!
* Module dependencies
*/
var get = require('lodash.get');
var isDefiningProjection = require('./services/projection/isDefiningProjection');
var utils = require('./utils');
/*!
* Prepare a set of path options for query population.
*
* @param {Query} query
* @param {Object} options
* @return {Array}
*/
exports.preparePopulationOptions = function preparePopulationOptions(query, options) {
var pop = utils.object.vals(query.options.populate);
// lean options should trickle through all queries
if (options.lean) {
pop.forEach(makeLean(options.lean));
}
return pop;
};
/*!
* Prepare a set of path options for query population. This is the MongooseQuery
* version
*
* @param {Query} query
* @param {Object} options
* @return {Array}
*/
exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query, options) {
var pop = utils.object.vals(query._mongooseOptions.populate);
// lean options should trickle through all queries
if (options.lean) {
pop.forEach(makeLean(options.lean));
}
return pop;
};
/*!
* returns discriminator by discriminatorMapping.value
*
* @param {Model} model
* @param {string} value
*/
function getDiscriminatorByValue(model, value) {
var discriminator = null;
if (!model.discriminators) {
return discriminator;
}
for (var name in model.discriminators) {
var it = model.discriminators[name];
if (
it.schema &&
it.schema.discriminatorMapping &&
it.schema.discriminatorMapping.value == value
) {
discriminator = it;
break;
}
}
return discriminator;
}
exports.getDiscriminatorByValue = getDiscriminatorByValue;
/*!
* If the document is a mapped discriminator type, it returns a model instance for that type, otherwise,
* it returns an instance of the given model.
*
* @param {Model} model
* @param {Object} doc
* @param {Object} fields
*
* @return {Model}
*/
exports.createModel = function createModel(model, doc, fields, userProvidedFields) {
var discriminatorMapping = model.schema
? model.schema.discriminatorMapping
: null;
var key = discriminatorMapping && discriminatorMapping.isRoot
? discriminatorMapping.key
: null;
var value = doc[key];
if (key && value && model.discriminators) {
var discriminator = model.discriminators[value] || getDiscriminatorByValue(model, value);
if (discriminator) {
var _fields = utils.clone(userProvidedFields);
exports.applyPaths(_fields, discriminator.schema);
return new discriminator(undefined, _fields, true);
}
}
return new model(undefined, fields, true);
};
/*!
* ignore
*/
exports.applyPaths = function applyPaths(fields, schema) {
// determine if query is selecting or excluding fields
var exclude;
var keys;
var ki;
var field;
if (fields) {
keys = Object.keys(fields);
ki = keys.length;
while (ki--) {
if (keys[ki][0] === '+') {
continue;
}
field = fields[keys[ki]];
// Skip `$meta` and `$slice`
if (!isDefiningProjection(field)) {
continue;
}
exclude = field === 0;
break;
}
}
// if selecting, apply default schematype select:true fields
// if excluding, apply schematype select:false fields
var selected = [];
var excluded = [];
var stack = [];
var analyzePath = function(path, type) {
if (typeof type.selected !== 'boolean') return;
var plusPath = '+' + path;
if (fields && plusPath in fields) {
// forced inclusion
delete fields[plusPath];
// if there are other fields being included, add this one
// if no other included fields, leave this out (implied inclusion)
if (exclude === false && keys.length > 1 && !~keys.indexOf(path)) {
fields[path] = 1;
}
return;
}
// check for parent exclusions
var pieces = path.split('.');
var root = pieces[0];
if (~excluded.indexOf(root)) {
return;
}
// Special case: if user has included a parent path of a discriminator key,
// don't explicitly project in the discriminator key because that will
// project out everything else under the parent path
if (!exclude && get(type, 'options.$skipDiscriminatorCheck', false)) {
var cur = '';
for (var i = 0; i < pieces.length; ++i) {
cur += (cur.length === 0 ? '' : '.') + pieces[i];
var projection = get(fields, cur, false);
if (projection && typeof projection !== 'object') {
return;
}
}
}
(type.selected ? selected : excluded).push(path);
};
var analyzeSchema = function(schema, prefix) {
prefix || (prefix = '');
// avoid recursion
if (stack.indexOf(schema) !== -1) {
return;
}
stack.push(schema);
schema.eachPath(function(path, type) {
if (prefix) path = prefix + '.' + path;
analyzePath(path, type);
// array of subdocs?
if (type.schema) {
analyzeSchema(type.schema, path);
}
});
stack.pop();
};
analyzeSchema(schema);
var i;
switch (exclude) {
case true:
for (i = 0; i < excluded.length; ++i) {
fields[excluded[i]] = 0;
}
break;
case false:
if (schema &&
schema.paths['_id'] &&
schema.paths['_id'].options &&
schema.paths['_id'].options.select === false) {
fields._id = 0;
}
for (i = 0; i < selected.length; ++i) {
fields[selected[i]] = 1;
}
break;
case undefined:
// user didn't specify fields, implies returning all fields.
// only need to apply excluded fields
for (i = 0; i < excluded.length; ++i) {
fields[excluded[i]] = 0;
}
break;
}
};
/*!
* Set each path query option to lean
*
* @param {Object} option
*/
function makeLean(val) {
return function(option) {
option.options || (option.options = {});
option.options.lean = val;
};
}
/*!
* Handle the `WriteOpResult` from the server
*/
exports.handleWriteOpResult = function handleWriteOpResult(callback) {
return function _handleWriteOpResult(error, res) {
if (error) {
return callback(error);
}
return callback(null, res.result);
};
};