c2-mongoose 2.1.239 → 2.1.241

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.
@@ -8,6 +8,7 @@ declare class C2Flow<D> {
8
8
  createMany(data: Partial<D>[], options: Partial<Options>): Promise<Partial<D>[]>;
9
9
  updateById(_id: Types.ObjectId, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>>;
10
10
  updateByModel(searcher: SearchFlow, data: Partial<D>, options: Partial<Options>): Promise<Partial<D>>;
11
+ updateMany(searcher: SearchFlow, data: D, options: Options): Promise<any>;
11
12
  deleteById(_id: Types.ObjectId, options: Partial<Options>): Promise<void>;
12
13
  deleteByModel(searcher: SearchFlow, options: Partial<Options>): Promise<void>;
13
14
  }
@@ -155,6 +155,35 @@ var C2Flow = /** @class */ (function () {
155
155
  });
156
156
  });
157
157
  };
158
+ C2Flow.prototype.updateMany = function (searcher, data, options) {
159
+ return __awaiter(this, void 0, void 0, function () {
160
+ var dataAfter, log;
161
+ return __generator(this, function (_a) {
162
+ switch (_a.label) {
163
+ case 0: return [4 /*yield*/, this.repository.updateMany(searcher.filters, data, { returnDocument: 'after', session: options.session })];
164
+ case 1:
165
+ dataAfter = _a.sent();
166
+ if (options.logger === false) {
167
+ if ((0, Utils_1.isEmpty)(dataAfter)) {
168
+ return [2 /*return*/, dataAfter];
169
+ }
170
+ return [2 /*return*/, dataAfter];
171
+ }
172
+ if ((0, Utils_1.isEmpty)(dataAfter)) {
173
+ return [2 /*return*/, dataAfter];
174
+ }
175
+ log = BuildLogFlowItem_1.default.build(options, __assign({ filters: searcher.filters }, data), Logger_1.TypeOfOperation.UPDATE, this.repository);
176
+ return [4 /*yield*/, global.LoggerRepository.create([log], { session: options.session })];
177
+ case 2:
178
+ _a.sent();
179
+ if ((0, Utils_1.isEmpty)(dataAfter)) {
180
+ return [2 /*return*/, dataAfter];
181
+ }
182
+ return [2 /*return*/, dataAfter];
183
+ }
184
+ });
185
+ });
186
+ };
158
187
  C2Flow.prototype.deleteById = function (_id, options) {
159
188
  return __awaiter(this, void 0, void 0, function () {
160
189
  var dataAfter, log;
@@ -2,7 +2,7 @@ import mongoose from "mongoose";
2
2
  import { IPopulate } from "../SearcherFlow";
3
3
  declare class BuildPopulateSingleFlowItem {
4
4
  buildPopulate(model: mongoose.Model<any>, populatesStr: string[], selectsStr: string[]): IPopulate[];
5
- buildPath(target: string, nested?: string): IPopulate;
5
+ buildPath(target: string, nested: string | undefined, populateAcc: IPopulate): IPopulate;
6
6
  }
7
7
  declare const _default: BuildPopulateSingleFlowItem;
8
8
  export default _default;
@@ -16,15 +16,16 @@ var BuildPopulateSingleFlowItem = /** @class */ (function () {
16
16
  function BuildPopulateSingleFlowItem() {
17
17
  }
18
18
  BuildPopulateSingleFlowItem.prototype.buildPopulate = function (model, populatesStr, selectsStr) {
19
- var _a, _b, _c, _d;
20
- var populate = [];
19
+ var _a;
20
+ var populateArray = [];
21
21
  if ((0, Utils_1.isEmpty)(populatesStr)) {
22
22
  return [];
23
23
  }
24
24
  var _loop_1 = function () {
25
- var _e = property.split('.'), first = _e[0], rest = _e.slice(1);
25
+ var _b = property.split('.'), first = _b[0], rest = _b.slice(1);
26
26
  var nested = rest.join('.');
27
- var populateLocal = this_1.buildPath(first, nested);
27
+ var existing2 = (_a = populateArray.find(function (populateAux) { return populateAux.path === first; })) !== null && _a !== void 0 ? _a : { path: '', populate: {} };
28
+ var populateLocal = this_1.buildPath(first, nested, existing2);
28
29
  //EXISTE UMA LIMITACAO DE SELECT A PARTIR DO 3 NIVEL DENTRO DOS CAMPO POPULATE, MELHORAR O CODIGO ABAIXO PARA PERCORRER MAIS NIVEIS
29
30
  if (Array.isArray(selectsStr)) {
30
31
  var allSelectOfPopulate = selectsStr.filter(function (sel) { return sel.startsWith("".concat(first, ".").concat(populateLocal.path)); });
@@ -35,31 +36,42 @@ var BuildPopulateSingleFlowItem = /** @class */ (function () {
35
36
  }
36
37
  }
37
38
  //FIM
38
- var existing = populate.find(function (populateAux) { return populateAux.path === populateLocal.path; });
39
- if (existing) {
40
- var populateAux = __assign(__assign({}, existing === null || existing === void 0 ? void 0 : existing.populate), { path: "".concat((_b = (_a = existing.populate) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : "", " ").concat((_d = (_c = populateLocal.populate) === null || _c === void 0 ? void 0 : _c.path) !== null && _d !== void 0 ? _d : "").trim() });
41
- existing.populate = populateAux;
42
- return "continue";
43
- }
44
- populate.push(populateLocal);
39
+ // const existing = populateArray.find((populateAux: IPopulate) => populateAux.path === populateLocal.path)
40
+ // if (existing) {
41
+ // if (populateLocal.populate && existing.populate){
42
+ // if (!existing.populate.path.includes(populateLocal.populate.path)){
43
+ // existing.populate.path += populateLocal.populate.path
44
+ // }
45
+ // }
46
+ // const populateAux = {
47
+ // ...existing?.populate,
48
+ // path: `${existing.populate?.path ?? ``} ${populateLocal.populate?.path ?? ``}`.trim()
49
+ // }
50
+ // existing.populate = populateAux
51
+ // continue
52
+ // }
53
+ populateArray.push(populateLocal);
45
54
  };
46
55
  var this_1 = this;
47
56
  for (var _i = 0, populatesStr_1 = populatesStr; _i < populatesStr_1.length; _i++) {
48
57
  var property = populatesStr_1[_i];
49
58
  _loop_1();
50
59
  }
51
- return populate;
60
+ return populateArray;
52
61
  };
53
- BuildPopulateSingleFlowItem.prototype.buildPath = function (target, nested) {
62
+ BuildPopulateSingleFlowItem.prototype.buildPath = function (target, nested, populateAcc) {
63
+ // var populate = {} as IPopulate
64
+ var _a;
54
65
  if (nested === void 0) { nested = ""; }
55
- var populate = {};
56
- populate.path = target;
66
+ if (!((_a = populateAcc.path) === null || _a === void 0 ? void 0 : _a.includes(target))) {
67
+ populateAcc.path += " ".concat(target);
68
+ }
57
69
  if ((0, Utils_1.isNotEmpty)(nested)) {
58
- var _a = nested.split('.'), first = _a[0], rest = _a.slice(1);
70
+ var _b = nested.split('.'), first = _b[0], rest = _b.slice(1);
59
71
  var nested2 = rest.join('.');
60
- populate.populate = this.buildPath(first, nested2);
72
+ populateAcc.populate = __assign(__assign({}, populateAcc.populate), this.buildPath(first, nested2, populateAcc.populate));
61
73
  }
62
- return populate;
74
+ return populateAcc;
63
75
  };
64
76
  return BuildPopulateSingleFlowItem;
65
77
  }());
@@ -13,6 +13,8 @@ interface ILogger {
13
13
  createdAtDateTime: moment.Moment;
14
14
  operation: TypeOfOperation;
15
15
  data: any;
16
+ filters: any;
17
+ notes: string;
16
18
  }
17
19
  declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, mongoose.ResolveSchemaOptions<{
18
20
  timestamps: {
@@ -25,18 +27,24 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
25
27
  collectionName: string;
26
28
  operation: string;
27
29
  data?: any;
30
+ filters?: any;
31
+ notes?: string | undefined;
28
32
  }, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
29
33
  owner: Types.ObjectId;
30
34
  user: Types.ObjectId;
31
35
  collectionName: string;
32
36
  operation: string;
33
37
  data?: any;
38
+ filters?: any;
39
+ notes?: string | undefined;
34
40
  }>> & Omit<mongoose.FlatRecord<{
35
41
  owner: Types.ObjectId;
36
42
  user: Types.ObjectId;
37
43
  collectionName: string;
38
44
  operation: string;
39
45
  data?: any;
46
+ filters?: any;
47
+ notes?: string | undefined;
40
48
  }> & {
41
49
  _id: Types.ObjectId;
42
50
  }, never>>;
@@ -57,7 +57,9 @@ var LoggerModel = new mongoose_1.Schema({
57
57
  user: { type: mongoose_1.default.Schema.Types.ObjectId, required: true, immutable: true },
58
58
  collectionName: { type: String, required: true, immutable: true },
59
59
  operation: { type: String, enum: TypeOfOperation, required: true, immutable: true },
60
- data: { type: mongoose_1.default.Schema.Types.Mixed, immutable: true }
60
+ data: { type: mongoose_1.default.Schema.Types.Mixed, immutable: true },
61
+ filters: { type: mongoose_1.default.Schema.Types.Mixed, immutable: true },
62
+ notes: { type: String, immutable: true }
61
63
  }, {
62
64
  timestamps: { createdAt: 'createdAtDateTime', updatedAt: 'updatedAtDateTime' }
63
65
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.239",
3
+ "version": "2.1.241",
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,5 +1,5 @@
1
1
 
2
- import mongoose, { Types } from "mongoose"
2
+ import mongoose, { QueryWithHelpers, Types, UpdateWriteOpResult } from "mongoose"
3
3
  import { ILogger, TypeOfOperation } from "../model/Logger"
4
4
  import { Options } from "../types/Options"
5
5
  import BuildLogFlowItem from "./item/BuildLogFlowItem"
@@ -83,6 +83,27 @@ class C2Flow<D> {
83
83
  return dataAfter._doc
84
84
  }
85
85
 
86
+ async updateMany(searcher: SearchFlow, data: D, options: Options): Promise<any> {
87
+ let dataAfter = await this.repository.updateMany(searcher.filters, data as Partial<D>, { returnDocument: 'after', session: options.session })
88
+ if (options.logger === false) {
89
+ if (isEmpty(dataAfter)) {
90
+ return dataAfter
91
+ }
92
+ return dataAfter
93
+ }
94
+
95
+ if (isEmpty(dataAfter)) {
96
+ return dataAfter
97
+ }
98
+
99
+ let log: Partial<ILogger> = BuildLogFlowItem.build(options, { filters: searcher.filters, ...data }, TypeOfOperation.UPDATE, this.repository)
100
+ await (global as any).LoggerRepository.create([log], { session: options.session })
101
+ if (isEmpty(dataAfter)) {
102
+ return dataAfter
103
+ }
104
+ return dataAfter
105
+ }
106
+
86
107
 
87
108
  public async deleteById(_id: Types.ObjectId, options: Partial<Options>) {
88
109
 
@@ -5,7 +5,7 @@ import { IPopulate } from "../SearcherFlow";
5
5
  class BuildPopulateSingleFlowItem {
6
6
 
7
7
  public buildPopulate(model: mongoose.Model<any>, populatesStr: string[], selectsStr: string[]): IPopulate[] {
8
- var populate = [] as IPopulate[]
8
+ var populateArray = [] as IPopulate[]
9
9
  if (isEmpty(populatesStr)) {
10
10
  return []
11
11
  }
@@ -14,7 +14,9 @@ class BuildPopulateSingleFlowItem {
14
14
  let [first, ...rest] = property.split('.')
15
15
  let nested = rest.join('.')
16
16
 
17
- let populateLocal = this.buildPath(first, nested)
17
+ const existing2 = populateArray.find((populateAux: IPopulate) => populateAux.path === first) ?? { path: '', populate: {} }
18
+
19
+ let populateLocal = this.buildPath(first, nested, existing2 as IPopulate)
18
20
 
19
21
  //EXISTE UMA LIMITACAO DE SELECT A PARTIR DO 3 NIVEL DENTRO DOS CAMPO POPULATE, MELHORAR O CODIGO ABAIXO PARA PERCORRER MAIS NIVEIS
20
22
  if (Array.isArray(selectsStr)) {
@@ -27,31 +29,45 @@ class BuildPopulateSingleFlowItem {
27
29
  }
28
30
  //FIM
29
31
 
30
- const existing = populate.find((populateAux: IPopulate) => populateAux.path === populateLocal.path)
31
- if (existing) {
32
- const populateAux = {
33
- ...existing?.populate,
34
- path: `${existing.populate?.path ?? ``} ${populateLocal.populate?.path ?? ``}`.trim()
35
- }
36
- existing.populate = populateAux
37
- continue
38
- }
32
+ // const existing = populateArray.find((populateAux: IPopulate) => populateAux.path === populateLocal.path)
33
+ // if (existing) {
34
+
35
+ // if (populateLocal.populate && existing.populate){
36
+ // if (!existing.populate.path.includes(populateLocal.populate.path)){
37
+ // existing.populate.path += populateLocal.populate.path
38
+ // }
39
+ // }
39
40
 
40
- populate.push(populateLocal)
41
+ // const populateAux = {
42
+ // ...existing?.populate,
43
+ // path: `${existing.populate?.path ?? ``} ${populateLocal.populate?.path ?? ``}`.trim()
44
+ // }
45
+ // existing.populate = populateAux
46
+ // continue
47
+ // }
48
+
49
+ populateArray.push(populateLocal)
41
50
  }
42
51
 
43
- return populate
52
+ return populateArray
44
53
  }
45
54
 
46
- public buildPath(target: string, nested: string = ""): IPopulate {
47
- var populate = {} as IPopulate
48
- populate.path = target
55
+ public buildPath(target: string, nested: string = "", populateAcc: IPopulate): IPopulate {
56
+ // var populate = {} as IPopulate
57
+
58
+ if (!populateAcc.path?.includes(target)){
59
+ populateAcc.path += ` ${target}`
60
+ }
61
+
49
62
  if (isNotEmpty(nested)) {
50
63
  let [first, ...rest] = nested.split('.')
51
64
  let nested2 = rest.join('.')
52
- populate.populate = this.buildPath(first, nested2)
65
+ populateAcc.populate = {
66
+ ...populateAcc.populate,
67
+ ...this.buildPath(first, nested2, populateAcc.populate)
68
+ }
53
69
  }
54
- return populate
70
+ return populateAcc
55
71
  }
56
72
  }
57
73
 
@@ -16,6 +16,8 @@ interface ILogger {
16
16
  createdAtDateTime: moment.Moment
17
17
  operation: TypeOfOperation
18
18
  data: any
19
+ filters: any
20
+ notes: string
19
21
  }
20
22
 
21
23
  const LoggerModel = new Schema({
@@ -23,7 +25,9 @@ const LoggerModel = new Schema({
23
25
  user: { type: mongoose.Schema.Types.ObjectId, required: true, immutable: true },
24
26
  collectionName: { type: String, required: true, immutable: true },
25
27
  operation: { type: String, enum: TypeOfOperation, required: true, immutable: true },
26
- data: { type: mongoose.Schema.Types.Mixed, immutable: true }
28
+ data: { type: mongoose.Schema.Types.Mixed, immutable: true },
29
+ filters: { type: mongoose.Schema.Types.Mixed, immutable: true },
30
+ notes: { type: String, immutable: true }
27
31
  }, {
28
32
  timestamps: { createdAt: 'createdAtDateTime', updatedAt: 'updatedAtDateTime' }
29
33
  })