c2-mongoose 2.1.112 → 2.1.114

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,8 +1,11 @@
1
- import mongoose from "mongoose";
1
+ import mongoose, { Types } from "mongoose";
2
2
  import { Options } from "../types/Options";
3
+ import SearchFlow from "./SearchFlow";
3
4
  declare class C2Flow<D> {
4
5
  private repository;
5
6
  constructor(repository: mongoose.Model<any>);
6
- create(data: Partial<D>, options: Partial<Options>): Promise<any[]>;
7
+ create(data: Partial<D>, options: Partial<Options>): Promise<Partial<D>>;
8
+ updateById(id: Types.ObjectId, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>>;
9
+ updateByModel(searcher: SearchFlow, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>>;
7
10
  }
8
11
  export default C2Flow;
@@ -54,13 +54,53 @@ var C2Flow = /** @class */ (function () {
54
54
  case 1:
55
55
  dataAfter = _a.sent();
56
56
  if (options.logger === false) {
57
- return [2 /*return*/, dataAfter];
57
+ return [2 /*return*/, dataAfter[0]._doc];
58
58
  }
59
- log = BuildLogFlowItem_1.default.build(options, dataAfter, Logger_1.TypeOfOperation.CREATE);
59
+ log = BuildLogFlowItem_1.default.build(options, dataAfter[0]._doc, Logger_1.TypeOfOperation.CREATE, this.repository);
60
60
  return [4 /*yield*/, global.LoggerRepository.create([log], { session: options.session })];
61
61
  case 2:
62
62
  _a.sent();
63
- return [2 /*return*/, dataAfter];
63
+ return [2 /*return*/, dataAfter[0]._doc];
64
+ }
65
+ });
66
+ });
67
+ };
68
+ C2Flow.prototype.updateById = function (id, data, options) {
69
+ return __awaiter(this, void 0, void 0, function () {
70
+ var dataAfter, log;
71
+ return __generator(this, function (_a) {
72
+ switch (_a.label) {
73
+ case 0: return [4 /*yield*/, this.repository.findByIdAndUpdate(id, data, { returnDocument: 'after', session: options.session })];
74
+ case 1:
75
+ dataAfter = _a.sent();
76
+ if (options.logger === false) {
77
+ return [2 /*return*/, dataAfter._doc];
78
+ }
79
+ log = BuildLogFlowItem_1.default.build(options, dataAfter._doc, Logger_1.TypeOfOperation.UPDATE, this.repository);
80
+ return [4 /*yield*/, global.LoggerRepository.create([log], { session: options.session })];
81
+ case 2:
82
+ _a.sent();
83
+ return [2 /*return*/, dataAfter._doc];
84
+ }
85
+ });
86
+ });
87
+ };
88
+ C2Flow.prototype.updateByModel = function (searcher, data, options) {
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var dataAfter, log;
91
+ return __generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, this.repository.findOneAndUpdate(searcher.filters, data, { session: options.session })];
94
+ case 1:
95
+ dataAfter = _a.sent();
96
+ if (options.logger === false) {
97
+ return [2 /*return*/, dataAfter._doc];
98
+ }
99
+ log = BuildLogFlowItem_1.default.build(options, dataAfter._doc, Logger_1.TypeOfOperation.UPDATE, this.repository);
100
+ return [4 /*yield*/, global.LoggerRepository.create([log], { session: options.session })];
101
+ case 2:
102
+ _a.sent();
103
+ return [2 /*return*/, dataAfter._doc];
64
104
  }
65
105
  });
66
106
  });
@@ -1,7 +1,8 @@
1
1
  import { ILogger, TypeOfOperation } from "../../model/Logger";
2
2
  import { Options } from "../../types/Options";
3
+ import mongoose from "mongoose";
3
4
  declare class BuildLogFlowItem {
4
- build(options: Partial<Options>, dataAfter: any[], operation: TypeOfOperation): Partial<ILogger>;
5
+ build(options: Partial<Options>, dataAfter: any[], operation: TypeOfOperation, repository: mongoose.Model<any>): Partial<ILogger>;
5
6
  }
6
7
  declare const _default: BuildLogFlowItem;
7
8
  export default _default;
@@ -7,12 +7,13 @@ var express_http_context_1 = __importDefault(require("express-http-context"));
7
7
  var BuildLogFlowItem = /** @class */ (function () {
8
8
  function BuildLogFlowItem() {
9
9
  }
10
- BuildLogFlowItem.prototype.build = function (options, dataAfter, operation) {
10
+ BuildLogFlowItem.prototype.build = function (options, dataAfter, operation, repository) {
11
11
  var _a;
12
12
  return {
13
- user: (_a = options.owner) !== null && _a !== void 0 ? _a : express_http_context_1.default.get("user")._id,
13
+ user: (_a = options.user) !== null && _a !== void 0 ? _a : express_http_context_1.default.get("user")._id,
14
14
  owner: options.owner,
15
15
  data: dataAfter,
16
+ collectionName: repository.collection.name,
16
17
  operation: operation
17
18
  };
18
19
  };
@@ -9,6 +9,7 @@ declare enum TypeOfOperation {
9
9
  interface ILogger {
10
10
  owner: Types.ObjectId;
11
11
  user: Types.ObjectId;
12
+ collectionName: string;
12
13
  createdAtDateTime: moment.Moment;
13
14
  operation: TypeOfOperation;
14
15
  data: any;
@@ -34,6 +35,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
34
35
  createFromHexString?: {} | undefined;
35
36
  isValid?: {} | undefined;
36
37
  };
38
+ collectionName: string;
37
39
  operation: string;
38
40
  data?: any;
39
41
  }, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
@@ -53,6 +55,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
53
55
  createFromHexString?: {} | undefined;
54
56
  isValid?: {} | undefined;
55
57
  };
58
+ collectionName: string;
56
59
  operation: string;
57
60
  data?: any;
58
61
  }>> & Omit<mongoose.FlatRecord<{
@@ -72,6 +75,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
72
75
  createFromHexString?: {} | undefined;
73
76
  isValid?: {} | undefined;
74
77
  };
78
+ collectionName: string;
75
79
  operation: string;
76
80
  data?: any;
77
81
  }> & {
@@ -55,6 +55,7 @@ exports.TypeOfOperation = TypeOfOperation;
55
55
  var LoggerModel = new mongoose_1.Schema({
56
56
  owner: { type: mongoose_1.Types.ObjectId, required: true, immutable: true },
57
57
  user: { type: mongoose_1.Types.ObjectId, required: true, immutable: true },
58
+ collectionName: { type: String, required: true, immutable: true },
58
59
  operation: { type: String, enum: TypeOfOperation, required: true, immutable: true },
59
60
  data: { type: mongoose_1.default.Schema.Types.Mixed, immutable: true }
60
61
  }, {
@@ -78,7 +79,7 @@ var LoggerSearch = /** @class */ (function (_super) {
78
79
  var regex = this.buildRegex(this.searchText);
79
80
  var condition = {
80
81
  $or: [
81
- // { 'name': { $regex: regex } },
82
+ { 'collectionName': { $regex: regex } },
82
83
  ]
83
84
  };
84
85
  filters.$and.push(condition);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.112",
3
+ "version": "2.1.114",
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,8 +1,9 @@
1
1
 
2
- import mongoose from "mongoose"
2
+ import mongoose, { Types } from "mongoose"
3
3
  import { ILogger, TypeOfOperation } from "../model/Logger"
4
4
  import { Options } from "../types/Options"
5
5
  import BuildLogFlowItem from "./item/BuildLogFlowItem"
6
+ import SearchFlow from "./SearchFlow"
6
7
 
7
8
  class C2Flow<D> {
8
9
 
@@ -12,29 +13,44 @@ class C2Flow<D> {
12
13
  this.repository = repository
13
14
  }
14
15
 
15
- public async create(data: Partial<D>, options: Partial<Options>) {
16
+ async create(data: Partial<D>, options: Partial<Options>): Promise<Partial<D>> {
16
17
  let dataAfter = await this.repository.create([data], { session: options.session })
17
18
 
18
19
  if (options.logger === false) {
19
- return dataAfter
20
+ return dataAfter[0]._doc
20
21
  }
21
22
 
22
- let log: Partial<ILogger> = BuildLogFlowItem.build(options, dataAfter, TypeOfOperation.CREATE)
23
+ let log: Partial<ILogger> = BuildLogFlowItem.build(options, dataAfter[0]._doc, TypeOfOperation.CREATE, this.repository)
23
24
  await (global as any).LoggerRepository.create([log], { session: options.session })
24
25
 
25
- return dataAfter
26
+ return dataAfter[0]._doc
26
27
  }
27
28
 
29
+ async updateById(id: Types.ObjectId, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>> {
30
+ let dataAfter = await this.repository.findByIdAndUpdate(id, data, { returnDocument: 'after', session: options.session })
28
31
 
29
- // public async updateById(id: Types.ObjectId, data: any, session: any = undefined): Promise<D> {
30
- // const dataAfter = await this.repository.findByIdAndUpdate(id, data, { returnDocument: 'after', session })
31
- // return dataAfter as D
32
- // }
32
+ if (options.logger === false) {
33
+ return dataAfter._doc
34
+ }
35
+
36
+ let log: Partial<ILogger> = BuildLogFlowItem.build(options, dataAfter._doc, TypeOfOperation.UPDATE, this.repository)
37
+ await (global as any).LoggerRepository.create([log], { session: options.session })
38
+
39
+ return dataAfter._doc
40
+ }
41
+
42
+ async updateByModel(searcher: SearchFlow, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>> {
43
+ let dataAfter = await this.repository.findOneAndUpdate(searcher.filters, data, { session: options.session })
33
44
 
34
- // public async updateByModel(searcher: SearchFlow, data: any, params: any): Promise<D> {
35
- // const dataAfter = await this.repository.findOneAndUpdate(searcher.filters, data, params)
36
- // return dataAfter as D
37
- // }
45
+ if (options.logger === false) {
46
+ return dataAfter._doc
47
+ }
48
+
49
+ let log: Partial<ILogger> = BuildLogFlowItem.build(options, dataAfter._doc, TypeOfOperation.UPDATE, this.repository)
50
+ await (global as any).LoggerRepository.create([log], { session: options.session })
51
+
52
+ return dataAfter._doc
53
+ }
38
54
  }
39
55
 
40
56
  export default C2Flow
@@ -1,13 +1,15 @@
1
1
  import httpContext from "express-http-context"
2
2
  import { ILogger, TypeOfOperation } from "../../model/Logger"
3
3
  import { Options } from "../../types/Options"
4
+ import mongoose from "mongoose"
4
5
 
5
6
  class BuildLogFlowItem {
6
- build(options: Partial<Options>, dataAfter: any[], operation: TypeOfOperation): Partial<ILogger> {
7
+ build(options: Partial<Options>, dataAfter: any[], operation: TypeOfOperation, repository: mongoose.Model<any>): Partial<ILogger> {
7
8
  return {
8
- user: options.owner ?? httpContext.get("user")._id,
9
+ user: options.user ?? httpContext.get("user")._id,
9
10
  owner: options.owner,
10
11
  data: dataAfter,
12
+ collectionName: repository.collection.name,
11
13
  operation
12
14
  }
13
15
  }
@@ -12,6 +12,7 @@ enum TypeOfOperation {
12
12
  interface ILogger {
13
13
  owner: Types.ObjectId
14
14
  user: Types.ObjectId
15
+ collectionName: string
15
16
  createdAtDateTime: moment.Moment
16
17
  operation: TypeOfOperation
17
18
  data: any
@@ -20,6 +21,7 @@ interface ILogger {
20
21
  const LoggerModel = new Schema({
21
22
  owner: { type: Types.ObjectId, required: true, immutable: true },
22
23
  user: { type: Types.ObjectId, required: true, immutable: true },
24
+ collectionName: { type: String, required: true, immutable: true },
23
25
  operation: { type: String, enum: TypeOfOperation, required: true, immutable: true },
24
26
  data: { type: mongoose.Schema.Types.Mixed, immutable: true }
25
27
  }, {
@@ -45,7 +47,7 @@ class LoggerSearch extends SearchFlow {
45
47
  let regex = this.buildRegex(this.searchText)
46
48
  let condition = {
47
49
  $or: [
48
- // { 'name': { $regex: regex } },
50
+ { 'collectionName': { $regex: regex } },
49
51
  ]
50
52
  }
51
53
  filters.$and.push(condition)