getModelsMapForPopulate.js
13.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
const MongooseError = require('../../error/index');
const get = require('../get');
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
const isPathExcluded = require('../projection/isPathExcluded');
const getSchemaTypes = require('./getSchemaTypes');
const getVirtual = require('./getVirtual');
const normalizeRefPath = require('./normalizeRefPath');
const util = require('util');
const utils = require('../../utils');
const modelSymbol = require('../symbols').modelSymbol;
const populateModelSymbol = require('../symbols').populateModelSymbol;
const schemaMixedSymbol = require('../../schema/symbols').schemaMixedSymbol;
module.exports = function getModelsMapForPopulate(model, docs, options) {
let i;
let doc;
const len = docs.length;
const available = {};
const map = [];
const modelNameFromQuery = options.model && options.model.modelName || options.model;
let schema;
let refPath;
let Model;
let currentOptions;
let modelNames;
let modelName;
let modelForFindSchema;
const originalModel = options.model;
let isVirtual = false;
const modelSchema = model.schema;
for (i = 0; i < len; i++) {
doc = docs[i];
schema = getSchemaTypes(modelSchema, doc, options.path);
const isUnderneathDocArray = schema && schema.$isUnderneathDocArray;
if (isUnderneathDocArray && get(options, 'options.sort') != null) {
return new MongooseError('Cannot populate with `sort` on path ' + options.path +
' because it is a subproperty of a document array');
}
modelNames = null;
let isRefPath = false;
if (Array.isArray(schema)) {
for (let j = 0; j < schema.length; ++j) {
let _modelNames;
try {
const res = _getModelNames(doc, schema[j]);
_modelNames = res.modelNames;
isRefPath = res.isRefPath;
} catch (error) {
return error;
}
if (!_modelNames) {
continue;
}
modelNames = modelNames || [];
for (let x = 0; x < _modelNames.length; ++x) {
if (modelNames.indexOf(_modelNames[x]) === -1) {
modelNames.push(_modelNames[x]);
}
}
}
} else {
try {
const res = _getModelNames(doc, schema);
modelNames = res.modelNames;
isRefPath = res.isRefPath;
} catch (error) {
return error;
}
if (!modelNames) {
continue;
}
}
const _virtualRes = getVirtual(model.schema, options.path);
const virtual = _virtualRes == null ? null : _virtualRes.virtual;
let localField;
let count = false;
if (virtual && virtual.options) {
const virtualPrefix = _virtualRes.nestedSchemaPath ?
_virtualRes.nestedSchemaPath + '.' : '';
if (typeof virtual.options.localField === 'function') {
localField = virtualPrefix + virtual.options.localField.call(doc, doc);
} else {
localField = virtualPrefix + virtual.options.localField;
}
count = virtual.options.count;
} else {
localField = options.path;
}
let foreignField = virtual && virtual.options ?
virtual.options.foreignField :
'_id';
// `justOne = null` means we don't know from the schema whether the end
// result should be an array or a single doc. This can result from
// populating a POJO using `Model.populate()`
let justOne = null;
if ('justOne' in options && options.justOne !== void 0) {
justOne = options.justOne;
} else if (virtual && virtual.options && virtual.options.refPath) {
const normalizedRefPath =
normalizeRefPath(virtual.options.refPath, doc, options.path);
justOne = !!virtual.options.justOne;
isVirtual = true;
const refValue = utils.getValue(normalizedRefPath, doc);
modelNames = Array.isArray(refValue) ? refValue : [refValue];
} else if (virtual && virtual.options && virtual.options.ref) {
let normalizedRef;
if (typeof virtual.options.ref === 'function') {
normalizedRef = virtual.options.ref.call(doc, doc);
} else {
normalizedRef = virtual.options.ref;
}
justOne = !!virtual.options.justOne;
isVirtual = true;
if (!modelNames) {
modelNames = [].concat(normalizedRef);
}
} else if (schema && !schema[schemaMixedSymbol]) {
// Skip Mixed types because we explicitly don't do casting on those.
justOne = !schema.$isMongooseArray;
}
if (!modelNames) {
continue;
}
if (virtual && (!localField || !foreignField)) {
return new MongooseError('If you are populating a virtual, you must set the ' +
'localField and foreignField options');
}
options.isVirtual = isVirtual;
options.virtual = virtual;
if (typeof localField === 'function') {
localField = localField.call(doc, doc);
}
if (typeof foreignField === 'function') {
foreignField = foreignField.call(doc);
}
const localFieldPathType = modelSchema._getPathType(localField);
const localFieldPath = localFieldPathType === 'real' ? modelSchema.path(localField) : localFieldPathType.schema;
const localFieldGetters = localFieldPath && localFieldPath.getters ? localFieldPath.getters : [];
let ret;
const _populateOptions = get(options, 'options', {});
const getters = 'getters' in _populateOptions ?
_populateOptions.getters :
options.isVirtual && get(virtual, 'options.getters', false);
if (localFieldGetters.length > 0 && getters) {
const hydratedDoc = (doc.$__ != null) ? doc : model.hydrate(doc);
const localFieldValue = utils.getValue(localField, doc);
if (Array.isArray(localFieldValue)) {
const localFieldHydratedValue = utils.getValue(localField.split('.').slice(0, -1), hydratedDoc);
ret = localFieldValue.map((localFieldArrVal, localFieldArrIndex) =>
localFieldPath.applyGetters(localFieldArrVal, localFieldHydratedValue[localFieldArrIndex]));
} else {
ret = localFieldPath.applyGetters(localFieldValue, hydratedDoc);
}
} else {
ret = convertTo_id(utils.getValue(localField, doc), schema);
}
const id = String(utils.getValue(foreignField, doc));
options._docs[id] = Array.isArray(ret) ? ret.slice() : ret;
let match = get(options, 'match', null) ||
get(currentOptions, 'match', null) ||
get(options, 'virtual.options.options.match', null);
const hasMatchFunction = typeof match === 'function';
if (hasMatchFunction) {
match = match.call(doc, doc);
}
let k = modelNames.length;
while (k--) {
modelName = modelNames[k];
if (modelName == null) {
continue;
}
// `PopulateOptions#connection`: if the model is passed as a string, the
// connection matters because different connections have different models.
const connection = options.connection != null ? options.connection : model.db;
try {
Model = originalModel && originalModel[modelSymbol] ?
originalModel :
modelName[modelSymbol] ? modelName : connection.model(modelName);
} catch (error) {
return error;
}
let ids = ret;
const flat = Array.isArray(ret) ? utils.array.flatten(ret) : [];
if (isRefPath && Array.isArray(ret) && flat.length === modelNames.length) {
ids = flat.filter((val, i) => modelNames[i] === modelName);
}
if (!available[modelName]) {
currentOptions = {
model: Model
};
if (isVirtual && virtual.options && virtual.options.options) {
currentOptions.options = utils.clone(virtual.options.options);
}
utils.merge(currentOptions, options);
// Used internally for checking what model was used to populate this
// path.
options[populateModelSymbol] = Model;
available[modelName] = {
model: Model,
options: currentOptions,
match: hasMatchFunction ? [match] : match,
docs: [doc],
ids: [ids],
allIds: [ret],
localField: new Set([localField]),
foreignField: new Set([foreignField]),
justOne: justOne,
isVirtual: isVirtual,
virtual: virtual,
count: count,
[populateModelSymbol]: Model
};
map.push(available[modelName]);
} else {
available[modelName].localField.add(localField);
available[modelName].foreignField.add(foreignField);
available[modelName].docs.push(doc);
available[modelName].ids.push(ids);
available[modelName].allIds.push(ret);
if (hasMatchFunction) {
available[modelName].match.push(match);
}
}
}
}
function _getModelNames(doc, schema) {
let modelNames;
let discriminatorKey;
let isRefPath = false;
if (schema && schema.caster) {
schema = schema.caster;
}
if (schema && schema.$isSchemaMap) {
schema = schema.$__schemaType;
}
if (!schema && model.discriminators) {
discriminatorKey = model.schema.discriminatorMapping.key;
}
refPath = schema && schema.options && schema.options.refPath;
const normalizedRefPath = normalizeRefPath(refPath, doc, options.path);
if (modelNameFromQuery) {
modelNames = [modelNameFromQuery]; // query options
} else if (normalizedRefPath) {
if (options._queryProjection != null && isPathExcluded(options._queryProjection, normalizedRefPath)) {
throw new MongooseError('refPath `' + normalizedRefPath +
'` must not be excluded in projection, got ' +
util.inspect(options._queryProjection));
}
if (modelSchema.virtuals.hasOwnProperty(normalizedRefPath) && doc.$__ == null) {
modelNames = [modelSchema.virtuals[normalizedRefPath].applyGetters(void 0, doc)];
} else {
modelNames = utils.getValue(normalizedRefPath, doc);
}
if (Array.isArray(modelNames)) {
modelNames = utils.array.flatten(modelNames);
}
isRefPath = true;
} else {
let modelForCurrentDoc = model;
let schemaForCurrentDoc;
if (!schema && discriminatorKey) {
modelForFindSchema = utils.getValue(discriminatorKey, doc);
if (modelForFindSchema) {
// `modelForFindSchema` is the discriminator value, so we might need
// find the discriminated model name
const discriminatorModel = getDiscriminatorByValue(model, modelForFindSchema);
if (discriminatorModel != null) {
modelForCurrentDoc = discriminatorModel;
} else {
try {
modelForCurrentDoc = model.db.model(modelForFindSchema);
} catch (error) {
return error;
}
}
schemaForCurrentDoc = modelForCurrentDoc.schema._getSchema(options.path);
if (schemaForCurrentDoc && schemaForCurrentDoc.caster) {
schemaForCurrentDoc = schemaForCurrentDoc.caster;
}
}
} else {
schemaForCurrentDoc = schema;
}
const _virtualRes = getVirtual(modelForCurrentDoc.schema, options.path);
const virtual = _virtualRes == null ? null : _virtualRes.virtual;
let ref;
let refPath;
if ((ref = get(schemaForCurrentDoc, 'options.ref')) != null) {
ref = handleRefFunction(ref, doc);
modelNames = [ref];
} else if ((ref = get(virtual, 'options.ref')) != null) {
ref = handleRefFunction(ref, doc);
// When referencing nested arrays, the ref should be an Array
// of modelNames.
if (Array.isArray(ref)) {
modelNames = ref;
} else {
modelNames = [ref];
}
isVirtual = true;
} else if ((refPath = get(schemaForCurrentDoc, 'options.refPath')) != null) {
isRefPath = true;
refPath = normalizeRefPath(refPath, doc, options.path);
modelNames = utils.getValue(refPath, doc);
if (Array.isArray(modelNames)) {
modelNames = utils.array.flatten(modelNames);
}
} else {
// We may have a discriminator, in which case we don't want to
// populate using the base model by default
modelNames = discriminatorKey ? null : [model.modelName];
}
}
if (!modelNames) {
return { modelNames: modelNames, isRefPath: isRefPath };
}
if (!Array.isArray(modelNames)) {
modelNames = [modelNames];
}
return { modelNames: modelNames, isRefPath: isRefPath };
}
return map;
};
/*!
* ignore
*/
function handleRefFunction(ref, doc) {
if (typeof ref === 'function' && !ref[modelSymbol]) {
return ref.call(doc, doc);
}
return ref;
}
/*!
* Retrieve the _id of `val` if a Document or Array of Documents.
*
* @param {Array|Document|Any} val
* @return {Array|Document|Any}
*/
function convertTo_id(val, schema) {
if (val != null && val.$__ != null) return val._id;
if (Array.isArray(val)) {
for (let i = 0; i < val.length; ++i) {
if (val[i] != null && val[i].$__ != null) {
val[i] = val[i]._id;
}
}
if (val.isMongooseArray && val.$schema()) {
return val.$schema().cast(val, val.$parent());
}
return [].concat(val);
}
// `populate('map')` may be an object if populating on a doc that hasn't
// been hydrated yet
if (val != null &&
val.constructor.name === 'Object' &&
// The intent here is we should only flatten the object if we expect
// to get a Map in the end. Avoid doing this for mixed types.
(schema == null || schema[schemaMixedSymbol] == null)) {
const ret = [];
for (const key of Object.keys(val)) {
ret.push(val[key]);
}
return ret;
}
// If doc has already been hydrated, e.g. `doc.populate('map').execPopulate()`
// then `val` will already be a map
if (val instanceof Map) {
return Array.from(val.values());
}
return val;
}