@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.
Files changed (66) hide show
  1. package/README.md +24 -35
  2. package/lib/Query.d.ts +60 -77
  3. package/lib/Query.js +227 -307
  4. package/lib/QueryError.d.ts +16 -50
  5. package/lib/QueryError.js +52 -62
  6. package/lib/QueryHandler.d.ts +47 -59
  7. package/lib/QueryHandler.js +250 -297
  8. package/lib/QueryHistory.d.ts +75 -76
  9. package/lib/QueryHistory.js +137 -154
  10. package/lib/QueryModel.d.ts +65 -80
  11. package/lib/QueryModel.js +214 -201
  12. package/lib/index.d.ts +9 -10
  13. package/lib/index.js +6 -32
  14. package/lib/tsconfig.tsbuildinfo +1 -1
  15. package/lib/utils/Query.utils.d.ts +49 -0
  16. package/lib/utils/Query.utils.js +143 -0
  17. package/lib/{constants.d.ts → utils/constants.d.ts} +2 -2
  18. package/lib/utils/constants.js +2 -0
  19. package/lib/utils/types.d.ts +11 -0
  20. package/lib/utils/types.js +1 -0
  21. package/lib/utils/utils.d.ts +8 -0
  22. package/lib/{utils.js → utils/utils.js} +38 -44
  23. package/package.json +4 -4
  24. package/lib/ExpressHandler.d.ts +0 -30
  25. package/lib/ExpressHandler.js +0 -215
  26. package/lib/ExpressHandler.js.map +0 -1
  27. package/lib/ModelQuery.d.ts +0 -24
  28. package/lib/ModelQuery.js +0 -80
  29. package/lib/ModelQuery.js.map +0 -1
  30. package/lib/Query.js.map +0 -1
  31. package/lib/QueryError.js.map +0 -1
  32. package/lib/QueryExpressHandler.d.ts +0 -50
  33. package/lib/QueryExpressHandler.js +0 -383
  34. package/lib/QueryExpressHandler.js.map +0 -1
  35. package/lib/QueryExpressSubHandler.d.ts +0 -20
  36. package/lib/QueryExpressSubHandler.js +0 -137
  37. package/lib/QueryExpressSubHandler.js.map +0 -1
  38. package/lib/QueryHandler.js.map +0 -1
  39. package/lib/QueryHistory.js.map +0 -1
  40. package/lib/QueryModel.js.map +0 -1
  41. package/lib/constants.js +0 -6
  42. package/lib/constants.js.map +0 -1
  43. package/lib/declarations.d.ts +0 -16
  44. package/lib/declarations.js +0 -3
  45. package/lib/declarations.js.map +0 -1
  46. package/lib/error/ExpressError.d.ts +0 -44
  47. package/lib/error/ExpressError.js +0 -39
  48. package/lib/error/ExpressError.js.map +0 -1
  49. package/lib/error/QueryError.d.ts +0 -47
  50. package/lib/error/QueryError.js +0 -50
  51. package/lib/error/QueryError.js.map +0 -1
  52. package/lib/error/QueryExpressError.d.ts +0 -46
  53. package/lib/error/QueryExpressError.js +0 -39
  54. package/lib/error/QueryExpressError.js.map +0 -1
  55. package/lib/error/V1Error.d.ts +0 -12
  56. package/lib/error/V1Error.js +0 -27
  57. package/lib/error/V1Error.js.map +0 -1
  58. package/lib/error/error.handler.d.ts +0 -3
  59. package/lib/error/error.handler.js +0 -30
  60. package/lib/error/error.handler.js.map +0 -1
  61. package/lib/index.js.map +0 -1
  62. package/lib/statics.d.ts +0 -17
  63. package/lib/statics.js +0 -35
  64. package/lib/statics.js.map +0 -1
  65. package/lib/utils.d.ts +0 -3
  66. package/lib/utils.js.map +0 -1
@@ -1,297 +1,250 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.QueryHandler = void 0;
13
- const mongoose_1 = require("mongoose");
14
- const QueryModel_1 = require("./QueryModel");
15
- const mongoose = require("mongoose");
16
- class QueryHandler {
17
- constructor(_model, queryModel) {
18
- this._model = _model;
19
- this.queryModel = queryModel;
20
- this.toJSON = this.toJSON.bind(this);
21
- }
22
- get model() {
23
- return this._model;
24
- }
25
- create(body) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- const newDoc = new this._model(body);
28
- yield newDoc.validate();
29
- yield newDoc.save();
30
- return newDoc;
31
- });
32
- }
33
- json(doc) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- return doc.toJSON({ transform: this.toJSON });
36
- });
37
- }
38
- search(query) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- const findQuery = this.queryModel.createSearchQuery(query);
41
- const modelFind = this._model.find(findQuery, query.options);
42
- this.queryModel.setPopulate(modelFind, query);
43
- this.queryModel.setLimitAndSkip(modelFind, query);
44
- this.queryModel.setSort(modelFind, query);
45
- this.queryModel.setSelect(modelFind, query, query.select);
46
- const { skip = query.skip, limit = query.limit } = modelFind.getOptions();
47
- const [data, count] = yield Promise.all([
48
- modelFind.lean().exec(),
49
- this._model.find(findQuery).count()
50
- ]);
51
- return { data: data, count, limit, skip };
52
- });
53
- }
54
- findOne(query, opts = {}) {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- const filter = query.root || query.createQuery();
57
- const modelFindOne = this._model.findOne(filter);
58
- if (opts.populate)
59
- this.queryModel.setPopulate(modelFindOne, query);
60
- this.queryModel.setSelect(modelFindOne, query, opts.select ? undefined : []);
61
- this.queryModel.setProjection(modelFindOne, query);
62
- return modelFindOne.exec();
63
- });
64
- }
65
- findOneAndUpdate(query, filter, update) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- if (this.queryModel.updateDateKey) {
68
- if ('$set' in update) {
69
- // @ts-ignore
70
- update['$set'][this.queryModel.updateDateKey] = new Date();
71
- }
72
- else {
73
- // @ts-ignore
74
- update[this.queryModel.updateDateKey] = new Date();
75
- }
76
- }
77
- const modelUpdateOne = this._model.findOneAndUpdate(filter, update, {
78
- new: true
79
- });
80
- this.queryModel.setPopulate(modelUpdateOne, query);
81
- this.queryModel.setSelect(modelUpdateOne, query);
82
- return modelUpdateOne.exec();
83
- });
84
- }
85
- deleteOne(filter) {
86
- return __awaiter(this, void 0, void 0, function* () {
87
- return this._model.deleteOne(filter);
88
- });
89
- }
90
- populate(req, doc, type) {
91
- return __awaiter(this, void 0, void 0, function* () {
92
- try {
93
- const populates = type ? this.getPopulates(type) : [];
94
- populates.push(...this.queryModel.getValidPopulate(req.mongooseQuery.populate));
95
- yield this.populateDoc(doc, populates);
96
- }
97
- catch (e) {
98
- // empty
99
- }
100
- });
101
- }
102
- getPopulates(type) {
103
- const populate = this.queryModel.populate;
104
- if (!populate || populate.length === 0) {
105
- return [];
106
- }
107
- return populate.filter(x => x[type] || x.onAll);
108
- }
109
- updateMany(ids, body) {
110
- return __awaiter(this, void 0, void 0, function* () {
111
- // @ts-ignore
112
- return this._model.updateMany({ _id: { $in: ids } }, { $set: body });
113
- });
114
- }
115
- deleteMany(ids) {
116
- return __awaiter(this, void 0, void 0, function* () {
117
- // @ts-ignore
118
- return this._model.deleteMany({ _id: { $in: ids } });
119
- });
120
- }
121
- subCreate(sub, query, body) {
122
- return __awaiter(this, void 0, void 0, function* () {
123
- if (!('_id' in body)) {
124
- body['_id'] = new mongoose_1.Types.ObjectId();
125
- }
126
- yield this._model.updateOne(Object.assign({}, query.root),
127
- // @ts-ignore
128
- { $push: { [sub]: body } });
129
- return this.subFindOne(sub, query, { _id: body._id });
130
- });
131
- }
132
- subSearch(sub, query, rootQuery) {
133
- return __awaiter(this, void 0, void 0, function* () {
134
- const subQuery = query.createQuery({ noRoot: true });
135
- const populate = query.populate;
136
- const limit = query.limit;
137
- const skip = query.skip;
138
- const $lookups = this.queryModel.getSubPopulateLookup(query, sub);
139
- const { $filter, $match } = this.queryModel.getSubFilterConditionAndMatch(subQuery, sub);
140
- const pipeline = [
141
- { $match: rootQuery },
142
- {
143
- $project: {
144
- [sub]: { $filter },
145
- count: { $size: `$${sub}` }
146
- }
147
- },
148
- { $unwind: `$${sub}` },
149
- ...(skip ? [{ $skip: skip }] : []),
150
- ...(limit ? [{ $limit: limit }] : []),
151
- ...$lookups
152
- ];
153
- if (Object.keys($match).length > 0) {
154
- for (const key in $match) {
155
- if ($match.hasOwnProperty(key)) {
156
- const [path, pathKey] = key.split('.');
157
- if (pathKey && populate.some(pop => pop.path === path)) {
158
- $match[`${path}.${pathKey}`] = QueryModel_1.toValidTextRegexp($match[key]);
159
- }
160
- else if (typeof $match[key] === 'string') {
161
- $match[`${sub}.${path}`] = QueryModel_1.toValidTextRegexp($match[key]);
162
- }
163
- else {
164
- $match[`${sub}.${path}`] = $match[key];
165
- }
166
- delete $match[key];
167
- }
168
- }
169
- pipeline.push({ $match });
170
- }
171
- const arr = yield this._model.aggregate(pipeline);
172
- return arr.reduce((obj, item) => {
173
- const data = Object.assign({}, item[sub]);
174
- for (const key in item) {
175
- if (!['_id', 'count', 'skip', 'limit', sub].includes(key)) {
176
- data[key] = Array.isArray(item[key]) ? item[key][0] : item[key];
177
- }
178
- }
179
- return {
180
- count: item.count,
181
- data: [...obj.data, data],
182
- skip: obj.skip,
183
- limit: obj.limit
184
- };
185
- }, { count: 0, data: [], skip, limit });
186
- });
187
- }
188
- subFindOne(sub, query, subQuery) {
189
- return __awaiter(this, void 0, void 0, function* () {
190
- const subDoc = yield this._model.findOne(Object.assign(Object.assign({}, query.root), { [sub]: { $elemMatch: subQuery } }), { [`${sub}.$`]: 1 });
191
- return this.getSubDocument(sub, subDoc);
192
- });
193
- }
194
- subFindOneAndUpdate(sub, query, subQuery, body) {
195
- return __awaiter(this, void 0, void 0, function* () {
196
- const $set = {};
197
- for (const key in body) {
198
- $set[`${sub}.$.${key}`] = body[key];
199
- }
200
- yield this._model.updateOne(Object.assign(Object.assign({}, query.root), { [sub]: { $elemMatch: subQuery } }),
201
- // @ts-ignore
202
- { $set });
203
- return this.subFindOne(sub, query, subQuery);
204
- });
205
- }
206
- subDelete(sub, query, subQuery) {
207
- return __awaiter(this, void 0, void 0, function* () {
208
- const subDoc = yield this.subFindOne(sub, query, subQuery);
209
- if (!subDoc)
210
- return null;
211
- yield this._model.updateOne(Object.assign(Object.assign({}, query.root), { [sub]: { $elemMatch: subQuery } }),
212
- // @ts-ignore
213
- { $pull: { [sub]: { _id: subDoc._id } } });
214
- return subDoc;
215
- });
216
- }
217
- subPopulate(req, subDoc, sub, type) {
218
- return __awaiter(this, void 0, void 0, function* () {
219
- const populates = type ? this.getSubPopulate(sub, type) : [];
220
- populates.push(...this.queryModel.getValidPopulate(req.mongooseQuery.populate, sub));
221
- return this.populateDoc(subDoc, populates, sub);
222
- });
223
- }
224
- getSubPopulate(sub, type) {
225
- const subs = this.queryModel.subs;
226
- if (!subs)
227
- return [];
228
- const docSub = subs.find(x => x.sub === sub);
229
- if (!docSub)
230
- return [];
231
- return docSub.populate.filter(x => x[type] || x.onAll);
232
- }
233
- getSubDocument(key, doc) {
234
- if (!doc)
235
- return null;
236
- const array = doc[key];
237
- if (array && Array.isArray(array)) {
238
- // @ts-ignore
239
- return array[0] || null;
240
- }
241
- return null;
242
- }
243
- validateBody(body) {
244
- const invalidFields = [];
245
- if (this.queryModel.publicFields) {
246
- for (const key in body) {
247
- if (body.hasOwnProperty(key)) {
248
- if (!this.queryModel.publicFields.includes(key)) {
249
- invalidFields.push(key);
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
+ }