c2-mongoose 2.1.235 → 2.1.240

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;
@@ -1,4 +1,4 @@
1
- import mongoose from "mongoose";
1
+ import mongoose, { Schema } from "mongoose";
2
2
  import { SearchOptions, SearchResponse } from "../types/SearchResponse";
3
3
  export interface IPopulate {
4
4
  [key: string]: any;
@@ -22,6 +22,7 @@ declare class SearcherFlow<D> {
22
22
  prepareSearch(search: any): void;
23
23
  search(options: SearchOptions): Promise<SearchResponse<D>>;
24
24
  transformObject(originalObject: any): any;
25
+ extractFieldsOfType(schema: Schema, typing?: string, parent?: string): string[];
25
26
  buildDefaultFilters(objectSearch: any): any;
26
27
  isValidObjectId(value: string): boolean;
27
28
  buildOrdenation(): any;
@@ -46,6 +46,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
49
58
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
59
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
60
  };
@@ -178,12 +187,25 @@ var SearcherFlow = /** @class */ (function () {
178
187
  var hasPagination = (originalObject === null || originalObject === void 0 ? void 0 : originalObject.paging) ? true : false;
179
188
  return __assign(__assign({}, transformedObject), { metadata: metadata, paging: hasPagination ? (originalObject === null || originalObject === void 0 ? void 0 : originalObject.paging[0]) || { total: 0, page: 1, limit: this.limit } : undefined });
180
189
  };
190
+ SearcherFlow.prototype.extractFieldsOfType = function (schema, typing, parent) {
191
+ var _this = this;
192
+ if (typing === void 0) { typing = 'String'; }
193
+ if (parent === void 0) { parent = ''; }
194
+ var fieldsTyped = [];
195
+ Object.keys(schema.paths).forEach(function (field) {
196
+ if (schema.paths[field].instance === typing) {
197
+ fieldsTyped.push("".concat(parent !== '' ? "".concat(parent, ".") : '').concat(field));
198
+ }
199
+ else if (schema.paths[field].instance === 'Embedded') {
200
+ fieldsTyped = __spreadArray(__spreadArray([], fieldsTyped, true), _this.extractFieldsOfType(schema.paths[field].schema, typing, field), true);
201
+ }
202
+ });
203
+ return fieldsTyped;
204
+ };
181
205
  SearcherFlow.prototype.buildDefaultFilters = function (objectSearch) {
182
206
  var _this = this;
183
207
  // Recuperar todos os campos do tipo String
184
- var camposString = Object.keys(this.model.schema.paths).filter(function (campo) {
185
- return _this.model.schema.paths[campo].instance === 'String';
186
- });
208
+ var camposString = this.extractFieldsOfType(this.model.schema, 'String');
187
209
  var filters = { $and: [] };
188
210
  Object.entries(objectSearch).forEach(function (_a) {
189
211
  var key = _a[0], value = _a[1];
@@ -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.235",
3
+ "version": "2.1.240",
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
 
@@ -1,5 +1,5 @@
1
1
  import moment from "moment"
2
- import mongoose, { ClientSession, Types } from "mongoose"
2
+ import mongoose, { ClientSession, Schema, Types } from "mongoose"
3
3
  import { SearchOptions, SearchResponse } from "../types/SearchResponse"
4
4
  import { isNotEmpty } from "../utils/Utils"
5
5
  import BuildPopulateSingleFlowItem from "./item/BuildPopulateSingleFlowItem"
@@ -152,12 +152,23 @@ class SearcherFlow<D> {
152
152
  };
153
153
  }
154
154
 
155
+ extractFieldsOfType(schema: Schema, typing = 'String', parent = ''): string[]{
156
+ let fieldsTyped: string[] = []
157
+ Object.keys(schema.paths).forEach((field: string) => {
158
+ if (schema.paths[field].instance === typing) {
159
+ fieldsTyped.push(`${parent !== '' ? `${parent}.` : ''}${field}`)
160
+ } else if (schema.paths[field].instance === 'Embedded') {
161
+ fieldsTyped = [...fieldsTyped, ...this.extractFieldsOfType(schema.paths[field].schema, typing, field)]
162
+ }
163
+ })
164
+
165
+ return fieldsTyped
166
+ }
167
+
155
168
  public buildDefaultFilters(objectSearch: any) {
156
169
 
157
170
  // Recuperar todos os campos do tipo String
158
- const camposString = Object.keys(this.model.schema.paths).filter((campo: string) => {
159
- return this.model.schema.paths[campo].instance === 'String';
160
- });
171
+ const camposString = this.extractFieldsOfType(this.model.schema, 'String')
161
172
 
162
173
 
163
174
  let filters = { $and: [] } as any
@@ -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
  })