c2-mongoose 2.1.40 → 2.1.42

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 class CrudFlow<D> {
12
12
  update(id: string, data: D, session?: any): Promise<D>;
13
13
  updateAny(id: string, data: any, session?: any): Promise<D>;
14
14
  updateByModel(filter: any, data: any, params: any): Promise<D>;
15
- find(): Promise<SearchResponse<D>>;
15
+ find(...pipeline: any): Promise<SearchResponse<D>>;
16
16
  getOne(model: D, params?: any): Promise<D>;
17
17
  get(id: string, pop?: string, sel?: string, session?: ClientSession): Promise<D>;
18
18
  getById(id: string, session?: ClientSession): Promise<Partial<D>>;
@@ -124,10 +124,14 @@ var CrudFlow = /** @class */ (function () {
124
124
  });
125
125
  };
126
126
  CrudFlow.prototype.find = function () {
127
+ var pipeline = [];
128
+ for (var _i = 0; _i < arguments.length; _i++) {
129
+ pipeline[_i] = arguments[_i];
130
+ }
127
131
  return __awaiter(this, void 0, void 0, function () {
128
132
  return __generator(this, function (_a) {
129
133
  switch (_a.label) {
130
- case 0: return [4 /*yield*/, this.search.search(this.repository)];
134
+ case 0: return [4 /*yield*/, this.search.search(this.repository, pipeline)];
131
135
  case 1: return [2 /*return*/, _a.sent()];
132
136
  }
133
137
  });
@@ -9,14 +9,22 @@ declare abstract class SearchFlow {
9
9
  select: string;
10
10
  populate: any;
11
11
  filters: any;
12
+ sort: any;
12
13
  pageable: boolean;
13
14
  constructor(params: any);
14
15
  buildPopulate(): any;
15
16
  buildPath(target: string, nested?: string): any;
16
17
  buildOrdenation(): any;
17
18
  diacriticSensitiveRegex(string?: string): string;
18
- search(model: mongoose.Model<any>): Promise<SearchResponse<any>>;
19
- private searchPageable;
19
+ searchPageable(model: mongoose.Model<any>, ...pipeline: any): Promise<any>;
20
+ search(model: mongoose.Model<any>, ...pipeline: any): Promise<SearchResponse<any>>;
21
+ /**
22
+ *
23
+ * @param model
24
+ * @deprecated
25
+ * @returns
26
+ */
27
+ private searchPageableOld;
20
28
  private searchNoPageable;
21
29
  private result;
22
30
  count(model: mongoose.Model<any>): Promise<number>;
@@ -56,7 +56,13 @@ var Utils_1 = require("../utils/Utils");
56
56
  var SearchFlow = /** @class */ (function () {
57
57
  function SearchFlow(params) {
58
58
  var _this = this;
59
- this.searchPageable = function (model) { return __awaiter(_this, void 0, void 0, function () {
59
+ /**
60
+ *
61
+ * @param model
62
+ * @deprecated
63
+ * @returns
64
+ */
65
+ this.searchPageableOld = function (model) { return __awaiter(_this, void 0, void 0, function () {
60
66
  var sort, items;
61
67
  return __generator(this, function (_a) {
62
68
  switch (_a.label) {
@@ -86,6 +92,7 @@ var SearchFlow = /** @class */ (function () {
86
92
  this.limit = Number(params.limit || 25);
87
93
  this.pageable = params.pageable == false || params.pageable == 'false' ? false : true;
88
94
  this.buildPopulate();
95
+ this.buildOrdenation();
89
96
  }
90
97
  SearchFlow.prototype.buildPopulate = function () {
91
98
  if ((0, Utils_1.isEmpty)(this.populate)) {
@@ -116,6 +123,7 @@ var SearchFlow = /** @class */ (function () {
116
123
  var order = {};
117
124
  this.orderBy = this.orderBy || "_id";
118
125
  order[this.orderBy] = this.order === "desc" ? -1 : 1;
126
+ this.sort = order;
119
127
  return order;
120
128
  };
121
129
  SearchFlow.prototype.diacriticSensitiveRegex = function (string) {
@@ -128,16 +136,62 @@ var SearchFlow = /** @class */ (function () {
128
136
  .replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
129
137
  .replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
130
138
  };
139
+ SearchFlow.prototype.searchPageable = function (model) {
140
+ var pipeline = [];
141
+ for (var _i = 1; _i < arguments.length; _i++) {
142
+ pipeline[_i - 1] = arguments[_i];
143
+ }
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ var result;
146
+ return __generator(this, function (_a) {
147
+ switch (_a.label) {
148
+ case 0: return [4 /*yield*/, model.aggregate([
149
+ {
150
+ $facet: {
151
+ items: [
152
+ { $match: this.filters },
153
+ { $sort: this.sort },
154
+ { $skip: (this.page - 1) * this.limit },
155
+ { $limit: this.limit },
156
+ pipeline
157
+ ],
158
+ paging: [
159
+ { $match: this.filters },
160
+ { $count: "total" },
161
+ {
162
+ $addFields: {
163
+ page: this.page,
164
+ limit: this.limit
165
+ }
166
+ }
167
+ ]
168
+ }
169
+ }
170
+ ])];
171
+ case 1:
172
+ result = _a.sent();
173
+ return [2 /*return*/, result[0]];
174
+ }
175
+ });
176
+ });
177
+ };
131
178
  SearchFlow.prototype.search = function (model) {
179
+ var pipeline = [];
180
+ for (var _i = 1; _i < arguments.length; _i++) {
181
+ pipeline[_i - 1] = arguments[_i];
182
+ }
132
183
  return __awaiter(this, void 0, void 0, function () {
133
184
  return __generator(this, function (_a) {
134
185
  switch (_a.label) {
135
186
  case 0:
136
- if (!(this.pageable == true)) return [3 /*break*/, 2];
137
- return [4 /*yield*/, this.searchPageable(model)];
187
+ if (!(this.pageable == true)) return [3 /*break*/, 4];
188
+ if (!pipeline) return [3 /*break*/, 2];
189
+ return [4 /*yield*/, this.searchPageable(model, pipeline)];
138
190
  case 1: return [2 /*return*/, _a.sent()];
139
- case 2: return [4 /*yield*/, this.searchNoPageable(model)];
191
+ case 2: return [4 /*yield*/, this.searchPageableOld(model)];
140
192
  case 3: return [2 /*return*/, _a.sent()];
193
+ case 4: return [4 /*yield*/, this.searchNoPageable(model)];
194
+ case 5: return [2 /*return*/, _a.sent()];
141
195
  }
142
196
  });
143
197
  });
@@ -299,6 +353,12 @@ var SearchFlow = /** @class */ (function () {
299
353
  if (['pageable', 'order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
300
354
  return;
301
355
  }
356
+ if (value === 'true') {
357
+ value = true;
358
+ }
359
+ if (value === 'false') {
360
+ value = false;
361
+ }
302
362
  if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
303
363
  var fieldName = key.replace('Range', '');
304
364
  var values = value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.40",
3
+ "version": "2.1.42",
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",
@@ -44,8 +44,8 @@ class CrudFlow<D> {
44
44
  return dataAfter as D
45
45
  }
46
46
 
47
- public async find(): Promise<SearchResponse<D>> {
48
- return await this.search.search(this.repository)
47
+ public async find(...pipeline: any): Promise<SearchResponse<D>> {
48
+ return await this.search.search(this.repository, pipeline)
49
49
  }
50
50
 
51
51
  public async getOne(model: D, params: any = {}): Promise<D> {
@@ -13,6 +13,7 @@ abstract class SearchFlow {
13
13
  select: string
14
14
  populate: any
15
15
  filters: any
16
+ sort: any
16
17
  pageable: boolean
17
18
 
18
19
  constructor(params: any) {
@@ -25,6 +26,7 @@ abstract class SearchFlow {
25
26
  this.limit = Number(params.limit || 25)
26
27
  this.pageable = params.pageable == false || params.pageable == 'false' ? false : true
27
28
  this.buildPopulate()
29
+ this.buildOrdenation()
28
30
  }
29
31
 
30
32
  public buildPopulate(): any {
@@ -58,6 +60,7 @@ abstract class SearchFlow {
58
60
  let order = {} as any
59
61
  this.orderBy = this.orderBy || "_id"
60
62
  order[this.orderBy] = this.order === "desc" ? -1 : 1
63
+ this.sort = order
61
64
  return order
62
65
  }
63
66
 
@@ -71,15 +74,54 @@ abstract class SearchFlow {
71
74
  .replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]')
72
75
  }
73
76
 
74
- public async search(model: mongoose.Model<any>): Promise<SearchResponse<any>> {
77
+ async searchPageable(model: mongoose.Model<any>, ...pipeline: any) {
78
+ const result = await model.aggregate(
79
+ [
80
+ {
81
+ $facet: {
82
+ items: [
83
+ { $match: this.filters },
84
+ { $sort: this.sort },
85
+ { $skip: (this.page - 1) * this.limit },
86
+ { $limit: this.limit },
87
+ pipeline
88
+ ],
89
+ paging: [
90
+ { $match: this.filters },
91
+ { $count: "total" },
92
+ {
93
+ $addFields:
94
+ {
95
+ page: this.page,
96
+ limit: this.limit
97
+ }
98
+ }
99
+ ]
100
+ }
101
+ }
102
+ ]
103
+ )
104
+ return result[0]
105
+ }
106
+
107
+ public async search(model: mongoose.Model<any>, ...pipeline: any): Promise<SearchResponse<any>> {
75
108
  if (this.pageable == true) {
76
- return await this.searchPageable(model)
109
+ if (pipeline) {
110
+ return await this.searchPageable(model, pipeline)
111
+ }
112
+ return await this.searchPageableOld(model)
77
113
  }
78
114
 
79
115
  return await this.searchNoPageable(model)
80
116
  }
81
117
 
82
- private searchPageable = async (model: mongoose.Model<any>): Promise<SearchResponse<any>> => {
118
+ /**
119
+ *
120
+ * @param model
121
+ * @deprecated
122
+ * @returns
123
+ */
124
+ private searchPageableOld = async (model: mongoose.Model<any>): Promise<SearchResponse<any>> => {
83
125
  const sort = this.buildOrdenation()
84
126
  var items = await model
85
127
  .find(this.filters, this.select)
@@ -209,6 +251,14 @@ abstract class SearchFlow {
209
251
  return
210
252
  }
211
253
 
254
+ if (value === 'true') {
255
+ value = true
256
+ }
257
+
258
+ if (value === 'false') {
259
+ value = false
260
+ }
261
+
212
262
  if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
213
263
  var fieldName = key.replace('Range', '')
214
264
  var values = value as any