c2-mongoose 2.0.2 → 2.0.4

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, select: undefined, populate: string | [] | any, 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,14 @@ var CrudFlow = /** @class */ (function () {
85
106
  }
86
107
  });
87
108
  }); };
88
- this.findOne = function (model, sort) {
109
+ _this.getOne = function (model, select, populate, sort) {
110
+ if (select === void 0) { select = undefined; }
89
111
  if (sort === void 0) { sort = undefined; }
90
112
  return __awaiter(_this, void 0, void 0, function () {
91
113
  var data;
92
114
  return __generator(this, function (_a) {
93
115
  switch (_a.label) {
94
- case 0: return [4 /*yield*/, this.repository.findOne(model)
95
- .sort(sort)];
116
+ case 0: return [4 /*yield*/, _super.prototype.findOne.call(this, this.repository, model, select, populate, sort)];
96
117
  case 1:
97
118
  data = _a.sent();
98
119
  return [2 /*return*/, data];
@@ -100,7 +121,7 @@ var CrudFlow = /** @class */ (function () {
100
121
  });
101
122
  });
102
123
  };
103
- this.get = function (id, pop, sel) {
124
+ _this.get = function (id, pop, sel) {
104
125
  if (pop === void 0) { pop = ""; }
105
126
  if (sel === void 0) { sel = ""; }
106
127
  return __awaiter(_this, void 0, void 0, function () {
@@ -115,8 +136,9 @@ var CrudFlow = /** @class */ (function () {
115
136
  });
116
137
  });
117
138
  };
118
- this.repository = repository;
139
+ _this.repository = repository;
140
+ return _this;
119
141
  }
120
142
  return CrudFlow;
121
- }());
143
+ }(SearchFlow_1.default));
122
144
  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, select: undefined, populate: string | [] | 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,21 @@ var SearchFlow = /** @class */ (function () {
260
260
  this.limit = params.limit || 25;
261
261
  this.buildPopulate();
262
262
  }
263
+ SearchFlow.prototype.findOne = function (repository, model, select, populate, sort) {
264
+ if (select === void 0) { select = undefined; }
265
+ if (sort === void 0) { sort = undefined; }
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ return __generator(this, function (_a) {
268
+ switch (_a.label) {
269
+ case 0: return [4 /*yield*/, repository.findOne(model)
270
+ .sort(sort)
271
+ .select(select)
272
+ .populate(populate)];
273
+ case 1: return [2 /*return*/, _a.sent()];
274
+ }
275
+ });
276
+ });
277
+ };
263
278
  return SearchFlow;
264
279
  }());
265
280
  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.4",
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, select = undefined, populate: string | [] | any, sort = undefined): Promise<D> => {
31
+ const data = await super.findOne(this.repository, model, select, populate, sort)
33
32
  return data as D
34
33
  }
35
34
 
@@ -133,6 +133,13 @@ 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, select = undefined, populate: string | [] | any, sort = undefined) {
137
+ return await repository.findOne(model)
138
+ .sort(sort)
139
+ .select(select)
140
+ .populate(populate)
141
+ }
142
+
136
143
  public sumBy = async (model: mongoose.Model<any>, _sum: any, _by: any): Promise<any[]> => {
137
144
  const ret = await model.aggregate(
138
145
  [