c2-mongoose 2.1.277 → 2.1.279

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.
@@ -1,5 +1,10 @@
1
1
  import mongoose, { Schema } from "mongoose";
2
2
  import { SearchOptions, SearchResponse } from "../types/SearchResponse";
3
+ export interface IPopulate2 {
4
+ path: string;
5
+ select: string;
6
+ populate: IPopulate2[];
7
+ }
3
8
  export interface IPopulate {
4
9
  [key: string]: any;
5
10
  path: string;
@@ -24,6 +29,7 @@ declare class SearcherFlow<D> {
24
29
  search(options: SearchOptions): Promise<SearchResponse<D>>;
25
30
  transformObject(originalObject: any): any;
26
31
  extractFieldsOfType(schema: Schema, typing?: string, parent?: string): string[];
32
+ buildCondition(key: string, value: any): any;
27
33
  buildDefaultFilters(objectSearch: any): any;
28
34
  isValidObjectId(value: string): boolean;
29
35
  buildOrdenation(): any;
@@ -167,7 +167,7 @@ var SearcherFlow = /** @class */ (function () {
167
167
  result = _d.sent();
168
168
  if (!(0, Utils_1.isNotEmpty)(this.populate)) return [3 /*break*/, 3];
169
169
  _c = result[0];
170
- return [4 /*yield*/, this.model.populate(result[0].items, BuildPopulateSingleFlowItem_1.default.buildPopulate(this.model, this.populate, this.select))];
170
+ return [4 /*yield*/, this.model.populate(result[0].items, BuildPopulateSingleFlowItem_1.default.buildPopulate2(this.populate))];
171
171
  case 2:
172
172
  _c.items = _d.sent();
173
173
  _d.label = 3;
@@ -211,17 +211,105 @@ var SearcherFlow = /** @class */ (function () {
211
211
  });
212
212
  return fieldsTyped;
213
213
  };
214
+ SearcherFlow.prototype.buildCondition = function (key, value) {
215
+ var _this = this;
216
+ var _a, _b, _c;
217
+ var condition = {};
218
+ if (key.endsWith('DateRange')) {
219
+ var fieldName = key.replace('Range', '');
220
+ var values = value;
221
+ if ((0, Utils_1.isNotEmpty)(values[0])) {
222
+ var momentValue = (0, moment_1.default)(values[0]);
223
+ momentValue.hour(0);
224
+ momentValue.minute(0);
225
+ momentValue.second(0);
226
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
227
+ }
228
+ if ((0, Utils_1.isNotEmpty)(values[1])) {
229
+ var momentValue = (0, moment_1.default)(values[1]);
230
+ momentValue.hour(23);
231
+ momentValue.minute(59);
232
+ momentValue.second(59);
233
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
234
+ }
235
+ }
236
+ else if (key.endsWith('DateTimeRange')) {
237
+ var fieldName = key.replace('Range', '');
238
+ var values = value;
239
+ if ((0, Utils_1.isNotEmpty)(values[0])) {
240
+ var momentValue = (0, moment_1.default)(values[0]);
241
+ momentValue.hour(0);
242
+ momentValue.minute(0);
243
+ momentValue.second(0);
244
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
245
+ }
246
+ if ((0, Utils_1.isNotEmpty)(values[1])) {
247
+ var momentValue = (0, moment_1.default)(values[1]);
248
+ momentValue.hour(23);
249
+ momentValue.minute(59);
250
+ momentValue.second(59);
251
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
252
+ }
253
+ }
254
+ else if (key.endsWith('Like')) {
255
+ var fieldName = key.replace('Like', '');
256
+ condition[fieldName] = this.buildRegex(value); //{ $regex: value as any, $options: 'i' }
257
+ }
258
+ else if (key.endsWith('Exists')) {
259
+ var fieldName = key.replace('Exists', '');
260
+ condition[fieldName] = { $exists: value };
261
+ }
262
+ else {
263
+ if (((_c = (_b = (_a = this.model) === null || _a === void 0 ? void 0 : _a.schema) === null || _b === void 0 ? void 0 : _b.paths[key]) === null || _c === void 0 ? void 0 : _c.instance) === 'Array') {
264
+ if (!Array.isArray(value)) {
265
+ value = [value];
266
+ }
267
+ value === null || value === void 0 ? void 0 : value.forEach(function (v) {
268
+ if (typeof v === 'string' && _this.isValidObjectId(v)) {
269
+ v = new mongoose_1.Types.ObjectId(v);
270
+ }
271
+ });
272
+ if (key.startsWith("notIn")) {
273
+ key = key.replace("notIn", "");
274
+ condition[key] = { $nin: value };
275
+ }
276
+ else {
277
+ condition[key] = { $in: value };
278
+ }
279
+ }
280
+ else if (Array.isArray(value)) {
281
+ var arr = [];
282
+ for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
283
+ var val = value_1[_i];
284
+ if (typeof val === 'string' && this.isValidObjectId(val)) {
285
+ val = new mongoose_1.Types.ObjectId(val);
286
+ }
287
+ arr.push(val);
288
+ }
289
+ if (key.startsWith("notIn")) {
290
+ key = key.replace("notIn", "");
291
+ condition[key] = { $nin: arr };
292
+ }
293
+ else {
294
+ condition[key] = { $in: arr };
295
+ }
296
+ }
297
+ else {
298
+ condition[key] = value;
299
+ }
300
+ }
301
+ return condition;
302
+ };
214
303
  SearcherFlow.prototype.buildDefaultFilters = function (objectSearch) {
215
304
  var _this = this;
216
305
  // Recuperar todos os campos do tipo String
217
306
  var camposString = (0, Utils_1.isEmpty)(this.searchTextFields) ? this.extractFieldsOfType(this.model.schema, 'String') : this.searchTextFields;
218
307
  var filters = { $and: [] };
219
308
  Object.entries(objectSearch).forEach(function (_a) {
220
- var _b, _c, _d;
221
309
  var key = _a[0], value = _a[1];
222
310
  if ((0, Utils_1.isNotEmpty)(value) || value === null) {
223
- var condition = {};
224
311
  if ([
312
+ 'or',
225
313
  'filters',
226
314
  'withTotalizers',
227
315
  'onlyMetadata',
@@ -252,94 +340,8 @@ var SearcherFlow = /** @class */ (function () {
252
340
  if (value === 'false') {
253
341
  value = false;
254
342
  }
255
- if (key.endsWith('DateRange')) {
256
- var fieldName = key.replace('Range', '');
257
- var values = value;
258
- if ((0, Utils_1.isNotEmpty)(values[0])) {
259
- var momentValue = (0, moment_1.default)(values[0]);
260
- momentValue.hour(0);
261
- momentValue.minute(0);
262
- momentValue.second(0);
263
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
264
- }
265
- if ((0, Utils_1.isNotEmpty)(values[1])) {
266
- var momentValue = (0, moment_1.default)(values[1]);
267
- momentValue.hour(23);
268
- momentValue.minute(59);
269
- momentValue.second(59);
270
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
271
- }
272
- filters.$and.push(condition);
273
- }
274
- else if (key.endsWith('DateTimeRange')) {
275
- var fieldName = key.replace('Range', '');
276
- var values = value;
277
- if ((0, Utils_1.isNotEmpty)(values[0])) {
278
- var momentValue = (0, moment_1.default)(values[0]);
279
- momentValue.hour(0);
280
- momentValue.minute(0);
281
- momentValue.second(0);
282
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
283
- }
284
- if ((0, Utils_1.isNotEmpty)(values[1])) {
285
- var momentValue = (0, moment_1.default)(values[1]);
286
- momentValue.hour(23);
287
- momentValue.minute(59);
288
- momentValue.second(59);
289
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
290
- }
291
- filters.$and.push(condition);
292
- }
293
- else if (key.endsWith('Like')) {
294
- var fieldName = key.replace('Like', '');
295
- condition[fieldName] = _this.buildRegex(value); //{ $regex: value as any, $options: 'i' }
296
- filters.$and.push(condition);
297
- }
298
- else if (key.endsWith('Exists')) {
299
- var fieldName = key.replace('Exists', '');
300
- condition[fieldName] = { $exists: value };
301
- filters.$and.push(condition);
302
- }
303
- else {
304
- if (((_d = (_c = (_b = _this.model) === null || _b === void 0 ? void 0 : _b.schema) === null || _c === void 0 ? void 0 : _c.paths[key]) === null || _d === void 0 ? void 0 : _d.instance) === 'Array') {
305
- if (!Array.isArray(value)) {
306
- value = [value];
307
- }
308
- value === null || value === void 0 ? void 0 : value.forEach(function (v) {
309
- if (typeof v === 'string' && _this.isValidObjectId(v)) {
310
- v = new mongoose_1.Types.ObjectId(v);
311
- }
312
- });
313
- if (key.startsWith("notIn")) {
314
- key = key.replace("notIn", "");
315
- condition[key] = { $nin: value };
316
- }
317
- else {
318
- condition[key] = { $in: value };
319
- }
320
- }
321
- else if (Array.isArray(value)) {
322
- var arr = [];
323
- for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
324
- var val = value_1[_i];
325
- if (typeof val === 'string' && _this.isValidObjectId(val)) {
326
- val = new mongoose_1.Types.ObjectId(val);
327
- }
328
- arr.push(val);
329
- }
330
- if (key.startsWith("notIn")) {
331
- key = key.replace("notIn", "");
332
- condition[key] = { $nin: arr };
333
- }
334
- else {
335
- condition[key] = { $in: arr };
336
- }
337
- }
338
- else {
339
- condition[key] = value;
340
- }
341
- filters.$and.push(condition);
342
- }
343
+ var condition = _this.buildCondition(key, value);
344
+ filters.$and.push(condition);
343
345
  }
344
346
  });
345
347
  if ((0, Utils_1.isNotEmpty)(this.searchText) && (0, Utils_1.isNotEmpty)(camposString)) {
@@ -355,6 +357,23 @@ var SearcherFlow = /** @class */ (function () {
355
357
  }
356
358
  if (filters.$and.length === 0)
357
359
  delete filters['$and'];
360
+ Object.entries(objectSearch === null || objectSearch === void 0 ? void 0 : objectSearch.or).forEach(function (_a) {
361
+ var key = _a[0], value = _a[1];
362
+ console.log(key, value);
363
+ if ((0, Utils_1.isEmpty)(value))
364
+ return;
365
+ if (typeof value === 'string' && _this.isValidObjectId(value) && key !== 'owner') {
366
+ value = new mongoose_1.Types.ObjectId(value);
367
+ }
368
+ if (value === 'true') {
369
+ value = true;
370
+ }
371
+ if (value === 'false') {
372
+ value = false;
373
+ }
374
+ var condition = _this.buildCondition(key, value);
375
+ filters.$or.push(condition);
376
+ });
358
377
  return filters;
359
378
  };
360
379
  SearcherFlow.prototype.isValidObjectId = function (value) {
@@ -1,6 +1,8 @@
1
1
  import mongoose from "mongoose";
2
- import { IPopulate } from "../SearcherFlow";
2
+ import { IPopulate, IPopulate2 } from "../SearcherFlow";
3
3
  declare class BuildPopulateSingleFlowItem {
4
+ buildPopulate2(paths: string[]): IPopulate2[];
5
+ addPath2(root: IPopulate2[], parts: string[]): void;
4
6
  buildPopulate(model: mongoose.Model<any>, populatesStr: string[], selectsStr: string[]): IPopulate[];
5
7
  buildPath(target: string, nested: string | undefined, populateAcc: IPopulate): IPopulate;
6
8
  }
@@ -15,6 +15,26 @@ var Utils_1 = require("../../utils/Utils");
15
15
  var BuildPopulateSingleFlowItem = /** @class */ (function () {
16
16
  function BuildPopulateSingleFlowItem() {
17
17
  }
18
+ BuildPopulateSingleFlowItem.prototype.buildPopulate2 = function (paths) {
19
+ var _this = this;
20
+ var root = [];
21
+ paths.forEach(function (path) {
22
+ var parts = path.split('.');
23
+ _this.addPath2(root, parts);
24
+ });
25
+ return root;
26
+ };
27
+ BuildPopulateSingleFlowItem.prototype.addPath2 = function (root, parts) {
28
+ if (parts.length === 0)
29
+ return;
30
+ var currentPath = parts[0];
31
+ var existing = root.find(function (item) { return item.path === currentPath; });
32
+ if (!existing) {
33
+ existing = { path: currentPath, populate: [], select: "" };
34
+ root.push(existing);
35
+ }
36
+ this.addPath2(existing.populate, parts.slice(1));
37
+ };
18
38
  BuildPopulateSingleFlowItem.prototype.buildPopulate = function (model, populatesStr, selectsStr) {
19
39
  var populateArray = [];
20
40
  if ((0, Utils_1.isEmpty)(populatesStr)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.277",
3
+ "version": "2.1.279",
4
4
  "description": "Lib to make any search in database mongoose and use as basic crud",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -4,6 +4,13 @@ import { SearchOptions, SearchResponse } from "../types/SearchResponse"
4
4
  import { isEmpty, isNotEmpty } from "../utils/Utils"
5
5
  import BuildPopulateSingleFlowItem from "./item/BuildPopulateSingleFlowItem"
6
6
  import BuildSelectSingleFlowItem from "./item/BuildSelectSingleFlowItem"
7
+
8
+ export interface IPopulate2 {
9
+ path: string
10
+ select: string
11
+ populate: IPopulate2[]
12
+ }
13
+
7
14
  export interface IPopulate {
8
15
  [key: string]: any
9
16
  path: string
@@ -128,7 +135,7 @@ class SearcherFlow<D> {
128
135
 
129
136
 
130
137
  if (isNotEmpty(this.populate)) {
131
- result[0].items = await this.model.populate(result[0].items, BuildPopulateSingleFlowItem.buildPopulate(this.model, this.populate, this.select))
138
+ result[0].items = await this.model.populate(result[0].items, BuildPopulateSingleFlowItem.buildPopulate2(this.populate))
132
139
  }
133
140
 
134
141
  let resultAux = this.transformObject(result[0])
@@ -173,6 +180,105 @@ class SearcherFlow<D> {
173
180
  return fieldsTyped
174
181
  }
175
182
 
183
+ public buildCondition(key: string, value: any) {
184
+ let condition = {} as any
185
+ if (key.endsWith('DateRange')) {
186
+ var fieldName = key.replace('Range', '')
187
+ var values = value as any
188
+ if (isNotEmpty(values[0])) {
189
+ var momentValue = moment(values[0])
190
+ momentValue.hour(0)
191
+ momentValue.minute(0)
192
+ momentValue.second(0)
193
+ condition[fieldName] = {
194
+ ...condition[fieldName],
195
+ $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
196
+ }
197
+ }
198
+
199
+ if (isNotEmpty(values[1])) {
200
+ var momentValue = moment(values[1])
201
+ momentValue.hour(23)
202
+ momentValue.minute(59)
203
+ momentValue.second(59)
204
+ condition[fieldName] = {
205
+ ...condition[fieldName],
206
+ $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
207
+ }
208
+ }
209
+ } else if (key.endsWith('DateTimeRange')) {
210
+ var fieldName = key.replace('Range', '')
211
+ var values = value as any
212
+ if (isNotEmpty(values[0])) {
213
+ var momentValue = moment(values[0])
214
+ momentValue.hour(0)
215
+ momentValue.minute(0)
216
+ momentValue.second(0)
217
+ condition[fieldName] = {
218
+ ...condition[fieldName],
219
+ $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
220
+ }
221
+ }
222
+
223
+ if (isNotEmpty(values[1])) {
224
+ var momentValue = moment(values[1])
225
+ momentValue.hour(23)
226
+ momentValue.minute(59)
227
+ momentValue.second(59)
228
+ condition[fieldName] = {
229
+ ...condition[fieldName],
230
+ $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
231
+ }
232
+ }
233
+ } else if (key.endsWith('Like')) {
234
+ var fieldName = key.replace('Like', '')
235
+ condition[fieldName] = this.buildRegex(value as string) //{ $regex: value as any, $options: 'i' }
236
+ } else if (key.endsWith('Exists')) {
237
+ var fieldName = key.replace('Exists', '')
238
+ condition[fieldName] = { $exists: value as boolean }
239
+ } else {
240
+ if (this.model?.schema?.paths[key]?.instance === 'Array') {
241
+ if (!Array.isArray(value)) {
242
+ value = [value]
243
+ }
244
+
245
+ (value as [])?.forEach((v: any) => {
246
+ if (typeof v === 'string' && this.isValidObjectId(v)) {
247
+ v = new Types.ObjectId(v);
248
+ }
249
+ })
250
+
251
+ if (key.startsWith("notIn")) {
252
+ key = key.replace("notIn", "");
253
+ condition[key] = { $nin: value };
254
+ }
255
+ else {
256
+ condition[key] = { $in: value };
257
+ }
258
+ } else if (Array.isArray(value)) {
259
+ var arr = [];
260
+ for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
261
+ var val = value_1[_i];
262
+ if (typeof val === 'string' && this.isValidObjectId(val)) {
263
+ val = new Types.ObjectId(val);
264
+ }
265
+ arr.push(val);
266
+ }
267
+ if (key.startsWith("notIn")) {
268
+ key = key.replace("notIn", "");
269
+ condition[key] = { $nin: arr };
270
+ }
271
+ else {
272
+ condition[key] = { $in: arr };
273
+ }
274
+ } else {
275
+ condition[key] = value;
276
+ }
277
+ }
278
+
279
+ return condition
280
+ }
281
+
176
282
  public buildDefaultFilters(objectSearch: any) {
177
283
 
178
284
  // Recuperar todos os campos do tipo String
@@ -182,8 +288,8 @@ class SearcherFlow<D> {
182
288
  let filters = { $and: [] } as any
183
289
  Object.entries(objectSearch).forEach(([key, value]) => {
184
290
  if (isNotEmpty(value) || value === null) {
185
- let condition = {} as any
186
291
  if ([
292
+ 'or',
187
293
  'filters',
188
294
  'withTotalizers',
189
295
  'onlyMetadata',
@@ -217,106 +323,8 @@ class SearcherFlow<D> {
217
323
  value = false
218
324
  }
219
325
 
220
- if (key.endsWith('DateRange')) {
221
- var fieldName = key.replace('Range', '')
222
- var values = value as any
223
- if (isNotEmpty(values[0])) {
224
- var momentValue = moment(values[0])
225
- momentValue.hour(0)
226
- momentValue.minute(0)
227
- momentValue.second(0)
228
- condition[fieldName] = {
229
- ...condition[fieldName],
230
- $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
231
- }
232
- }
233
-
234
- if (isNotEmpty(values[1])) {
235
- var momentValue = moment(values[1])
236
- momentValue.hour(23)
237
- momentValue.minute(59)
238
- momentValue.second(59)
239
- condition[fieldName] = {
240
- ...condition[fieldName],
241
- $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
242
- }
243
- }
244
- filters.$and.push(condition)
245
- }else if (key.endsWith('DateTimeRange')) {
246
- var fieldName = key.replace('Range', '')
247
- var values = value as any
248
- if (isNotEmpty(values[0])) {
249
- var momentValue = moment(values[0])
250
- momentValue.hour(0)
251
- momentValue.minute(0)
252
- momentValue.second(0)
253
- condition[fieldName] = {
254
- ...condition[fieldName],
255
- $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
256
- }
257
- }
258
-
259
- if (isNotEmpty(values[1])) {
260
- var momentValue = moment(values[1])
261
- momentValue.hour(23)
262
- momentValue.minute(59)
263
- momentValue.second(59)
264
- condition[fieldName] = {
265
- ...condition[fieldName],
266
- $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
267
- }
268
- }
269
- filters.$and.push(condition)
270
- } else if (key.endsWith('Like')) {
271
- var fieldName = key.replace('Like', '')
272
- condition[fieldName] = this.buildRegex(value as string) //{ $regex: value as any, $options: 'i' }
273
- filters.$and.push(condition)
274
- } else if (key.endsWith('Exists')) {
275
- var fieldName = key.replace('Exists', '')
276
- condition[fieldName] = { $exists: value as boolean }
277
- filters.$and.push(condition)
278
- }
279
-
280
- else {
281
- if (this.model?.schema?.paths[key]?.instance === 'Array') {
282
- if (!Array.isArray(value)) {
283
- value = [value]
284
- }
285
-
286
- (value as [])?.forEach((v: any) => {
287
- if (typeof v === 'string' && this.isValidObjectId(v)) {
288
- v = new Types.ObjectId(v);
289
- }
290
- })
291
-
292
- if (key.startsWith("notIn")) {
293
- key = key.replace("notIn", "");
294
- condition[key] = { $nin: value };
295
- }
296
- else {
297
- condition[key] = { $in: value };
298
- }
299
- } else if (Array.isArray(value)) {
300
- var arr = [];
301
- for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
302
- var val = value_1[_i];
303
- if (typeof val === 'string' && this.isValidObjectId(val)) {
304
- val = new Types.ObjectId(val);
305
- }
306
- arr.push(val);
307
- }
308
- if (key.startsWith("notIn")) {
309
- key = key.replace("notIn", "");
310
- condition[key] = { $nin: arr };
311
- }
312
- else {
313
- condition[key] = { $in: arr };
314
- }
315
- } else {
316
- condition[key] = value;
317
- }
318
- filters.$and.push(condition)
319
- }
326
+ const condition = this.buildCondition(key, value)
327
+ filters.$and.push(condition)
320
328
  }
321
329
  })
322
330
 
@@ -335,6 +343,27 @@ class SearcherFlow<D> {
335
343
  if (filters.$and.length === 0)
336
344
  delete filters['$and']
337
345
 
346
+
347
+ Object.entries(objectSearch?.or).forEach(([key, value]) => {
348
+ console.log(key, value)
349
+ if (isEmpty(value)) return
350
+
351
+ if (typeof value === 'string' && this.isValidObjectId(value as string) && key !== 'owner') {
352
+ value = new Types.ObjectId(value as string)
353
+ }
354
+
355
+ if (value === 'true') {
356
+ value = true
357
+ }
358
+
359
+ if (value === 'false') {
360
+ value = false
361
+ }
362
+
363
+ const condition = this.buildCondition(key, value)
364
+ filters.$or.push(condition)
365
+ })
366
+
338
367
  return filters
339
368
  }
340
369
 
@@ -1,9 +1,36 @@
1
1
  import mongoose from "mongoose";
2
2
  import { isEmpty, isNotEmpty } from "../../utils/Utils";
3
- import { IPopulate } from "../SearcherFlow";
3
+ import { IPopulate, IPopulate2 } from "../SearcherFlow";
4
4
 
5
5
  class BuildPopulateSingleFlowItem {
6
6
 
7
+
8
+ buildPopulate2(paths: string[]): IPopulate2[] {
9
+ const root: IPopulate2[] = [];
10
+
11
+ paths.forEach((path) => {
12
+ const parts = path.split('.');
13
+ this.addPath2(root, parts);
14
+ });
15
+
16
+ return root;
17
+ }
18
+
19
+ addPath2(root: IPopulate2[], parts: string[]) {
20
+ if (parts.length === 0) return;
21
+
22
+ const currentPath = parts[0];
23
+ let existing = root.find((item) => item.path === currentPath) as IPopulate2;
24
+
25
+ if (!existing) {
26
+ existing = { path: currentPath, populate: [], select: "" };
27
+ root.push(existing as any);
28
+ }
29
+
30
+ this.addPath2(existing.populate, parts.slice(1));
31
+ }
32
+
33
+
7
34
  public buildPopulate(model: mongoose.Model<any>, populatesStr: string[], selectsStr: string[]): IPopulate[] {
8
35
  var populateArray = [] as IPopulate[]
9
36
  if (isEmpty(populatesStr)) {