c2-mongoose 2.1.111 → 2.1.113
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.
- package/dist/flow/C2Flow.d.ts +5 -2
- package/dist/flow/C2Flow.js +44 -4
- package/dist/flow/item/BuildLogFlowItem.d.ts +2 -1
- package/dist/flow/item/BuildLogFlowItem.js +2 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -2
- package/dist/model/Logger.d.ts +5 -124
- package/dist/model/Logger.js +2 -4
- package/dist/utils/Utils.d.ts +2 -0
- package/dist/utils/Utils.js +6 -1
- package/package.json +1 -1
- package/src/flow/C2Flow.ts +31 -15
- package/src/flow/item/BuildLogFlowItem.ts +3 -1
- package/src/index.ts +6 -3
- package/src/model/Logger.ts +3 -3
- package/src/utils/Utils.ts +8 -0
package/dist/flow/C2Flow.d.ts
CHANGED
|
@@ -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<
|
|
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;
|
package/dist/flow/C2Flow.js
CHANGED
|
@@ -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);
|
|
60
|
-
return [4 /*yield*/,
|
|
59
|
+
log = BuildLogFlowItem_1.default.build(options, dataAfter[0]._doc, Logger_1.TypeOfOperation.CREATE, this.repository);
|
|
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
13
|
user: (_a = options.owner) !== null && _a !== void 0 ? _a : express_http_context_1.default.get("user")._id,
|
|
14
14
|
owner: options.owner,
|
|
15
15
|
data: dataAfter,
|
|
16
|
+
collection: repository.collection.name,
|
|
16
17
|
operation: operation
|
|
17
18
|
};
|
|
18
19
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import C2Flow from "./flow/C2Flow";
|
|
2
2
|
import CrudFlow from "./flow/CrudFlow";
|
|
3
3
|
import SearchFlow from "./flow/SearchFlow";
|
|
4
|
-
import { ILogger, LoggerModel,
|
|
4
|
+
import { ILogger, LoggerModel, LoggerSearch, TypeOfOperation } from "./model/Logger";
|
|
5
5
|
import { Options } from "./types/Options";
|
|
6
6
|
import { Pagination, SearchResponse } from "./types/SearchResponse";
|
|
7
|
-
|
|
7
|
+
import { initialize } from "./utils/Utils";
|
|
8
|
+
export { C2Flow, CrudFlow, ILogger, LoggerModel, LoggerSearch, Options, Pagination, SearchFlow, SearchResponse, TypeOfOperation, initialize };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.initialize = exports.TypeOfOperation = exports.SearchFlow = exports.LoggerSearch = exports.LoggerModel = exports.CrudFlow = exports.C2Flow = void 0;
|
|
7
7
|
var C2Flow_1 = __importDefault(require("./flow/C2Flow"));
|
|
8
8
|
exports.C2Flow = C2Flow_1.default;
|
|
9
9
|
var CrudFlow_1 = __importDefault(require("./flow/CrudFlow"));
|
|
@@ -12,6 +12,8 @@ var SearchFlow_1 = __importDefault(require("./flow/SearchFlow"));
|
|
|
12
12
|
exports.SearchFlow = SearchFlow_1.default;
|
|
13
13
|
var Logger_1 = require("./model/Logger");
|
|
14
14
|
Object.defineProperty(exports, "LoggerModel", { enumerable: true, get: function () { return Logger_1.LoggerModel; } });
|
|
15
|
-
Object.defineProperty(exports, "LoggerRepository", { enumerable: true, get: function () { return Logger_1.LoggerRepository; } });
|
|
16
15
|
Object.defineProperty(exports, "LoggerSearch", { enumerable: true, get: function () { return Logger_1.LoggerSearch; } });
|
|
17
16
|
Object.defineProperty(exports, "TypeOfOperation", { enumerable: true, get: function () { return Logger_1.TypeOfOperation; } });
|
|
17
|
+
var Utils_1 = require("./utils/Utils");
|
|
18
|
+
Object.defineProperty(exports, "initialize", { enumerable: true, get: function () { return Utils_1.initialize; } });
|
|
19
|
+
global.LoggerRepository = undefined;
|
package/dist/model/Logger.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ declare enum TypeOfOperation {
|
|
|
9
9
|
interface ILogger {
|
|
10
10
|
owner: Types.ObjectId;
|
|
11
11
|
user: Types.ObjectId;
|
|
12
|
+
collection: string;
|
|
12
13
|
createdAtDateTime: moment.Moment;
|
|
13
14
|
operation: TypeOfOperation;
|
|
14
15
|
data: any;
|
|
@@ -18,6 +19,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
|
|
|
18
19
|
createdAt: string;
|
|
19
20
|
};
|
|
20
21
|
}>, {
|
|
22
|
+
collection: string;
|
|
21
23
|
owner: {
|
|
22
24
|
prototype?: Types.ObjectId | undefined;
|
|
23
25
|
cacheHexString?: unknown;
|
|
@@ -37,6 +39,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
|
|
|
37
39
|
operation: string;
|
|
38
40
|
data?: any;
|
|
39
41
|
}, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
|
|
42
|
+
collection: string;
|
|
40
43
|
owner: {
|
|
41
44
|
prototype?: Types.ObjectId | undefined;
|
|
42
45
|
cacheHexString?: unknown;
|
|
@@ -56,6 +59,7 @@ declare const LoggerModel: mongoose.Schema<any, mongoose.Model<any, any, any, an
|
|
|
56
59
|
operation: string;
|
|
57
60
|
data?: any;
|
|
58
61
|
}>> & Omit<mongoose.FlatRecord<{
|
|
62
|
+
collection: string;
|
|
59
63
|
owner: {
|
|
60
64
|
prototype?: Types.ObjectId | undefined;
|
|
61
65
|
cacheHexString?: unknown;
|
|
@@ -82,127 +86,4 @@ declare class LoggerSearch extends SearchFlow {
|
|
|
82
86
|
constructor(params: any);
|
|
83
87
|
buildFilters(): void;
|
|
84
88
|
}
|
|
85
|
-
|
|
86
|
-
owner: {
|
|
87
|
-
prototype?: Types.ObjectId | undefined;
|
|
88
|
-
cacheHexString?: unknown;
|
|
89
|
-
generate?: {} | undefined;
|
|
90
|
-
createFromTime?: {} | undefined;
|
|
91
|
-
createFromHexString?: {} | undefined;
|
|
92
|
-
isValid?: {} | undefined;
|
|
93
|
-
};
|
|
94
|
-
user: {
|
|
95
|
-
prototype?: Types.ObjectId | undefined;
|
|
96
|
-
cacheHexString?: unknown;
|
|
97
|
-
generate?: {} | undefined;
|
|
98
|
-
createFromTime?: {} | undefined;
|
|
99
|
-
createFromHexString?: {} | undefined;
|
|
100
|
-
isValid?: {} | undefined;
|
|
101
|
-
};
|
|
102
|
-
operation: string;
|
|
103
|
-
data?: any;
|
|
104
|
-
}, {}, {}, {}, mongoose.Document<unknown, {}, {
|
|
105
|
-
owner: {
|
|
106
|
-
prototype?: Types.ObjectId | undefined;
|
|
107
|
-
cacheHexString?: unknown;
|
|
108
|
-
generate?: {} | undefined;
|
|
109
|
-
createFromTime?: {} | undefined;
|
|
110
|
-
createFromHexString?: {} | undefined;
|
|
111
|
-
isValid?: {} | undefined;
|
|
112
|
-
};
|
|
113
|
-
user: {
|
|
114
|
-
prototype?: Types.ObjectId | undefined;
|
|
115
|
-
cacheHexString?: unknown;
|
|
116
|
-
generate?: {} | undefined;
|
|
117
|
-
createFromTime?: {} | undefined;
|
|
118
|
-
createFromHexString?: {} | undefined;
|
|
119
|
-
isValid?: {} | undefined;
|
|
120
|
-
};
|
|
121
|
-
operation: string;
|
|
122
|
-
data?: any;
|
|
123
|
-
}> & Omit<{
|
|
124
|
-
owner: {
|
|
125
|
-
prototype?: Types.ObjectId | undefined;
|
|
126
|
-
cacheHexString?: unknown;
|
|
127
|
-
generate?: {} | undefined;
|
|
128
|
-
createFromTime?: {} | undefined;
|
|
129
|
-
createFromHexString?: {} | undefined;
|
|
130
|
-
isValid?: {} | undefined;
|
|
131
|
-
};
|
|
132
|
-
user: {
|
|
133
|
-
prototype?: Types.ObjectId | undefined;
|
|
134
|
-
cacheHexString?: unknown;
|
|
135
|
-
generate?: {} | undefined;
|
|
136
|
-
createFromTime?: {} | undefined;
|
|
137
|
-
createFromHexString?: {} | undefined;
|
|
138
|
-
isValid?: {} | undefined;
|
|
139
|
-
};
|
|
140
|
-
operation: string;
|
|
141
|
-
data?: any;
|
|
142
|
-
} & {
|
|
143
|
-
_id: Types.ObjectId;
|
|
144
|
-
}, never>, mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, mongoose.ResolveSchemaOptions<{
|
|
145
|
-
timestamps: {
|
|
146
|
-
createdAt: string;
|
|
147
|
-
};
|
|
148
|
-
}>, {
|
|
149
|
-
owner: {
|
|
150
|
-
prototype?: Types.ObjectId | undefined;
|
|
151
|
-
cacheHexString?: unknown;
|
|
152
|
-
generate?: {} | undefined;
|
|
153
|
-
createFromTime?: {} | undefined;
|
|
154
|
-
createFromHexString?: {} | undefined;
|
|
155
|
-
isValid?: {} | undefined;
|
|
156
|
-
};
|
|
157
|
-
user: {
|
|
158
|
-
prototype?: Types.ObjectId | undefined;
|
|
159
|
-
cacheHexString?: unknown;
|
|
160
|
-
generate?: {} | undefined;
|
|
161
|
-
createFromTime?: {} | undefined;
|
|
162
|
-
createFromHexString?: {} | undefined;
|
|
163
|
-
isValid?: {} | undefined;
|
|
164
|
-
};
|
|
165
|
-
operation: string;
|
|
166
|
-
data?: any;
|
|
167
|
-
}, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
|
|
168
|
-
owner: {
|
|
169
|
-
prototype?: Types.ObjectId | undefined;
|
|
170
|
-
cacheHexString?: unknown;
|
|
171
|
-
generate?: {} | undefined;
|
|
172
|
-
createFromTime?: {} | undefined;
|
|
173
|
-
createFromHexString?: {} | undefined;
|
|
174
|
-
isValid?: {} | undefined;
|
|
175
|
-
};
|
|
176
|
-
user: {
|
|
177
|
-
prototype?: Types.ObjectId | undefined;
|
|
178
|
-
cacheHexString?: unknown;
|
|
179
|
-
generate?: {} | undefined;
|
|
180
|
-
createFromTime?: {} | undefined;
|
|
181
|
-
createFromHexString?: {} | undefined;
|
|
182
|
-
isValid?: {} | undefined;
|
|
183
|
-
};
|
|
184
|
-
operation: string;
|
|
185
|
-
data?: any;
|
|
186
|
-
}>> & Omit<mongoose.FlatRecord<{
|
|
187
|
-
owner: {
|
|
188
|
-
prototype?: Types.ObjectId | undefined;
|
|
189
|
-
cacheHexString?: unknown;
|
|
190
|
-
generate?: {} | undefined;
|
|
191
|
-
createFromTime?: {} | undefined;
|
|
192
|
-
createFromHexString?: {} | undefined;
|
|
193
|
-
isValid?: {} | undefined;
|
|
194
|
-
};
|
|
195
|
-
user: {
|
|
196
|
-
prototype?: Types.ObjectId | undefined;
|
|
197
|
-
cacheHexString?: unknown;
|
|
198
|
-
generate?: {} | undefined;
|
|
199
|
-
createFromTime?: {} | undefined;
|
|
200
|
-
createFromHexString?: {} | undefined;
|
|
201
|
-
isValid?: {} | undefined;
|
|
202
|
-
};
|
|
203
|
-
operation: string;
|
|
204
|
-
data?: any;
|
|
205
|
-
}> & {
|
|
206
|
-
_id: Types.ObjectId;
|
|
207
|
-
}, never>>>;
|
|
208
|
-
export { ILogger, TypeOfOperation, LoggerModel, LoggerRepository, LoggerSearch };
|
|
89
|
+
export { ILogger, TypeOfOperation, LoggerModel, LoggerSearch };
|
package/dist/model/Logger.js
CHANGED
|
@@ -41,7 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
41
41
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.LoggerSearch = exports.
|
|
44
|
+
exports.LoggerSearch = exports.LoggerModel = exports.TypeOfOperation = void 0;
|
|
45
45
|
var mongoose_1 = __importStar(require("mongoose"));
|
|
46
46
|
var SearchFlow_1 = __importDefault(require("../flow/SearchFlow"));
|
|
47
47
|
var Utils_1 = require("../utils/Utils");
|
|
@@ -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
|
+
collection: { 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
|
}, {
|
|
@@ -90,6 +91,3 @@ var LoggerSearch = /** @class */ (function (_super) {
|
|
|
90
91
|
return LoggerSearch;
|
|
91
92
|
}(SearchFlow_1.default));
|
|
92
93
|
exports.LoggerSearch = LoggerSearch;
|
|
93
|
-
var LoggerRepository = (0, mongoose_1.model)('testeLogger', LoggerModel);
|
|
94
|
-
exports.LoggerRepository = LoggerRepository;
|
|
95
|
-
LoggerRepository.createCollection();
|
package/dist/utils/Utils.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Connection } from "mongoose";
|
|
2
|
+
export declare const initialize: (connection: Connection) => void;
|
|
1
3
|
export declare const isNotEmpty: (obj: any) => boolean;
|
|
2
4
|
export declare const isEmpty: (obj: any) => boolean;
|
|
3
5
|
export declare const isIterable: (input: any) => boolean;
|
package/dist/utils/Utils.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.compareDesc = exports.compareAsc = exports.isNotIterable = exports.isIterable = exports.isEmpty = exports.isNotEmpty = void 0;
|
|
3
|
+
exports.compareDesc = exports.compareAsc = exports.isNotIterable = exports.isIterable = exports.isEmpty = exports.isNotEmpty = exports.initialize = void 0;
|
|
4
|
+
var Logger_1 = require("../model/Logger");
|
|
5
|
+
var initialize = function (connection) {
|
|
6
|
+
global.LoggerRepository = connection.model('c2-mongoose-logger', Logger_1.LoggerModel);
|
|
7
|
+
};
|
|
8
|
+
exports.initialize = initialize;
|
|
4
9
|
var isNotEmpty = function (obj) {
|
|
5
10
|
return !(0, exports.isEmpty)(obj);
|
|
6
11
|
};
|
package/package.json
CHANGED
package/src/flow/C2Flow.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import mongoose from "mongoose"
|
|
3
|
-
import { ILogger,
|
|
2
|
+
import mongoose, { Types } from "mongoose"
|
|
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
|
-
|
|
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
|
-
await LoggerRepository.create([log], { session: options.session })
|
|
23
|
+
let log: Partial<ILogger> = BuildLogFlowItem.build(options, dataAfter[0]._doc, TypeOfOperation.CREATE, this.repository)
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
9
|
user: options.owner ?? httpContext.get("user")._id,
|
|
9
10
|
owner: options.owner,
|
|
10
11
|
data: dataAfter,
|
|
12
|
+
collection: repository.collection.name,
|
|
11
13
|
operation
|
|
12
14
|
}
|
|
13
15
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import mongoose from "mongoose"
|
|
2
1
|
import C2Flow from "./flow/C2Flow"
|
|
3
2
|
import CrudFlow from "./flow/CrudFlow"
|
|
4
3
|
import SearchFlow from "./flow/SearchFlow"
|
|
5
|
-
import { ILogger, LoggerModel,
|
|
4
|
+
import { ILogger, LoggerModel, LoggerSearch, TypeOfOperation } from "./model/Logger"
|
|
6
5
|
import { Options } from "./types/Options"
|
|
7
6
|
import { Pagination, SearchResponse } from "./types/SearchResponse"
|
|
7
|
+
import { initialize } from "./utils/Utils"
|
|
8
|
+
|
|
9
|
+
(global as any).LoggerRepository = undefined
|
|
10
|
+
|
|
11
|
+
export { C2Flow, CrudFlow, ILogger, LoggerModel, LoggerSearch, Options, Pagination, SearchFlow, SearchResponse, TypeOfOperation, initialize }
|
|
8
12
|
|
|
9
|
-
export { CrudFlow, C2Flow, Options, SearchFlow, SearchResponse, Pagination, ILogger, TypeOfOperation, LoggerModel, LoggerRepository, LoggerSearch }
|
package/src/model/Logger.ts
CHANGED
|
@@ -12,6 +12,7 @@ enum TypeOfOperation {
|
|
|
12
12
|
interface ILogger {
|
|
13
13
|
owner: Types.ObjectId
|
|
14
14
|
user: Types.ObjectId
|
|
15
|
+
collection: 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
|
+
collection: { 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
|
}, {
|
|
@@ -57,7 +59,5 @@ class LoggerSearch extends SearchFlow {
|
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
const LoggerRepository = model('testeLogger', LoggerModel)
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
export { ILogger, TypeOfOperation, LoggerModel, LoggerRepository, LoggerSearch }
|
|
63
|
+
export { ILogger, TypeOfOperation, LoggerModel, LoggerSearch }
|
package/src/utils/Utils.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { Connection } from "mongoose"
|
|
2
|
+
import { LoggerModel } from "../model/Logger"
|
|
3
|
+
|
|
4
|
+
export const initialize = (connection: Connection) => {
|
|
5
|
+
|
|
6
|
+
(global as any).LoggerRepository = connection.model('c2-mongoose-logger', LoggerModel)
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
export const isNotEmpty = (obj: any) => {
|
|
2
10
|
return !isEmpty(obj)
|
|
3
11
|
}
|