c2-mongoose 2.1.52 → 2.1.54

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
@@ -71,7 +71,7 @@ var SearchFlow = /** @class */ (function () {
71
71
  * @deprecated
72
72
  * @returns
73
73
  */
74
- this.searchPageableOld = function (model) { return __awaiter(_this, void 0, void 0, function () {
74
+ this.searchPageableOld = function (model, session) { return __awaiter(_this, void 0, void 0, function () {
75
75
  var sort, items;
76
76
  return __generator(this, function (_a) {
77
77
  switch (_a.label) {
@@ -83,6 +83,7 @@ var SearchFlow = /** @class */ (function () {
83
83
  .skip((this.page - 1) * this.limit)
84
84
  .limit(this.limit)
85
85
  .sort(sort)
86
+ .session(session)
86
87
  .collation({
87
88
  locale: Environment_1.dbCollation || "pt"
88
89
  })];
@@ -144,7 +145,7 @@ var SearchFlow = /** @class */ (function () {
144
145
  .replace(/[ç|c|C|Ç]/g, '[c,ç]');
145
146
  return new RegExp("".concat(regexExpression), 'i');
146
147
  };
147
- SearchFlow.prototype.searchPageable = function (model, pipeline) {
148
+ SearchFlow.prototype.searchPageable = function (model, options) {
148
149
  var _a;
149
150
  return __awaiter(this, void 0, void 0, function () {
150
151
  var stagesItems, result;
@@ -156,7 +157,7 @@ var SearchFlow = /** @class */ (function () {
156
157
  { $sort: this.sort },
157
158
  { $skip: (this.page - 1) * this.limit },
158
159
  { $limit: this.limit }
159
- ], pipeline, true);
160
+ ], options.pipelines, true);
160
161
  return [4 /*yield*/, model.aggregate([
161
162
  {
162
163
  $facet: {
@@ -173,7 +174,7 @@ var SearchFlow = /** @class */ (function () {
173
174
  ]
174
175
  }
175
176
  }
176
- ])];
177
+ ]).session(options === null || options === void 0 ? void 0 : options.session)];
177
178
  case 1:
178
179
  result = _b.sent();
179
180
  return [2 /*return*/, {
@@ -184,24 +185,24 @@ var SearchFlow = /** @class */ (function () {
184
185
  });
185
186
  });
186
187
  };
187
- SearchFlow.prototype.search = function (model, pipeline) {
188
+ SearchFlow.prototype.search = function (model, options) {
188
189
  return __awaiter(this, void 0, void 0, function () {
189
190
  return __generator(this, function (_a) {
190
191
  switch (_a.label) {
191
192
  case 0:
192
193
  if (!(this.pageable == true)) return [3 /*break*/, 4];
193
- if (!pipeline) return [3 /*break*/, 2];
194
- return [4 /*yield*/, this.searchPageable(model, pipeline)];
194
+ if (!(options === null || options === void 0 ? void 0 : options.pipelines)) return [3 /*break*/, 2];
195
+ return [4 /*yield*/, this.searchPageable(model, options)];
195
196
  case 1: return [2 /*return*/, _a.sent()];
196
- case 2: return [4 /*yield*/, this.searchPageableOld(model)];
197
+ case 2: return [4 /*yield*/, this.searchPageableOld(model, options === null || options === void 0 ? void 0 : options.session)];
197
198
  case 3: return [2 /*return*/, _a.sent()];
198
- case 4: return [4 /*yield*/, this.searchNoPageable(model)];
199
+ case 4: return [4 /*yield*/, this.searchNoPageable(model, options === null || options === void 0 ? void 0 : options.session)];
199
200
  case 5: return [2 /*return*/, _a.sent()];
200
201
  }
201
202
  });
202
203
  });
203
204
  };
204
- SearchFlow.prototype.searchNoPageable = function (model) {
205
+ SearchFlow.prototype.searchNoPageable = function (model, session) {
205
206
  return __awaiter(this, void 0, void 0, function () {
206
207
  var sort, items;
207
208
  return __generator(this, function (_a) {
@@ -212,6 +213,7 @@ var SearchFlow = /** @class */ (function () {
212
213
  .find(this.filters, this.select)
213
214
  .populate(this.populate)
214
215
  .sort(sort)
216
+ .session(session)
215
217
  .collation({
216
218
  locale: Environment_1.dbCollation || "pt"
217
219
  })];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.52",
3
+ "version": "2.1.54",
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,22 +102,22 @@ 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
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
- return await this.searchPageableOld(model)
117
+ return await this.searchPageableOld(model, options?.session)
118
118
  }
119
119
 
120
- return await this.searchNoPageable(model)
120
+ return await this.searchNoPageable(model, options?.session)
121
121
  }
122
122
 
123
123
  /**
@@ -126,7 +126,7 @@ abstract class SearchFlow {
126
126
  * @deprecated
127
127
  * @returns
128
128
  */
129
- private searchPageableOld = async (model: mongoose.Model<any>): Promise<SearchResponse<any>> => {
129
+ private searchPageableOld = async (model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> => {
130
130
  const sort = this.buildOrdenation()
131
131
  var items = await model
132
132
  .find(this.filters, this.select)
@@ -134,6 +134,7 @@ abstract class SearchFlow {
134
134
  .skip((this.page - 1) * this.limit)
135
135
  .limit(this.limit)
136
136
  .sort(sort)
137
+ .session(session!)
137
138
  .collation({
138
139
  locale: dbCollation || "pt"
139
140
  }) as []
@@ -141,12 +142,13 @@ abstract class SearchFlow {
141
142
  return this.result(model, items)
142
143
  }
143
144
 
144
- private async searchNoPageable(model: mongoose.Model<any>): Promise<SearchResponse<any>> {
145
+ private async searchNoPageable(model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> {
145
146
  const sort = this.buildOrdenation()
146
147
  var items = await model
147
148
  .find(this.filters, this.select)
148
149
  .populate(this.populate)
149
150
  .sort(sort)
151
+ .session(session!)
150
152
  .collation({
151
153
  locale: dbCollation || "pt"
152
154
  }) as []
@@ -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 };