c2-mongoose 2.1.62 → 2.1.64

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.
@@ -15,10 +15,11 @@ declare abstract class SearchFlow {
15
15
  [key: string]: number;
16
16
  } | undefined;
17
17
  constructor(params: any);
18
- buildPopulate(): any;
19
18
  buildProjections(): {
20
19
  [key: string]: number;
21
- };
20
+ } | undefined;
21
+ buildPopulate(): any;
22
+ buildPopulateArray(): void;
22
23
  buildPath(target: string, nested?: string): any;
23
24
  buildOrdenation(): any;
24
25
  buildRegex(searchText: string): RegExp;
@@ -105,10 +105,24 @@ var SearchFlow = /** @class */ (function () {
105
105
  this.buildPopulate();
106
106
  this.buildOrdenation();
107
107
  }
108
+ SearchFlow.prototype.buildProjections = function () {
109
+ if (!Array.isArray(this.select)) {
110
+ return undefined;
111
+ }
112
+ var projection = {};
113
+ for (var _i = 0, _a = this.select; _i < _a.length; _i++) {
114
+ var field = _a[_i];
115
+ projection[field] = 1;
116
+ }
117
+ return projection;
118
+ };
108
119
  SearchFlow.prototype.buildPopulate = function () {
109
120
  if ((0, Utils_1.isEmpty)(this.populate)) {
110
121
  return;
111
122
  }
123
+ if (Array.isArray(this.populate)) {
124
+ return undefined;
125
+ }
112
126
  var propertiesArray = this.populate.toString().split(',');
113
127
  var populates = [];
114
128
  for (var _i = 0, propertiesArray_1 = propertiesArray; _i < propertiesArray_1.length; _i++) {
@@ -119,16 +133,15 @@ var SearchFlow = /** @class */ (function () {
119
133
  }
120
134
  this.populate = populates;
121
135
  };
122
- SearchFlow.prototype.buildProjections = function () {
123
- if (!Array.isArray(this.select)) {
124
- return {};
125
- }
126
- var projection = {};
127
- for (var _i = 0, _a = this.select; _i < _a.length; _i++) {
128
- var field = _a[_i];
129
- projection[field] = 1;
136
+ SearchFlow.prototype.buildPopulateArray = function () {
137
+ var populates = [];
138
+ for (var _i = 0, _a = this.populate; _i < _a.length; _i++) {
139
+ var property = _a[_i];
140
+ var _b = property.split('.'), first = _b[0], rest = _b.slice(1);
141
+ var nested = rest.join('.');
142
+ populates.push(this.buildPath(first, nested));
130
143
  }
131
- return projection;
144
+ this.populate = populates;
132
145
  };
133
146
  SearchFlow.prototype.buildPath = function (target, nested) {
134
147
  if (nested === void 0) { nested = ""; }
@@ -168,7 +181,8 @@ var SearchFlow = /** @class */ (function () {
168
181
  { $match: this.filters },
169
182
  { $sort: this.sort },
170
183
  { $skip: (this.page - 1) * this.limit },
171
- { $limit: this.limit }
184
+ { $limit: this.limit },
185
+ this.projection ? { $project: this.projection } : { $addFields: {} }
172
186
  ], options.pipelines, true);
173
187
  return [4 /*yield*/, model.aggregate([
174
188
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.62",
3
+ "version": "2.1.64",
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",
@@ -31,12 +31,29 @@ abstract class SearchFlow {
31
31
  this.buildOrdenation()
32
32
  }
33
33
 
34
+ buildProjections() {
35
+ if (!Array.isArray(this.select)) {
36
+ return undefined
37
+ }
38
+
39
+ let projection: { [key: string]: number } = {}
40
+ for (var field of this.select) {
41
+ projection[field] = 1
42
+ }
43
+
44
+ return projection
45
+ }
46
+
34
47
  public buildPopulate(): any {
35
48
  if (isEmpty(this.populate)) {
36
49
  return
37
50
  }
38
51
 
39
- var propertiesArray = this.populate.toString().split(',')
52
+ if (Array.isArray(this.populate)) {
53
+ return undefined
54
+ }
55
+
56
+ var propertiesArray = (this.populate as string).toString().split(',')
40
57
  var populates = [] as any
41
58
  for (var property of propertiesArray) {
42
59
  let [first, ...rest] = property.split('.')
@@ -47,17 +64,15 @@ abstract class SearchFlow {
47
64
  this.populate = populates
48
65
  }
49
66
 
50
- buildProjections() {
51
- if (!Array.isArray(this.select)) {
52
- return {}
53
- }
54
-
55
- let projection: { [key: string]: number } = {}
56
- for (var field of this.select) {
57
- projection[field] = 1
67
+ buildPopulateArray() {
68
+ var populates = [] as any
69
+ for (var property of this.populate) {
70
+ let [first, ...rest] = property.split('.')
71
+ let nested = rest.join('.')
72
+ populates.push(this.buildPath(first, nested))
58
73
  }
59
74
 
60
- return projection
75
+ this.populate = populates
61
76
  }
62
77
 
63
78
  public buildPath(target: string, nested: string = "") {
@@ -95,7 +110,7 @@ abstract class SearchFlow {
95
110
  { $sort: this.sort },
96
111
  { $skip: (this.page - 1) * this.limit },
97
112
  { $limit: this.limit },
98
- // { $project: this.projection },
113
+ this.projection ? { $project: this.projection } : { $addFields: {} },
99
114
  ...options.pipelines
100
115
  ]
101
116