c2-mongoose 2.1.51 → 2.1.53

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,9 +1,6 @@
1
1
  import mongoose, { ClientSession } from "mongoose";
2
- import { SearchResponse } from "../types/SearchResponse";
2
+ import { SearchOptions, SearchResponse } from "../types/SearchResponse";
3
3
  import SearchFlow from "./SearchFlow";
4
- export interface PipelineStage {
5
- [key: string]: any;
6
- }
7
4
  declare class CrudFlow<D> {
8
5
  private repository;
9
6
  private search;
@@ -15,7 +12,7 @@ declare class CrudFlow<D> {
15
12
  update(id: string, data: D, session?: any): Promise<D>;
16
13
  updateAny(id: string, data: any, session?: any): Promise<D>;
17
14
  updateByModel(filter: any, data: any, params: any): Promise<D>;
18
- find(pipelineStages?: PipelineStage[]): Promise<SearchResponse<D>>;
15
+ find(options?: SearchOptions): Promise<SearchResponse<D>>;
19
16
  getOne(model: D, params?: any): Promise<D>;
20
17
  get(id: string, pop?: string, sel?: string, session?: ClientSession): Promise<D>;
21
18
  getById(id: string, session?: ClientSession): Promise<Partial<D>>;
@@ -123,11 +123,11 @@ var CrudFlow = /** @class */ (function () {
123
123
  });
124
124
  });
125
125
  };
126
- CrudFlow.prototype.find = function (pipelineStages) {
126
+ CrudFlow.prototype.find = function (options) {
127
127
  return __awaiter(this, void 0, void 0, function () {
128
128
  return __generator(this, function (_a) {
129
129
  switch (_a.label) {
130
- case 0: return [4 /*yield*/, this.search.search(this.repository, pipelineStages)];
130
+ case 0: return [4 /*yield*/, this.search.search(this.repository, options)];
131
131
  case 1: return [2 /*return*/, _a.sent()];
132
132
  }
133
133
  });
@@ -1,5 +1,5 @@
1
1
  import mongoose, { ClientSession } from "mongoose";
2
- import { SearchResponse } from "../types/SearchResponse";
2
+ import { SearchOptions, SearchResponse } from "../types/SearchResponse";
3
3
  declare abstract class SearchFlow {
4
4
  searchText: string;
5
5
  order: string;
@@ -16,11 +16,11 @@ declare abstract class SearchFlow {
16
16
  buildPath(target: string, nested?: string): any;
17
17
  buildOrdenation(): any;
18
18
  buildRegex(searchText: string): RegExp;
19
- searchPageable(model: mongoose.Model<any>, pipeline: any): Promise<{
19
+ searchPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<{
20
20
  items: any;
21
21
  paging: any;
22
22
  }>;
23
- search(model: mongoose.Model<any>, pipeline?: any): Promise<SearchResponse<any>>;
23
+ search(model: mongoose.Model<any>, options?: SearchOptions): Promise<SearchResponse<any>>;
24
24
  /**
25
25
  *
26
26
  * @param model
@@ -144,18 +144,19 @@ var SearchFlow = /** @class */ (function () {
144
144
  .replace(/[ç|c|C|Ç]/g, '[c,ç]');
145
145
  return new RegExp("".concat(regexExpression), 'i');
146
146
  };
147
- SearchFlow.prototype.searchPageable = function (model, pipeline) {
147
+ SearchFlow.prototype.searchPageable = function (model, options) {
148
+ var _a;
148
149
  return __awaiter(this, void 0, void 0, function () {
149
150
  var stagesItems, result;
150
- return __generator(this, function (_a) {
151
- switch (_a.label) {
151
+ return __generator(this, function (_b) {
152
+ switch (_b.label) {
152
153
  case 0:
153
154
  stagesItems = __spreadArray([
154
155
  { $match: this.filters },
155
156
  { $sort: this.sort },
156
157
  { $skip: (this.page - 1) * this.limit },
157
158
  { $limit: this.limit }
158
- ], pipeline, true);
159
+ ], options.pipelines, true);
159
160
  return [4 /*yield*/, model.aggregate([
160
161
  {
161
162
  $facet: {
@@ -172,25 +173,25 @@ var SearchFlow = /** @class */ (function () {
172
173
  ]
173
174
  }
174
175
  }
175
- ])];
176
+ ]).session(options === null || options === void 0 ? void 0 : options.session)];
176
177
  case 1:
177
- result = _a.sent();
178
+ result = _b.sent();
178
179
  return [2 /*return*/, {
179
180
  items: result[0].items,
180
- paging: result[0].paging[0]
181
+ paging: ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.paging[0]) || { total: 0, page: 1, limit: this.limit }
181
182
  }];
182
183
  }
183
184
  });
184
185
  });
185
186
  };
186
- SearchFlow.prototype.search = function (model, pipeline) {
187
+ SearchFlow.prototype.search = function (model, options) {
187
188
  return __awaiter(this, void 0, void 0, function () {
188
189
  return __generator(this, function (_a) {
189
190
  switch (_a.label) {
190
191
  case 0:
191
192
  if (!(this.pageable == true)) return [3 /*break*/, 4];
192
- if (!pipeline) return [3 /*break*/, 2];
193
- return [4 /*yield*/, this.searchPageable(model, pipeline)];
193
+ if (!(options === null || options === void 0 ? void 0 : options.pipelines)) return [3 /*break*/, 2];
194
+ return [4 /*yield*/, this.searchPageable(model, options)];
194
195
  case 1: return [2 /*return*/, _a.sent()];
195
196
  case 2: return [4 /*yield*/, this.searchPageableOld(model)];
196
197
  case 3: return [2 /*return*/, _a.sent()];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.51",
3
+ "version": "2.1.53",
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,11 +1,8 @@
1
1
  import mongoose, { ClientSession } from "mongoose"
2
- import { SearchResponse } from "../types/SearchResponse"
2
+ import { PipelineStage, SearchOptions, SearchResponse } from "../types/SearchResponse"
3
3
  import SearchFlow from "./SearchFlow"
4
4
  import { isEmpty, isNotEmpty } from "../utils/Utils"
5
5
 
6
- export interface PipelineStage {
7
- [key: string]: any;
8
- }
9
6
  class CrudFlow<D> {
10
7
 
11
8
  private repository: mongoose.Model<any>
@@ -47,8 +44,8 @@ class CrudFlow<D> {
47
44
  return dataAfter as D
48
45
  }
49
46
 
50
- public async find(pipelineStages?: PipelineStage[]): Promise<SearchResponse<D>> {
51
- return await this.search.search(this.repository, pipelineStages)
47
+ public async find(options?: SearchOptions): Promise<SearchResponse<D>> {
48
+ return await this.search.search(this.repository, options)
52
49
  }
53
50
 
54
51
  public async getOne(model: D, params: any = {}): Promise<D> {
@@ -1,7 +1,7 @@
1
1
  import moment from "moment"
2
2
  import mongoose, { ClientSession } from "mongoose"
3
3
  import { dbCollation } from "../configuration/Environment"
4
- import { Pagination, SearchResponse } from "../types/SearchResponse"
4
+ import { Pagination, PipelineStage, SearchOptions, SearchResponse } from "../types/SearchResponse"
5
5
  import { isEmpty, isNotEmpty } from "../utils/Utils"
6
6
 
7
7
  abstract class SearchFlow {
@@ -74,20 +74,20 @@ abstract class SearchFlow {
74
74
  return new RegExp(`${regexExpression}`, 'i');
75
75
  }
76
76
 
77
- async searchPageable(model: mongoose.Model<any>, pipeline: any) {
77
+ async searchPageable(model: mongoose.Model<any>, options: SearchOptions) {
78
78
  let stagesItems = [
79
79
  { $match: this.filters },
80
80
  { $sort: this.sort },
81
81
  { $skip: (this.page - 1) * this.limit },
82
82
  { $limit: this.limit },
83
- ...pipeline
83
+ ...options.pipelines
84
84
  ]
85
85
 
86
86
  const result = await model.aggregate(
87
87
  [
88
88
  {
89
89
  $facet: {
90
- items: stagesItems,
90
+ items: stagesItems as any,
91
91
  paging: [
92
92
  { $match: this.filters },
93
93
  { $count: "total" },
@@ -102,17 +102,17 @@ abstract class SearchFlow {
102
102
  }
103
103
  }
104
104
  ]
105
- )
105
+ ).session(options?.session as ClientSession)
106
106
  return {
107
107
  items: result[0].items,
108
- paging: result[0].paging[0]
108
+ paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit }
109
109
  }
110
110
  }
111
111
 
112
- public async search(model: mongoose.Model<any>, pipeline?: any): Promise<SearchResponse<any>> {
112
+ public async search(model: mongoose.Model<any>, options?: SearchOptions): Promise<SearchResponse<any>> {
113
113
  if (this.pageable == true) {
114
- if (pipeline) {
115
- return await this.searchPageable(model, pipeline)
114
+ if (options?.pipelines) {
115
+ return await this.searchPageable(model, options)
116
116
  }
117
117
  return await this.searchPageableOld(model)
118
118
  }
@@ -1,3 +1,5 @@
1
+ import { ClientSession } from "mongoose";
2
+
1
3
  declare interface SearchResponse<T> {
2
4
  paging?: Pagination,
3
5
  items?: T[]
@@ -9,4 +11,13 @@ declare interface Pagination {
9
11
  limit?: number
10
12
  }
11
13
 
12
- export { SearchResponse, Pagination }
14
+ declare interface PipelineStage {
15
+ [key: string]: any;
16
+ }
17
+
18
+ declare interface SearchOptions {
19
+ pipelines: PipelineStage[],
20
+ session?: ClientSession
21
+ }
22
+
23
+ export { SearchResponse, Pagination, PipelineStage, SearchOptions };