c2-mongoose 2.1.40 → 2.1.41

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>;
@@ -46,6 +46,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
49
58
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
59
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
60
  };
@@ -56,7 +65,13 @@ var Utils_1 = require("../utils/Utils");
56
65
  var SearchFlow = /** @class */ (function () {
57
66
  function SearchFlow(params) {
58
67
  var _this = this;
59
- this.searchPageable = function (model) { return __awaiter(_this, void 0, void 0, function () {
68
+ /**
69
+ *
70
+ * @param model
71
+ * @deprecated
72
+ * @returns
73
+ */
74
+ this.searchPageableOld = function (model) { return __awaiter(_this, void 0, void 0, function () {
60
75
  var sort, items;
61
76
  return __generator(this, function (_a) {
62
77
  switch (_a.label) {
@@ -86,6 +101,7 @@ var SearchFlow = /** @class */ (function () {
86
101
  this.limit = Number(params.limit || 25);
87
102
  this.pageable = params.pageable == false || params.pageable == 'false' ? false : true;
88
103
  this.buildPopulate();
104
+ this.buildOrdenation();
89
105
  }
90
106
  SearchFlow.prototype.buildPopulate = function () {
91
107
  if ((0, Utils_1.isEmpty)(this.populate)) {
@@ -116,6 +132,7 @@ var SearchFlow = /** @class */ (function () {
116
132
  var order = {};
117
133
  this.orderBy = this.orderBy || "_id";
118
134
  order[this.orderBy] = this.order === "desc" ? -1 : 1;
135
+ this.sort = order;
119
136
  return order;
120
137
  };
121
138
  SearchFlow.prototype.diacriticSensitiveRegex = function (string) {
@@ -128,16 +145,61 @@ var SearchFlow = /** @class */ (function () {
128
145
  .replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
129
146
  .replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
130
147
  };
148
+ SearchFlow.prototype.searchPageable = function (model) {
149
+ var pipeline = [];
150
+ for (var _i = 1; _i < arguments.length; _i++) {
151
+ pipeline[_i - 1] = arguments[_i];
152
+ }
153
+ return __awaiter(this, void 0, void 0, function () {
154
+ var result;
155
+ return __generator(this, function (_a) {
156
+ switch (_a.label) {
157
+ case 0: return [4 /*yield*/, model.aggregate([
158
+ {
159
+ $facet: {
160
+ items: __spreadArray([
161
+ { $match: this.filters },
162
+ { $sort: this.sort },
163
+ { $skip: (this.page - 1) * this.limit },
164
+ { $limit: this.limit }
165
+ ], pipeline, true),
166
+ paging: [
167
+ { $match: this.filters },
168
+ { $count: "total" },
169
+ {
170
+ $addFields: {
171
+ page: this.page,
172
+ limit: this.limit
173
+ }
174
+ }
175
+ ]
176
+ }
177
+ }
178
+ ])];
179
+ case 1:
180
+ result = _a.sent();
181
+ return [2 /*return*/, result[0]];
182
+ }
183
+ });
184
+ });
185
+ };
131
186
  SearchFlow.prototype.search = function (model) {
187
+ var pipeline = [];
188
+ for (var _i = 1; _i < arguments.length; _i++) {
189
+ pipeline[_i - 1] = arguments[_i];
190
+ }
132
191
  return __awaiter(this, void 0, void 0, function () {
133
192
  return __generator(this, function (_a) {
134
193
  switch (_a.label) {
135
194
  case 0:
136
- if (!(this.pageable == true)) return [3 /*break*/, 2];
137
- return [4 /*yield*/, this.searchPageable(model)];
195
+ if (!(this.pageable == true)) return [3 /*break*/, 4];
196
+ if (!pipeline) return [3 /*break*/, 2];
197
+ return [4 /*yield*/, this.searchPageable(model, pipeline)];
138
198
  case 1: return [2 /*return*/, _a.sent()];
139
- case 2: return [4 /*yield*/, this.searchNoPageable(model)];
199
+ case 2: return [4 /*yield*/, this.searchPageableOld(model)];
140
200
  case 3: return [2 /*return*/, _a.sent()];
201
+ case 4: return [4 /*yield*/, this.searchNoPageable(model)];
202
+ case 5: return [2 /*return*/, _a.sent()];
141
203
  }
142
204
  });
143
205
  });
@@ -299,6 +361,12 @@ var SearchFlow = /** @class */ (function () {
299
361
  if (['pageable', 'order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
300
362
  return;
301
363
  }
364
+ if (value === 'true') {
365
+ value = true;
366
+ }
367
+ if (value === 'false') {
368
+ value = false;
369
+ }
302
370
  if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
303
371
  var fieldName = key.replace('Range', '');
304
372
  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.41",
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