-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
279 lines (234 loc) · 8.44 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
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
const mongoose = require.main.require('mongoose');
const assert = require('assert');
/**
* Process each path of the schema and each path of each subschema
* @param {Schema} schema
* @param {Function<String, SchemaType>} handler
* @param {String[]} path root of the path
*/
function eachPathRecursive(schema, handler, path) {
if (!path) path = [];
schema.eachPath(function (pathName, schemaType) {
path.push(pathName);
if (schemaType.schema) eachPathRecursive(schemaType.schema, handler, path);
else handler(path.join('.'), schemaType);
path.pop();
});
}
var Query = mongoose.Query,
_exec = Query.prototype.exec;
/**
* Registers sub refs populate on this query.
* @param {import('mongoose').ModelPopulateOptions[]} options
* @remark this method can only be invoked once per query.
* @return {Query}
*/
Query.prototype.subPopulate = function (options) {
if (this.model.schema.methods.subPopulate == null) throw new Error('Plugin was not installed');
if (this._subPopulate) throw new Error('subPopulate was already invoked');
if (!options) return this;
this._subPopulate = { options: options };
return this;
};
/**
* Monkey-patches `exec` to add deep population hook.
* @param op the operation to be executed.
* @param cb the callback.
* @return {MongoosePromise}
*/
Query.prototype.exec = function (op, cb) {
var subPopulate = this._subPopulate;
if (!subPopulate) return _exec.call(this, op, cb);
const model = this.model,
options = subPopulate.options,
lean = this._mongooseOptions.lean;
if (typeof op === 'function') {
cb = op;
op = null;
} else cb = cb || (() => {});
return new mongoose.Promise(
function (resolve, reject) {
_exec.call(this, op, function (err, docs) {
if (err) return reject(err), cb(err);
if (!docs) return resolve(docs), cb(null, docs);
execute(model, docs, options, lean, function (err, docs) {
if (err) reject(err), cb(err);
else resolve(docs), cb(null, docs);
});
});
}.bind(this)
);
};
/**
* Format options
* @param {import('mongoose').ModelPopulateOptions[]|import('mongoose').ModelPopulateOptions|String[]|String} options
* @returns {import('mongoose').ModelPopulateOptions[]}
*/
function formatOptions(options) {
if (options instanceof Array) {
if (options.length > 0) {
if (typeof options[0] === 'object') return options;
return options.map((o) => ({ path: o }));
}
return options;
}
if (typeof options === 'object') return [options];
return [{ path: options }];
}
// Lean not supported for the moment
/**
* @param {import('mongoose').ModelPopulateOptions[]|import('mongoose').ModelPopulateOptions|String[]|String} options
*/
function execute(model, docs, options, lean, cb) {
options = formatOptions(options);
let resolvedCount = 0,
error = false;
for (let option of options) {
// The schema type of sub populate's path
const localFieldSchemaType = model.schema.path(option.path);
// Do we know who is the ref to the root document of the sub reference ?
const localFieldBoundToSchemaType = localFieldSchemaType.options.boundTo
? model.schema.path(localFieldSchemaType.options.boundTo)
: null;
// Root is always the starting model
const [rootRef, ...subRefRest] = localFieldSchemaType.options.subRef.split('.');
const subPathRef = subRefRest.join('.');
// Referenced root model
const rootRefModel = mongoose.model(rootRef);
// Ensure that boundTo field is referencing the same model of subRef
if (localFieldBoundToSchemaType)
assert.strictEqual(localFieldBoundToSchemaType.options.ref, rootRef);
// The schema type of referenced path
const foreignFieldSchemaType = rootRefModel.schema.path(subPathRef);
// Is the local field referencing an array of what ?
const arrayOfObjects = !foreignFieldSchemaType.options.type[0].type;
const arrayOfReferences = !!foreignFieldSchemaType.options.type[0].ref;
assert(
arrayOfObjects || arrayOfReferences,
'Unsupported sub-reference type. Sub-references are valid only for array of subdocuments and array of refs.'
);
const refIds = {};
// Take all refIds for this option and associate them with the corresponding docs
for (let doc of docs) {
const refId = doc[option.path];
if (refIds[refId]) refIds[refId].docs.push(doc);
else refIds[refId] = { refId, docs: [doc] };
}
const aggregationOptions = [
// Match the interested values
{
$match: localFieldBoundToSchemaType
? // This is the fast way
{
_id: {
$in: docs.map((doc) => {
let ref = doc.get(localFieldSchemaType.options.boundTo);
return ref._id || ref;
}),
},
}
: // This is the slow way
{
[`${subPathRef}${arrayOfObjects ? '._id' : ''}`]: {
$in: Object.keys(refIds).map((r) => refIds[r].refId),
},
},
},
// Unwind
{
$unwind: { path: `$${subPathRef}` },
},
...[
arrayOfObjects
? // Replace every unwinded document with the corrispective sub document
{
$replaceRoot: { newRoot: `$${subPathRef}` },
}
: // Replace every unwinded document with an object {_id: "... ref id ..."}
{ $replaceRoot: { newRoot: { _id: `$${subPathRef}` } } },
],
// If we are referencing an array of refs then we need to lookup
// to take the referenced documents
...(arrayOfReferences
? [
// Lookup
{
$lookup: {
// Get collection's name of the refs
from: mongoose.model(foreignFieldSchemaType.options.type[0].ref).collection
.collectionName,
localField: '_id',
foreignField: '_id',
as: 'lookupOne',
},
},
// Extract only the interested data
{
$unwind: '$lookupOne',
},
// Finally we have a collection of referenced documents
{
$replaceRoot: { newRoot: '$lookupOne' },
},
]
: []),
];
// Find exactly one reference associated with this subPath's value
rootRefModel.aggregate(aggregationOptions).exec(function (err, refSubDocuments) {
if (error) return;
resolvedCount++;
if (err) {
error = true;
return cb(err);
}
// Associate every retrieved referenced subDocument to the corresponding doc
for (let subDoc of refSubDocuments) {
if (refIds[subDoc._id] && refIds[subDoc._id].docs) {
for (let associatedDoc of refIds[subDoc._id].docs)
associatedDoc.set(option.path, subDoc, mongoose.Schema.Types.Mixed);
// Done with this ref
delete refIds[subDoc._id];
}
}
// The unresolved remaining refs must be set to null
Object.keys(refIds).forEach((k) => {
const associatedDocs = refIds[k].docs;
for (let associatedDoc of associatedDocs)
associatedDoc.set(option.path, null, mongoose.Schema.Types.Mixed);
});
if (resolvedCount === options.length) cb(null, docs);
});
}
}
/**
* This plugin ensures that using subPopulate(options)
* the resulting documents will have all sub document ref-fields populated,
*
* N.B.
* - Refs are specified using the property (subRef) on SchemaType's options
*
* @param {mongoose.Schema} schema the schema on which add the new function 'subPopulate'
*/
module.exports = function subDocumentReferences(schema) {
const populableReferencesPaths = {};
// Find all paths that contains a reference to sub documents
eachPathRecursive(schema, (path, schemaType) => {
if (schemaType.options.subRef) populableReferencesPaths[path] = schemaType;
});
/**
* This will populate sub refs
* @param {import('mongoose').ModelPopulateOptions[]|import('mongoose').ModelPopulateOptions|String[]|String} options
* @returns {Promise}
*/
schema.methods.subPopulate = function (options = null) {
const model = this.constructor;
if (options)
return new Promise((resolve, reject) =>
execute(model, [this], options, false, function (err, docs) {
if (err) return reject(err);
return resolve(docs[0]);
})
);
else Promise.resolve();
};
};