c2-mongoose 2.0.2 → 2.0.3

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,13 +1,13 @@
1
1
  import mongoose from "mongoose";
2
2
  import SearchFlow from "./SearchFlow";
3
- declare class CrudFlow<D> {
3
+ declare class CrudFlow<D> extends SearchFlow {
4
4
  private repository;
5
- constructor(repository: mongoose.Model<any>);
6
- create: (data: D, session?: any) => Promise<D>;
5
+ constructor(repository: mongoose.Model<any>, model?: any);
6
+ create: (data: any, session?: any) => Promise<any[]>;
7
7
  delete: (id: string, session?: any) => Promise<void>;
8
8
  update: (id: string, data: D, session?: any) => Promise<D>;
9
9
  find: (search: SearchFlow) => Promise<SearchResponse<D>>;
10
- findOne: (model: D, sort?: undefined) => Promise<D>;
10
+ getOne: (model: D, sort?: undefined) => Promise<D>;
11
11
  get: (id: string, pop?: string, sel?: string) => Promise<D>;
12
12
  }
13
13
  export default CrudFlow;
@@ -1,4 +1,19 @@
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
+ })();
2
17
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
18
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
19
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,11 +50,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
50
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
51
  }
37
52
  };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
38
56
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var CrudFlow = /** @class */ (function () {
40
- function CrudFlow(repository) {
41
- var _this = this;
42
- this.create = function (data, session) {
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.create = function (data, session) {
43
64
  if (session === void 0) { session = undefined; }
44
65
  return __awaiter(_this, void 0, void 0, function () {
45
66
  return __generator(this, function (_a) {
@@ -50,7 +71,7 @@ var CrudFlow = /** @class */ (function () {
50
71
  });
51
72
  });
52
73
  };
53
- this.delete = function (id, session) {
74
+ _this.delete = function (id, session) {
54
75
  if (session === void 0) { session = undefined; }
55
76
  return __awaiter(_this, void 0, void 0, function () {
56
77
  return __generator(this, function (_a) {
@@ -63,7 +84,7 @@ var CrudFlow = /** @class */ (function () {
63
84
  });
64
85
  });
65
86
  };
66
- this.update = function (id, data, session) {
87
+ _this.update = function (id, data, session) {
67
88
  if (session === void 0) { session = undefined; }
68
89
  return __awaiter(_this, void 0, void 0, function () {
69
90
  var dataAfter;
@@ -77,7 +98,7 @@ var CrudFlow = /** @class */ (function () {
77
98
  });
78
99
  });
79
100
  };
80
- this.find = function (search) { return __awaiter(_this, void 0, void 0, function () {
101
+ _this.find = function (search) { return __awaiter(_this, void 0, void 0, function () {
81
102
  return __generator(this, function (_a) {
82
103
  switch (_a.label) {
83
104
  case 0: return [4 /*yield*/, search.search(this.repository)];
@@ -85,14 +106,13 @@ var CrudFlow = /** @class */ (function () {
85
106
  }
86
107
  });
87
108
  }); };
88
- this.findOne = function (model, sort) {
109
+ _this.getOne = function (model, sort) {
89
110
  if (sort === void 0) { sort = undefined; }
90
111
  return __awaiter(_this, void 0, void 0, function () {
91
112
  var data;
92
113
  return __generator(this, function (_a) {
93
114
  switch (_a.label) {
94
- case 0: return [4 /*yield*/, this.repository.findOne(model)
95
- .sort(sort)];
115
+ case 0: return [4 /*yield*/, _super.prototype.findOne.call(this, this.repository, model, sort)];
96
116
  case 1:
97
117
  data = _a.sent();
98
118
  return [2 /*return*/, data];
@@ -100,7 +120,7 @@ var CrudFlow = /** @class */ (function () {
100
120
  });
101
121
  });
102
122
  };
103
- this.get = function (id, pop, sel) {
123
+ _this.get = function (id, pop, sel) {
104
124
  if (pop === void 0) { pop = ""; }
105
125
  if (sel === void 0) { sel = ""; }
106
126
  return __awaiter(_this, void 0, void 0, function () {
@@ -115,8 +135,9 @@ var CrudFlow = /** @class */ (function () {
115
135
  });
116
136
  });
117
137
  };
118
- this.repository = repository;
138
+ _this.repository = repository;
139
+ return _this;
119
140
  }
120
141
  return CrudFlow;
121
- }());
142
+ }(SearchFlow_1.default));
122
143
  exports.default = CrudFlow;
@@ -19,6 +19,7 @@ declare abstract class SearchFlow {
19
19
  private searchNoPageable;
20
20
  private result;
21
21
  count: (model: mongoose.Model<any>) => Promise<number>;
22
+ findOne(repository: mongoose.Model<any>, model: any, sort?: undefined): Promise<any>;
22
23
  sumBy: (model: mongoose.Model<any>, _sum: any, _by: any) => Promise<any[]>;
23
24
  buildDefaultFilters: (objectSearch: any) => any;
24
25
  addFilterModel: (model: any, filters: any) => void;
@@ -260,6 +260,18 @@ var SearchFlow = /** @class */ (function () {
260
260
  this.limit = params.limit || 25;
261
261
  this.buildPopulate();
262
262
  }
263
+ SearchFlow.prototype.findOne = function (repository, model, sort) {
264
+ if (sort === void 0) { sort = undefined; }
265
+ return __awaiter(this, void 0, void 0, function () {
266
+ return __generator(this, function (_a) {
267
+ switch (_a.label) {
268
+ case 0: return [4 /*yield*/, repository.findOne(model)
269
+ .sort(sort)];
270
+ case 1: return [2 /*return*/, _a.sent()];
271
+ }
272
+ });
273
+ });
274
+ };
263
275
  return SearchFlow;
264
276
  }());
265
277
  exports.default = SearchFlow;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
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",
7
7
  "scripts": {
8
- "build": "tsc",
9
- "preversion": "tsc",
8
+ "watch": "tsc -w --skipLibCheck",
9
+ "build": "tsc --skipLibCheck",
10
+ "preversion": "tsc --skipLibCheck",
10
11
  "postversion": "git push && git push --tags && npm publish",
11
12
  "publish-patch": "npm version patch",
12
13
  "publish-minor": "npm version minor",
@@ -1,16 +1,17 @@
1
1
  import mongoose from "mongoose"
2
2
  import SearchFlow from "./SearchFlow"
3
3
 
4
- class CrudFlow<D> {
5
-
4
+ class CrudFlow<D> extends SearchFlow {
5
+
6
6
  private repository: mongoose.Model<any>
7
7
 
8
- constructor(repository: mongoose.Model<any>) {
8
+ constructor(repository: mongoose.Model<any>, model: any = {}) {
9
+ super(model as D)
9
10
  this.repository = repository
10
11
  }
11
12
 
12
- public create = async (data: D, session: any = undefined): Promise<D> => {
13
- return await this.repository.create([data], { session }) as D
13
+ public create = async (data: any, session: any = undefined) => {
14
+ return await this.repository.create([data], { session })
14
15
  }
15
16
 
16
17
  public delete = async (id: string, session: any = undefined) => {
@@ -26,10 +27,8 @@ class CrudFlow<D> {
26
27
  return await search.search(this.repository)
27
28
  }
28
29
 
29
- public findOne = async (model: D, sort = undefined): Promise<D> => {
30
- const data = await this.repository.findOne(model as any)
31
- .sort(sort)
32
-
30
+ public getOne = async (model: D, sort = undefined): Promise<D> => {
31
+ const data = await super.findOne(this.repository, model, sort)
33
32
  return data as D
34
33
  }
35
34
 
@@ -133,6 +133,11 @@ abstract class SearchFlow {
133
133
  return await model.countDocuments(this.filters).exec()
134
134
  }
135
135
 
136
+ public async findOne(repository: mongoose.Model<any>, model: any, sort = undefined) {
137
+ return await repository.findOne(model)
138
+ .sort(sort)
139
+ }
140
+
136
141
  public sumBy = async (model: mongoose.Model<any>, _sum: any, _by: any): Promise<any[]> => {
137
142
  const ret = await model.aggregate(
138
143
  [