@ewb/mongoose-query 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -35
- package/lib/Query.d.ts +60 -77
- package/lib/Query.js +227 -307
- package/lib/QueryError.d.ts +16 -50
- package/lib/QueryError.js +52 -62
- package/lib/QueryHandler.d.ts +47 -59
- package/lib/QueryHandler.js +250 -297
- package/lib/QueryHistory.d.ts +75 -76
- package/lib/QueryHistory.js +137 -154
- package/lib/QueryModel.d.ts +65 -80
- package/lib/QueryModel.js +214 -201
- package/lib/index.d.ts +9 -10
- package/lib/index.js +6 -32
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/Query.utils.d.ts +49 -0
- package/lib/utils/Query.utils.js +143 -0
- package/lib/{constants.d.ts → utils/constants.d.ts} +2 -2
- package/lib/utils/constants.js +2 -0
- package/lib/utils/types.d.ts +11 -0
- package/lib/utils/types.js +1 -0
- package/lib/utils/utils.d.ts +8 -0
- package/lib/{utils.js → utils/utils.js} +38 -44
- package/package.json +4 -4
- package/lib/ExpressHandler.d.ts +0 -30
- package/lib/ExpressHandler.js +0 -215
- package/lib/ExpressHandler.js.map +0 -1
- package/lib/ModelQuery.d.ts +0 -24
- package/lib/ModelQuery.js +0 -80
- package/lib/ModelQuery.js.map +0 -1
- package/lib/Query.js.map +0 -1
- package/lib/QueryError.js.map +0 -1
- package/lib/QueryExpressHandler.d.ts +0 -50
- package/lib/QueryExpressHandler.js +0 -383
- package/lib/QueryExpressHandler.js.map +0 -1
- package/lib/QueryExpressSubHandler.d.ts +0 -20
- package/lib/QueryExpressSubHandler.js +0 -137
- package/lib/QueryExpressSubHandler.js.map +0 -1
- package/lib/QueryHandler.js.map +0 -1
- package/lib/QueryHistory.js.map +0 -1
- package/lib/QueryModel.js.map +0 -1
- package/lib/constants.js +0 -6
- package/lib/constants.js.map +0 -1
- package/lib/declarations.d.ts +0 -16
- package/lib/declarations.js +0 -3
- package/lib/declarations.js.map +0 -1
- package/lib/error/ExpressError.d.ts +0 -44
- package/lib/error/ExpressError.js +0 -39
- package/lib/error/ExpressError.js.map +0 -1
- package/lib/error/QueryError.d.ts +0 -47
- package/lib/error/QueryError.js +0 -50
- package/lib/error/QueryError.js.map +0 -1
- package/lib/error/QueryExpressError.d.ts +0 -46
- package/lib/error/QueryExpressError.js +0 -39
- package/lib/error/QueryExpressError.js.map +0 -1
- package/lib/error/V1Error.d.ts +0 -12
- package/lib/error/V1Error.js +0 -27
- package/lib/error/V1Error.js.map +0 -1
- package/lib/error/error.handler.d.ts +0 -3
- package/lib/error/error.handler.js +0 -30
- package/lib/error/error.handler.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/statics.d.ts +0 -17
- package/lib/statics.js +0 -35
- package/lib/statics.js.map +0 -1
- package/lib/utils.d.ts +0 -3
- package/lib/utils.js.map +0 -1
package/lib/QueryHandler.js
CHANGED
|
@@ -1,297 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.
|
|
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
|
-
return
|
|
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
|
-
return invalidFields.length > 0 ? invalidFields : null;
|
|
255
|
-
}
|
|
256
|
-
toJSON(_, ret) {
|
|
257
|
-
if (this.queryModel.privateFields) {
|
|
258
|
-
for (const key of this.queryModel.privateFields) {
|
|
259
|
-
// @ts-ignore
|
|
260
|
-
if (key in ret) {
|
|
261
|
-
// @ts-ignore
|
|
262
|
-
delete ret[key];
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
populateDoc(doc, populates, subPath) {
|
|
268
|
-
return Promise.all(populates.map((populate) => __awaiter(this, void 0, void 0, function* () {
|
|
269
|
-
try {
|
|
270
|
-
let subPathDoc;
|
|
271
|
-
if (subPath) {
|
|
272
|
-
// @ts-ignore subPaths is there. Not typed
|
|
273
|
-
subPathDoc = this._model.schema.subpaths[`${subPath}.${populate.path}`];
|
|
274
|
-
}
|
|
275
|
-
else {
|
|
276
|
-
subPathDoc = this._model.schema.paths[populate.path];
|
|
277
|
-
}
|
|
278
|
-
let ref = populate.ref || (subPathDoc === null || subPathDoc === void 0 ? void 0 : subPathDoc.options.ref);
|
|
279
|
-
if (!ref && Array.isArray(subPathDoc === null || subPathDoc === void 0 ? void 0 : subPathDoc.options.type)) {
|
|
280
|
-
ref = subPathDoc.options.type[0].ref;
|
|
281
|
-
}
|
|
282
|
-
if (!ref) {
|
|
283
|
-
throw new Error(`ref was not defined in model with path ${populate.path}`);
|
|
284
|
-
}
|
|
285
|
-
const model = mongoose.model(ref);
|
|
286
|
-
yield model.populate(doc, Object.assign({ path: populate.path, select: populate.select.join(' ') }, (populate.populate ? { populate: populate.populate } : {})));
|
|
287
|
-
}
|
|
288
|
-
catch (e) {
|
|
289
|
-
console.error(e);
|
|
290
|
-
console.error({ doc, populates, path: subPath });
|
|
291
|
-
return Promise.resolve();
|
|
292
|
-
}
|
|
293
|
-
})));
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
exports.QueryHandler = QueryHandler;
|
|
297
|
-
//# sourceMappingURL=QueryHandler.js.map
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { toValidTextRegexp, } from './QueryModel';
|
|
3
|
+
export class QueryHandler {
|
|
4
|
+
constructor(_model, queryModel) {
|
|
5
|
+
this._model = _model;
|
|
6
|
+
this.queryModel = queryModel;
|
|
7
|
+
this.toJSON = this.toJSON.bind(this);
|
|
8
|
+
}
|
|
9
|
+
get model() {
|
|
10
|
+
return this._model;
|
|
11
|
+
}
|
|
12
|
+
async create(body) {
|
|
13
|
+
const newDoc = new this._model(body);
|
|
14
|
+
await newDoc.validate();
|
|
15
|
+
await newDoc.save();
|
|
16
|
+
return newDoc;
|
|
17
|
+
}
|
|
18
|
+
async json(doc) {
|
|
19
|
+
return doc.toJSON({ transform: this.toJSON });
|
|
20
|
+
}
|
|
21
|
+
async search(query) {
|
|
22
|
+
const findQuery = this.queryModel.createSearchQuery(query);
|
|
23
|
+
const modelFind = this._model.find(findQuery, query.options);
|
|
24
|
+
this.queryModel.setPopulate(modelFind, query);
|
|
25
|
+
this.queryModel.setLimitAndSkip(modelFind, query);
|
|
26
|
+
this.queryModel.setSort(modelFind, query);
|
|
27
|
+
this.queryModel.setSelect(modelFind, query, query.select);
|
|
28
|
+
const { skip = query.skip, limit = query.limit } = modelFind.getOptions();
|
|
29
|
+
const [data, count] = await Promise.all([
|
|
30
|
+
modelFind.lean().exec(),
|
|
31
|
+
this._model.find(findQuery).count(),
|
|
32
|
+
]);
|
|
33
|
+
return { data: data, count, limit, skip };
|
|
34
|
+
}
|
|
35
|
+
async findOne(query, opts = {}) {
|
|
36
|
+
const filter = query.root || query.createQuery();
|
|
37
|
+
const modelFindOne = this._model.findOne(filter);
|
|
38
|
+
if (opts.populate)
|
|
39
|
+
this.queryModel.setPopulate(modelFindOne, query);
|
|
40
|
+
this.queryModel.setSelect(modelFindOne, query, opts.select ? undefined : []);
|
|
41
|
+
this.queryModel.setProjection(modelFindOne, query);
|
|
42
|
+
return modelFindOne.exec();
|
|
43
|
+
}
|
|
44
|
+
async findOneAndUpdate(query, filter, update) {
|
|
45
|
+
if (this.queryModel.updateDateKey) {
|
|
46
|
+
if ('$set' in update) {
|
|
47
|
+
update['$set'][this.queryModel.updateDateKey] = new Date();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
update[this.queryModel.updateDateKey] = new Date();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const modelUpdateOne = this._model.findOneAndUpdate(filter, update, {
|
|
54
|
+
new: true,
|
|
55
|
+
});
|
|
56
|
+
this.queryModel.setPopulate(modelUpdateOne, query);
|
|
57
|
+
this.queryModel.setSelect(modelUpdateOne, query);
|
|
58
|
+
return modelUpdateOne.exec();
|
|
59
|
+
}
|
|
60
|
+
async deleteOne(filter) {
|
|
61
|
+
return this._model.deleteOne(filter);
|
|
62
|
+
}
|
|
63
|
+
async populate(query, doc, type) {
|
|
64
|
+
try {
|
|
65
|
+
const populates = type ? this.getPopulates(type) : [];
|
|
66
|
+
populates.push(...this.queryModel.getValidPopulate(query.populate));
|
|
67
|
+
await this.populateDoc(doc, populates);
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
getPopulates(type) {
|
|
73
|
+
const populate = this.queryModel.populate;
|
|
74
|
+
if (!populate || populate.length === 0) {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
return populate.filter((x) => x[type] || x.onAll);
|
|
78
|
+
}
|
|
79
|
+
async updateMany(ids, body) {
|
|
80
|
+
return this._model.updateMany({ _id: { $in: ids } }, { $set: body });
|
|
81
|
+
}
|
|
82
|
+
async deleteMany(ids) {
|
|
83
|
+
return this._model.deleteMany({ _id: { $in: ids } });
|
|
84
|
+
}
|
|
85
|
+
async subCreate(sub, query, body) {
|
|
86
|
+
if (!('_id' in body)) {
|
|
87
|
+
body['_id'] = new mongoose.Types.ObjectId();
|
|
88
|
+
}
|
|
89
|
+
await this._model.updateOne({ ...query.root }, { $push: { [sub]: body } });
|
|
90
|
+
return this.subFindOne(sub, query, { _id: body._id });
|
|
91
|
+
}
|
|
92
|
+
async subSearch(sub, query, rootQuery) {
|
|
93
|
+
const subQuery = query.createQuery({ noRoot: true });
|
|
94
|
+
const populate = query.populate;
|
|
95
|
+
const limit = query.limit;
|
|
96
|
+
const skip = query.skip;
|
|
97
|
+
const $lookups = this.queryModel.getSubPopulateLookup(query, sub);
|
|
98
|
+
const { $filter, $match } = this.queryModel.getSubFilterConditionAndMatch(subQuery, sub);
|
|
99
|
+
const pipeline = [
|
|
100
|
+
{ $match: rootQuery },
|
|
101
|
+
{
|
|
102
|
+
$project: {
|
|
103
|
+
[sub]: { $filter },
|
|
104
|
+
count: { $size: `$${String(sub)}` },
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{ $unwind: `$${String(sub)}` },
|
|
108
|
+
...(skip ? [{ $skip: skip }] : []),
|
|
109
|
+
...(limit ? [{ $limit: limit }] : []),
|
|
110
|
+
...$lookups,
|
|
111
|
+
];
|
|
112
|
+
if (Object.keys($match).length > 0) {
|
|
113
|
+
for (const key in $match) {
|
|
114
|
+
if ($match.hasOwnProperty(key)) {
|
|
115
|
+
const [path, pathKey] = key.split('.');
|
|
116
|
+
if (pathKey && populate.some((pop) => pop.path === path)) {
|
|
117
|
+
$match[`${path}.${pathKey}`] = toValidTextRegexp($match[key]);
|
|
118
|
+
}
|
|
119
|
+
else if (typeof $match[key] === 'string') {
|
|
120
|
+
$match[`${String(sub)}.${path}`] = toValidTextRegexp($match[key]);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
$match[`${String(sub)}.${path}`] = $match[key];
|
|
124
|
+
}
|
|
125
|
+
delete $match[key];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
pipeline.push({ $match });
|
|
129
|
+
}
|
|
130
|
+
const arr = await this._model.aggregate(pipeline);
|
|
131
|
+
return arr.reduce((obj, item) => {
|
|
132
|
+
const data = { ...item[sub] };
|
|
133
|
+
for (const key in item) {
|
|
134
|
+
if (!['_id', 'count', 'skip', 'limit', sub].includes(key)) {
|
|
135
|
+
data[key] = Array.isArray(item[key]) ? item[key][0] : item[key];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
count: item.count,
|
|
140
|
+
data: [...obj.data, data],
|
|
141
|
+
skip: obj.skip,
|
|
142
|
+
limit: obj.limit,
|
|
143
|
+
};
|
|
144
|
+
}, { count: 0, data: [], skip, limit });
|
|
145
|
+
}
|
|
146
|
+
async subFindOne(sub, query, subQuery) {
|
|
147
|
+
const subDoc = await this._model.findOne({
|
|
148
|
+
...query.root,
|
|
149
|
+
[sub]: { $elemMatch: subQuery },
|
|
150
|
+
}, { [`${String(sub)}.$`]: 1 });
|
|
151
|
+
return this.getSubDocument(sub, subDoc);
|
|
152
|
+
}
|
|
153
|
+
async subFindOneAndUpdate(sub, query, subQuery, body) {
|
|
154
|
+
const $set = {};
|
|
155
|
+
for (const key in body) {
|
|
156
|
+
$set[`${String(sub)}.$.${key}`] = body[key];
|
|
157
|
+
}
|
|
158
|
+
await this._model.updateOne({
|
|
159
|
+
...query.root,
|
|
160
|
+
[sub]: { $elemMatch: subQuery },
|
|
161
|
+
}, { $set });
|
|
162
|
+
return this.subFindOne(sub, query, subQuery);
|
|
163
|
+
}
|
|
164
|
+
async subDelete(sub, query, subQuery) {
|
|
165
|
+
const subDoc = await this.subFindOne(sub, query, subQuery);
|
|
166
|
+
if (!subDoc)
|
|
167
|
+
return null;
|
|
168
|
+
await this._model.updateOne({
|
|
169
|
+
...query.root,
|
|
170
|
+
[sub]: { $elemMatch: subQuery },
|
|
171
|
+
}, { $pull: { [sub]: { _id: subDoc._id } } });
|
|
172
|
+
return subDoc;
|
|
173
|
+
}
|
|
174
|
+
async subPopulate(query, subDoc, sub, type) {
|
|
175
|
+
const populates = type ? this.getSubPopulate(sub, type) : [];
|
|
176
|
+
populates.push(...this.queryModel.getValidPopulate(query.populate, sub));
|
|
177
|
+
return this.populateDoc(subDoc, populates, sub);
|
|
178
|
+
}
|
|
179
|
+
getSubPopulate(sub, type) {
|
|
180
|
+
const subs = this.queryModel.subs;
|
|
181
|
+
if (!subs)
|
|
182
|
+
return [];
|
|
183
|
+
const docSub = subs.find((x) => x.sub === sub);
|
|
184
|
+
if (!docSub)
|
|
185
|
+
return [];
|
|
186
|
+
return docSub.populate.filter((x) => x[type] || x.onAll);
|
|
187
|
+
}
|
|
188
|
+
getSubDocument(key, doc) {
|
|
189
|
+
if (!doc)
|
|
190
|
+
return null;
|
|
191
|
+
const array = doc[key];
|
|
192
|
+
if (array && Array.isArray(array)) {
|
|
193
|
+
return array[0] || null;
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
validateBody(body) {
|
|
198
|
+
const invalidFields = [];
|
|
199
|
+
if (this.queryModel.publicFields) {
|
|
200
|
+
for (const key in body) {
|
|
201
|
+
if (body.hasOwnProperty(key)) {
|
|
202
|
+
if (!this.queryModel.publicFields.includes(key)) {
|
|
203
|
+
invalidFields.push(key);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return invalidFields.length > 0 ? invalidFields : null;
|
|
209
|
+
}
|
|
210
|
+
toJSON(_, ret) {
|
|
211
|
+
if (this.queryModel.privateFields) {
|
|
212
|
+
for (const key of this.queryModel.privateFields) {
|
|
213
|
+
if (key in ret) {
|
|
214
|
+
delete ret[key];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
populateDoc(doc, populates, subPath) {
|
|
220
|
+
return Promise.all(populates.map(async (populate) => {
|
|
221
|
+
try {
|
|
222
|
+
let subPathDoc;
|
|
223
|
+
if (subPath) {
|
|
224
|
+
subPathDoc = this._model.schema.subpaths[`${subPath}.${populate.path}`];
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
subPathDoc = this._model.schema.paths[populate.path];
|
|
228
|
+
}
|
|
229
|
+
let ref = populate.ref || subPathDoc?.options.ref;
|
|
230
|
+
if (!ref && Array.isArray(subPathDoc?.options.type)) {
|
|
231
|
+
ref = subPathDoc.options.type[0].ref;
|
|
232
|
+
}
|
|
233
|
+
if (!ref) {
|
|
234
|
+
throw new Error(`ref was not defined in model with path ${populate.path}`);
|
|
235
|
+
}
|
|
236
|
+
const model = mongoose.model(ref);
|
|
237
|
+
await model.populate(doc, {
|
|
238
|
+
path: populate.path,
|
|
239
|
+
select: populate.select.join(' '),
|
|
240
|
+
...(populate.populate ? { populate: populate.populate } : {}),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
console.error(e);
|
|
245
|
+
console.error({ doc, populates, path: subPath });
|
|
246
|
+
return Promise.resolve();
|
|
247
|
+
}
|
|
248
|
+
}));
|
|
249
|
+
}
|
|
250
|
+
}
|