c2-mongoose 2.0.6 → 2.1.0

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,12 +1,14 @@
1
1
  import mongoose from "mongoose";
2
+ import { SearchResponse } from "../types/SearchResponse";
2
3
  import SearchFlow from "./SearchFlow";
3
- declare class CrudFlow<D> extends SearchFlow {
4
+ declare class CrudFlow<D> {
4
5
  private repository;
5
- constructor(repository: mongoose.Model<any>, model?: any);
6
+ private search;
7
+ constructor(repository: mongoose.Model<any>, search: SearchFlow);
6
8
  create(data: any, session?: any): Promise<any[]>;
7
9
  delete(id: string, session?: any): Promise<void>;
8
10
  update(id: string, data: D, session?: any): Promise<D>;
9
- find(search: SearchFlow): Promise<SearchResponse<D>>;
11
+ find(): Promise<SearchResponse<D>>;
10
12
  getOne(model: D, params?: any): Promise<D>;
11
13
  get(id: string, pop?: string, sel?: string): Promise<D>;
12
14
  }
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -50,18 +35,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
50
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
36
  }
52
37
  };
53
- var __importDefault = (this && this.__importDefault) || function (mod) {
54
- return (mod && mod.__esModule) ? mod : { "default": mod };
55
- };
56
38
  Object.defineProperty(exports, "__esModule", { value: true });
57
- var SearchFlow_1 = __importDefault(require("./SearchFlow"));
58
- var CrudFlow = /** @class */ (function (_super) {
59
- __extends(CrudFlow, _super);
60
- function CrudFlow(repository, model) {
61
- if (model === void 0) { model = {}; }
62
- var _this = _super.call(this, model) || this;
63
- _this.repository = repository;
64
- return _this;
39
+ var CrudFlow = /** @class */ (function () {
40
+ function CrudFlow(repository, search) {
41
+ this.repository = repository;
42
+ this.search = search;
65
43
  }
66
44
  CrudFlow.prototype.create = function (data, session) {
67
45
  if (session === void 0) { session = undefined; }
@@ -101,11 +79,11 @@ var CrudFlow = /** @class */ (function (_super) {
101
79
  });
102
80
  });
103
81
  };
104
- CrudFlow.prototype.find = function (search) {
82
+ CrudFlow.prototype.find = function () {
105
83
  return __awaiter(this, void 0, void 0, function () {
106
84
  return __generator(this, function (_a) {
107
85
  switch (_a.label) {
108
- case 0: return [4 /*yield*/, search.search(this.repository)];
86
+ case 0: return [4 /*yield*/, this.search.search(this.repository)];
109
87
  case 1: return [2 /*return*/, _a.sent()];
110
88
  }
111
89
  });
@@ -117,7 +95,7 @@ var CrudFlow = /** @class */ (function (_super) {
117
95
  var data;
118
96
  return __generator(this, function (_a) {
119
97
  switch (_a.label) {
120
- case 0: return [4 /*yield*/, _super.prototype.findOne.call(this, this.repository, model, params)];
98
+ case 0: return [4 /*yield*/, this.search.findOne(this.repository, model, params)];
121
99
  case 1:
122
100
  data = _a.sent();
123
101
  return [2 /*return*/, data];
@@ -141,5 +119,5 @@ var CrudFlow = /** @class */ (function (_super) {
141
119
  });
142
120
  };
143
121
  return CrudFlow;
144
- }(SearchFlow_1.default));
122
+ }());
145
123
  exports.default = CrudFlow;
@@ -1,4 +1,5 @@
1
1
  import mongoose from "mongoose";
2
+ import { SearchResponse } from "../types/SearchResponse";
2
3
  declare abstract class SearchFlow {
3
4
  searchText: string;
4
5
  order: string;
@@ -181,7 +181,7 @@ var SearchFlow = /** @class */ (function () {
181
181
  paging.limit = this.limit;
182
182
  searchResponse = {};
183
183
  searchResponse.items = items;
184
- searchResponse.paging = paging;
184
+ searchResponse.pagination = paging;
185
185
  return [2 /*return*/, searchResponse];
186
186
  }
187
187
  });
@@ -253,6 +253,13 @@ var SearchFlow = /** @class */ (function () {
253
253
  if ((0, Utils_1.isNotEmpty)(values[1])) {
254
254
  condition[keyAux] = __assign(__assign({}, condition[keyAux]), { $lte: new Date(values[1]) });
255
255
  }
256
+ filters.$and.push(condition);
257
+ }
258
+ else if (key.endsWith('Like')) {
259
+ console.log(key);
260
+ var keyAux = key.replace('Like', '');
261
+ condition[keyAux] = { $regex: value, $options: 'i' };
262
+ filters.$and.push(condition);
256
263
  }
257
264
  else {
258
265
  if (!Array.isArray(value)) {
@@ -261,8 +268,6 @@ var SearchFlow = /** @class */ (function () {
261
268
  else {
262
269
  condition[key] = { $in: value };
263
270
  }
264
- }
265
- if ((0, Utils_1.isNotEmpty)(condition)) {
266
271
  filters.$and.push(condition);
267
272
  }
268
273
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import CrudFlow from "./flow/CrudFlow";
2
2
  import SearchFlow from "./flow/SearchFlow";
3
- export { SearchFlow, CrudFlow };
3
+ import { Pagination, SearchResponse } from "./types/SearchResponse";
4
+ export { CrudFlow, SearchFlow, SearchResponse, Pagination };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CrudFlow = exports.SearchFlow = void 0;
6
+ exports.SearchFlow = exports.CrudFlow = void 0;
7
7
  var CrudFlow_1 = __importDefault(require("./flow/CrudFlow"));
8
8
  exports.CrudFlow = CrudFlow_1.default;
9
9
  var SearchFlow_1 = __importDefault(require("./flow/SearchFlow"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.0.6",
3
+ "version": "2.1.0",
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,13 +1,15 @@
1
1
  import mongoose from "mongoose"
2
+ import { SearchResponse } from "../types/SearchResponse"
2
3
  import SearchFlow from "./SearchFlow"
3
4
 
4
- class CrudFlow<D> extends SearchFlow {
5
+ class CrudFlow<D> {
5
6
 
6
7
  private repository: mongoose.Model<any>
8
+ private search: any
7
9
 
8
- constructor(repository: mongoose.Model<any>, model: any = {}) {
9
- super(model as D)
10
+ constructor(repository: mongoose.Model<any>, search: SearchFlow) {
10
11
  this.repository = repository
12
+ this.search = search
11
13
  }
12
14
 
13
15
  public async create(data: any, session: any = undefined) {
@@ -23,12 +25,12 @@ class CrudFlow<D> extends SearchFlow {
23
25
  return dataAfter as D
24
26
  }
25
27
 
26
- public async find(search: SearchFlow): Promise<SearchResponse<D>> {
27
- return await search.search(this.repository)
28
+ public async find(): Promise<SearchResponse<D>> {
29
+ return await this.search.search(this.repository)
28
30
  }
29
31
 
30
32
  public async getOne(model: D, params: any = {}): Promise<D> {
31
- const data = await super.findOne(this.repository, model, params)
33
+ const data = await this.search.findOne(this.repository, model, params)
32
34
  return data as D
33
35
  }
34
36
 
@@ -1,5 +1,6 @@
1
1
  import mongoose from "mongoose"
2
2
  import { dbCollation } from "../configuration/Environment"
3
+ import { Pagination, SearchResponse } from "../types/SearchResponse"
3
4
  import { isEmpty, isNotEmpty } from "../utils/Utils"
4
5
 
5
6
  abstract class SearchFlow {
@@ -117,14 +118,14 @@ abstract class SearchFlow {
117
118
  private async result(model: mongoose.Model<any>, items: []): Promise<SearchResponse<any>> {
118
119
  var total = await this.count(model)
119
120
 
120
- var paging: Paging = {}
121
+ var paging: Pagination = {}
121
122
  paging.total = total
122
123
  paging.page = this.page
123
124
  paging.limit = this.limit
124
125
 
125
126
  var searchResponse: SearchResponse<any> = {}
126
127
  searchResponse.items = items
127
- searchResponse.paging = paging
128
+ searchResponse.pagination = paging
128
129
 
129
130
  return searchResponse
130
131
  }
@@ -183,15 +184,18 @@ abstract class SearchFlow {
183
184
  $lte: new Date(values[1])
184
185
  }
185
186
  }
187
+ filters.$and.push(condition)
188
+ } else if (key.endsWith('Like')) {
189
+ console.log(key)
190
+ var keyAux = key.replace('Like', '')
191
+ condition[keyAux] = {$regex: value as any, $options: 'i'}
192
+ filters.$and.push(condition)
186
193
  } else {
187
194
  if (!Array.isArray(value)) {
188
195
  condition[key] = value
189
196
  } else {
190
197
  condition[key] = { $in: value }
191
198
  }
192
- }
193
-
194
- if (isNotEmpty(condition)) {
195
199
  filters.$and.push(condition)
196
200
  }
197
201
  }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import CrudFlow from "./flow/CrudFlow"
2
2
  import SearchFlow from "./flow/SearchFlow"
3
+ import { Pagination, SearchResponse } from "./types/SearchResponse"
3
4
 
4
- export { SearchFlow, CrudFlow }
5
+ export { CrudFlow, SearchFlow, SearchResponse, Pagination }
@@ -1,9 +1,9 @@
1
- declare interface SearchResponse<T> {
2
- paging?: Paging,
1
+ export declare interface SearchResponse<T> {
2
+ pagination?: Pagination,
3
3
  items?: T[]
4
4
  }
5
5
 
6
- declare interface Paging {
6
+ export declare interface Pagination {
7
7
  total?: number,
8
8
  page?: number,
9
9
  limit?: number