c2-mongoose 2.1.95 → 2.1.97

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.
@@ -13,7 +13,7 @@ declare class CrudFlow<D> {
13
13
  updateAny(id: string, data: any, session?: any): Promise<D>;
14
14
  updateByModel(filter: any, data: any, params: any): Promise<D>;
15
15
  find(options?: SearchOptions): Promise<SearchResponse<D>>;
16
- getOne(model: D, params?: any): Promise<D>;
16
+ getOne(model: D, session: ClientSession): 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>>;
19
19
  sumBy(fieldToSum: any, fieldToGroup: any, addToSet?: any, session?: ClientSession): Promise<any>;
@@ -133,13 +133,12 @@ var CrudFlow = /** @class */ (function () {
133
133
  });
134
134
  });
135
135
  };
136
- CrudFlow.prototype.getOne = function (model, params) {
137
- if (params === void 0) { params = {}; }
136
+ CrudFlow.prototype.getOne = function (model, session) {
138
137
  return __awaiter(this, void 0, void 0, function () {
139
138
  var data;
140
139
  return __generator(this, function (_a) {
141
140
  switch (_a.label) {
142
- case 0: return [4 /*yield*/, this.search.findOne(this.repository, model, params)];
141
+ case 0: return [4 /*yield*/, this.search.findOne(this.repository, model, session)];
143
142
  case 1:
144
143
  data = _a.sent();
145
144
  return [2 /*return*/, data];
@@ -38,9 +38,10 @@ declare abstract class SearchFlow {
38
38
  private searchNoPageable;
39
39
  private result;
40
40
  count(model: mongoose.Model<any>): Promise<number>;
41
- findOne(repository: mongoose.Model<any>, model: any, params?: any): Promise<any>;
41
+ findOne(repository: mongoose.Model<any>, model: any, session?: ClientSession): Promise<any>;
42
42
  sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items?: any, session?: ClientSession): Promise<any[]>;
43
43
  sumByPaging(model: mongoose.Model<any>, fieldToSum: any, fieldToGroup: any, items?: any): Promise<SearchResponse<any>>;
44
+ isValidObjectId(value: string): boolean;
44
45
  buildDefaultFilters(objectSearch: any): any;
45
46
  addFilterModel(model: any, filters: any): void;
46
47
  }
@@ -309,16 +309,15 @@ var SearchFlow = /** @class */ (function () {
309
309
  });
310
310
  });
311
311
  };
312
- SearchFlow.prototype.findOne = function (repository, model, params) {
313
- if (params === void 0) { params = {}; }
312
+ SearchFlow.prototype.findOne = function (repository, model, session) {
314
313
  return __awaiter(this, void 0, void 0, function () {
315
314
  return __generator(this, function (_a) {
316
315
  switch (_a.label) {
317
316
  case 0: return [4 /*yield*/, repository.findOne(model)
318
- .sort(params.sort)
319
- .select(params.select)
320
- .populate(params.populate)
321
- .session(params === null || params === void 0 ? void 0 : params.session)];
317
+ .sort(this.sort)
318
+ .select(this.select)
319
+ .populate(this.populate)
320
+ .session(session)];
322
321
  case 1: return [2 /*return*/, _a.sent()];
323
322
  }
324
323
  });
@@ -404,6 +403,10 @@ var SearchFlow = /** @class */ (function () {
404
403
  });
405
404
  });
406
405
  };
406
+ SearchFlow.prototype.isValidObjectId = function (value) {
407
+ var objectIdPattern = /^[0-9a-fA-F]{24}$/;
408
+ return objectIdPattern.test(value);
409
+ };
407
410
  SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
408
411
  var _this = this;
409
412
  var filters = { $and: [] };
@@ -414,7 +417,7 @@ var SearchFlow = /** @class */ (function () {
414
417
  if (['projection', 'pageable', 'order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
415
418
  return;
416
419
  }
417
- if (typeof value === 'string' && mongoose_1.Types.ObjectId.isValid(value)) {
420
+ if (typeof value === 'string' && _this.isValidObjectId(value)) {
418
421
  value = new mongoose_1.Types.ObjectId(value);
419
422
  }
420
423
  if (value === 'true') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.95",
3
+ "version": "2.1.97",
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",
@@ -48,8 +48,8 @@ class CrudFlow<D> {
48
48
  return await this.search.search(this.repository, options)
49
49
  }
50
50
 
51
- public async getOne(model: D, params: any = {}): Promise<D> {
52
- const data = await this.search.findOne(this.repository, model, params)
51
+ public async getOne(model: D, session: ClientSession): Promise<D> {
52
+ const data = await this.search.findOne(this.repository, model, session)
53
53
  return data as D
54
54
  }
55
55
 
@@ -247,12 +247,12 @@ abstract class SearchFlow {
247
247
  return await model.countDocuments(this.filters).exec()
248
248
  }
249
249
 
250
- public async findOne(repository: mongoose.Model<any>, model: any, params: any = {}) {
250
+ public async findOne(repository: mongoose.Model<any>, model: any, session?: ClientSession) {
251
251
  return await repository.findOne(model)
252
- .sort(params.sort)
253
- .select(params.select)
254
- .populate(params.populate)
255
- .session(params?.session)
252
+ .sort(this.sort)
253
+ .select(this.select)
254
+ .populate(this.populate)
255
+ .session(session!)
256
256
  }
257
257
 
258
258
  public async sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items: any = undefined, session?: ClientSession): Promise<any[]> {
@@ -321,6 +321,11 @@ abstract class SearchFlow {
321
321
  }
322
322
  }
323
323
 
324
+ public isValidObjectId(value: string) {
325
+ const objectIdPattern = /^[0-9a-fA-F]{24}$/;
326
+ return objectIdPattern.test(value);
327
+ }
328
+
324
329
  public buildDefaultFilters(objectSearch: any) {
325
330
  let filters = { $and: [] } as any
326
331
  Object.entries(objectSearch.model).forEach(([key, value]) => {
@@ -330,7 +335,7 @@ abstract class SearchFlow {
330
335
  return
331
336
  }
332
337
 
333
- if (typeof value === 'string' && Types.ObjectId.isValid(value as any)) {
338
+ if (typeof value === 'string' && this.isValidObjectId(value as string)) {
334
339
  value = new Types.ObjectId(value as string)
335
340
  }
336
341