c2-mongoose 2.1.7 → 2.1.9

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,4 +1,4 @@
1
- import mongoose from "mongoose";
1
+ import mongoose, { ClientSession } from "mongoose";
2
2
  import { SearchResponse } from "../types/SearchResponse";
3
3
  import SearchFlow from "./SearchFlow";
4
4
  declare class CrudFlow<D> {
@@ -8,9 +8,10 @@ declare class CrudFlow<D> {
8
8
  prepareSearch(search: SearchFlow): void;
9
9
  create(data: any, session?: any): Promise<any[]>;
10
10
  delete(id: string, session?: any): Promise<void>;
11
+ deleteMany(conditions: {}, session?: any): Promise<void>;
11
12
  update(id: string, data: D, session?: any): Promise<D>;
12
13
  find(): Promise<SearchResponse<D>>;
13
14
  getOne(model: D, params?: any): Promise<D>;
14
- get(id: string, pop?: string, sel?: string): Promise<D>;
15
+ get(id: string, pop?: string, sel?: string, session?: ClientSession): Promise<D>;
15
16
  }
16
17
  export default CrudFlow;
@@ -68,6 +68,19 @@ var CrudFlow = /** @class */ (function () {
68
68
  });
69
69
  });
70
70
  };
71
+ CrudFlow.prototype.deleteMany = function (conditions, session) {
72
+ if (session === void 0) { session = undefined; }
73
+ return __awaiter(this, void 0, void 0, function () {
74
+ return __generator(this, function (_a) {
75
+ switch (_a.label) {
76
+ case 0: return [4 /*yield*/, this.repository.deleteMany(conditions).session(session)];
77
+ case 1:
78
+ _a.sent();
79
+ return [2 /*return*/];
80
+ }
81
+ });
82
+ });
83
+ };
71
84
  CrudFlow.prototype.update = function (id, data, session) {
72
85
  if (session === void 0) { session = undefined; }
73
86
  return __awaiter(this, void 0, void 0, function () {
@@ -106,14 +119,14 @@ var CrudFlow = /** @class */ (function () {
106
119
  });
107
120
  });
108
121
  };
109
- CrudFlow.prototype.get = function (id, pop, sel) {
122
+ CrudFlow.prototype.get = function (id, pop, sel, session) {
110
123
  if (pop === void 0) { pop = ""; }
111
124
  if (sel === void 0) { sel = ""; }
112
125
  return __awaiter(this, void 0, void 0, function () {
113
126
  var data;
114
127
  return __generator(this, function (_a) {
115
128
  switch (_a.label) {
116
- case 0: return [4 /*yield*/, this.repository.findById(id).populate(pop).select(sel)];
129
+ case 0: return [4 /*yield*/, this.repository.findById(id).populate(pop).select(sel).session(session)];
117
130
  case 1:
118
131
  data = _a.sent();
119
132
  return [2 /*return*/, data];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.7",
3
+ "version": "2.1.9",
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,4 +1,4 @@
1
- import mongoose from "mongoose"
1
+ import mongoose, { ClientSession } from "mongoose"
2
2
  import { SearchResponse } from "../types/SearchResponse"
3
3
  import SearchFlow from "./SearchFlow"
4
4
 
@@ -11,7 +11,7 @@ class CrudFlow<D> {
11
11
  this.repository = repository
12
12
  this.search = search
13
13
  }
14
-
14
+
15
15
  public prepareSearch(search: SearchFlow) {
16
16
  this.search = search
17
17
  }
@@ -24,6 +24,10 @@ class CrudFlow<D> {
24
24
  await this.repository.findByIdAndRemove(id, { session })
25
25
  }
26
26
 
27
+ public async deleteMany(conditions: {}, session: any = undefined) {
28
+ await this.repository.deleteMany(conditions).session(session)
29
+ }
30
+
27
31
  public async update(id: string, data: D, session: any = undefined): Promise<D> {
28
32
  const dataAfter = await this.repository.findByIdAndUpdate(id, { $set: data as any }, { returnDocument: 'after', session })
29
33
  return dataAfter as D
@@ -38,8 +42,8 @@ class CrudFlow<D> {
38
42
  return data as D
39
43
  }
40
44
 
41
- public async get(id: string, pop = "", sel = ""): Promise<D> {
42
- const data = await this.repository.findById(id).populate(pop).select(sel)
45
+ public async get(id: string, pop = "", sel = "", session?: ClientSession): Promise<D> {
46
+ const data = await this.repository.findById(id).populate(pop).select(sel).session(session!)
43
47
  return data as D
44
48
  }
45
49
  }