c2-mongoose 2.1.208 → 2.1.209

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.
@@ -12,7 +12,7 @@ declare abstract class SearchFlow {
12
12
  orderBy: string;
13
13
  page: number;
14
14
  limit: number;
15
- select: string | string[];
15
+ select: string[];
16
16
  populate: any;
17
17
  filters: any;
18
18
  sort: any;
@@ -54,6 +54,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
54
54
  var mongoose_1 = require("mongoose");
55
55
  var Utils_1 = require("../utils/Utils");
56
56
  var moment_timezone_1 = __importDefault(require("moment-timezone"));
57
+ var BuildSelectSingleFlowItem_1 = __importDefault(require("./item/BuildSelectSingleFlowItem"));
57
58
  var SearchFlow = /** @class */ (function () {
58
59
  function SearchFlow(params) {
59
60
  // Incluir as propriedades adicionais de 'params' como propriedades da classe
@@ -179,7 +180,7 @@ var SearchFlow = /** @class */ (function () {
179
180
  }
180
181
  stagesItems.push({ $match: this.filters });
181
182
  if ((0, Utils_1.isNotEmpty)(this.projection)) {
182
- stagesItems.push({ $project: this.projection });
183
+ stagesItems.push({ $project: BuildSelectSingleFlowItem_1.default.build(model, this.select) });
183
184
  }
184
185
  stagesItems.push({ $sort: this.sort });
185
186
  stagesMetadata = [];
@@ -242,7 +243,7 @@ var SearchFlow = /** @class */ (function () {
242
243
  }
243
244
  stagesItems.push({ $match: this.filters });
244
245
  if ((0, Utils_1.isNotEmpty)(this.projection)) {
245
- stagesItems.push({ $project: this.projection });
246
+ stagesItems.push({ $project: BuildSelectSingleFlowItem_1.default.build(model, this.select) });
246
247
  }
247
248
  stagesItems.push({ $sort: this.sort });
248
249
  stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 });
@@ -0,0 +1,7 @@
1
+ import mongoose from "mongoose";
2
+ declare class BuildSelectSingleFlowItem {
3
+ build(model: mongoose.Model<any>, selects: string[]): any;
4
+ buildProjectionNested(model: mongoose.Model<any>, projectCurrent: any, field: string, include: boolean): any;
5
+ }
6
+ declare const _default: BuildSelectSingleFlowItem;
7
+ export default _default;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ var mongoose_1 = __importDefault(require("mongoose"));
18
+ var BuildSelectSingleFlowItem = /** @class */ (function () {
19
+ function BuildSelectSingleFlowItem() {
20
+ }
21
+ BuildSelectSingleFlowItem.prototype.build = function (model, selects) {
22
+ var projection = {};
23
+ for (var _i = 0, selects_1 = selects; _i < selects_1.length; _i++) {
24
+ var field = selects_1[_i];
25
+ var include = field.startsWith("-") ? false : true;
26
+ field = field.replace("-", "");
27
+ projection = __assign(__assign({}, projection), this.buildProjectionNested(model, projection, field, include));
28
+ }
29
+ return projection;
30
+ };
31
+ BuildSelectSingleFlowItem.prototype.buildProjectionNested = function (model, projectCurrent, field, include) {
32
+ var _a = field.split('.'), first = _a[0], rest = _a.slice(1);
33
+ if (first !== undefined && rest !== undefined) {
34
+ var campoInfo = model.schema.path(first);
35
+ // Verifique se o tipo do campo é mongoose.Types.ObjectId
36
+ if (campoInfo instanceof mongoose_1.default.Schema.Types.ObjectId) {
37
+ console.log('O campo é um Types.ObjectId');
38
+ return undefined;
39
+ }
40
+ }
41
+ if (rest.length === 0) {
42
+ projectCurrent[first] = include ? 1 : 0;
43
+ return projectCurrent;
44
+ }
45
+ projectCurrent[first] = __assign(__assign({}, projectCurrent[first]), this.buildProjectionNested(model, {}, rest.join("."), include));
46
+ return projectCurrent;
47
+ };
48
+ return BuildSelectSingleFlowItem;
49
+ }());
50
+ exports.default = new BuildSelectSingleFlowItem;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.208",
3
+ "version": "2.1.209",
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",
@@ -1,8 +1,9 @@
1
1
  // import moment from "moment"
2
- import mongoose, { ClientSession, PopulateOption, PopulateOptions, Types } from "mongoose"
2
+ import mongoose, { ClientSession, PopulateOption, PopulateOptions, Types, model } from "mongoose"
3
3
  import { SearchOptions, SearchResponse } from "../types/SearchResponse"
4
4
  import { isEmpty, isNotEmpty } from "../utils/Utils"
5
5
  import moment from "moment-timezone"
6
+ import BuildSelectSingleFlowItem from "./item/BuildSelectSingleFlowItem"
6
7
 
7
8
  interface IPopulate {
8
9
  path: string,
@@ -16,7 +17,7 @@ abstract class SearchFlow {
16
17
  orderBy: string
17
18
  page: number
18
19
  limit: number
19
- select: string | string[]
20
+ select: string[]
20
21
  populate: any
21
22
  filters: any
22
23
  sort: any
@@ -168,7 +169,7 @@ abstract class SearchFlow {
168
169
  stagesItems.push({ $match: this.filters })
169
170
 
170
171
  if (isNotEmpty(this.projection)) {
171
- stagesItems.push({ $project: this.projection })
172
+ stagesItems.push({ $project: BuildSelectSingleFlowItem.build(model, this.select) })
172
173
  }
173
174
 
174
175
  stagesItems.push({ $sort: this.sort })
@@ -203,7 +204,7 @@ abstract class SearchFlow {
203
204
 
204
205
  if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
205
206
  for (var populate of this.populate) {
206
- result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions )
207
+ result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions)
207
208
  }
208
209
  }
209
210
 
@@ -225,7 +226,7 @@ abstract class SearchFlow {
225
226
  stagesItems.push({ $match: this.filters })
226
227
 
227
228
  if (isNotEmpty(this.projection)) {
228
- stagesItems.push({ $project: this.projection })
229
+ stagesItems.push({ $project: BuildSelectSingleFlowItem.build(model, this.select) })
229
230
  }
230
231
 
231
232
  stagesItems.push({ $sort: this.sort })
@@ -244,10 +245,10 @@ abstract class SearchFlow {
244
245
  stagesPaging.push({ $match: this.filters })
245
246
  stagesPaging.push({ $count: "total" })
246
247
  stagesPaging.push(
247
- {
248
- $addFields: {
249
- page: this.page || 1,
250
- limit: this.limit,
248
+ {
249
+ $addFields: {
250
+ page: this.page || 1,
251
+ limit: this.limit,
251
252
  totalPages: {
252
253
  $cond: {
253
254
  if: { $eq: ['$total', 0] },
@@ -255,9 +256,9 @@ abstract class SearchFlow {
255
256
  else: { $ceil: { $divide: ['$total', this.limit] } }
256
257
  }
257
258
  },
258
- startIndex: { $subtract: [ { $multiply: [this.page, this.limit] }, this.limit - 1 ] },
259
+ startIndex: { $subtract: [{ $multiply: [this.page, this.limit] }, this.limit - 1] },
259
260
  endIndex: { $multiply: [this.page, this.limit] }
260
- }
261
+ }
261
262
  })
262
263
 
263
264
  let stagesMetadata: any[] = []
@@ -293,7 +294,7 @@ abstract class SearchFlow {
293
294
 
294
295
  if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
295
296
  for (var populate of this.populate) {
296
- result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions )
297
+ result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions)
297
298
  }
298
299
  }
299
300
 
@@ -487,7 +488,7 @@ abstract class SearchFlow {
487
488
  condition[fieldName] = { $exists: value as boolean }
488
489
  filters.$and.push(condition)
489
490
  }
490
-
491
+
491
492
  else {
492
493
  if (!Array.isArray(value)) {
493
494
  condition[key] = value
@@ -0,0 +1,47 @@
1
+ import mongoose from "mongoose"
2
+
3
+ class BuildSelectSingleFlowItem {
4
+
5
+ build(model: mongoose.Model<any>, selects: string[]) {
6
+
7
+ let projection: any = {}
8
+ for (var field of selects) {
9
+ let include = field.startsWith("-") ? false : true
10
+ field = field.replace("-", "")
11
+ projection = {
12
+ ...projection,
13
+ ...this.buildProjectionNested(model, projection, field, include)
14
+ }
15
+
16
+ }
17
+ return projection;
18
+ }
19
+
20
+ buildProjectionNested(model: mongoose.Model<any>, projectCurrent: any, field: string, include: boolean) {
21
+ let [first, ...rest] = field.split('.')
22
+
23
+ if (first !== undefined && rest !== undefined){
24
+
25
+ const campoInfo = model.schema.path(first);
26
+
27
+ // Verifique se o tipo do campo é mongoose.Types.ObjectId
28
+ if (campoInfo instanceof mongoose.Schema.Types.ObjectId) {
29
+ console.log('O campo é um Types.ObjectId');
30
+ return undefined
31
+ }
32
+ }
33
+
34
+ if (rest.length === 0) {
35
+ projectCurrent[first] = include ? 1 : 0
36
+ return projectCurrent
37
+ }
38
+
39
+ projectCurrent[first] = {
40
+ ...projectCurrent[first],
41
+ ...this.buildProjectionNested(model, {}, rest.join("."), include)
42
+ }
43
+ return projectCurrent
44
+ }
45
+ }
46
+
47
+ export default new BuildSelectSingleFlowItem