@lasnutrias/mongoose-audit-plugin 1.0.0 → 1.1.0
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/audit.plugin.js +16 -2
- package/dist/audit.plugin.js.map +1 -1
- package/package.json +6 -2
package/dist/audit.plugin.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuditPlugin = AuditPlugin;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
4
5
|
function AuditPlugin(schema, options = {}) {
|
|
5
6
|
const createdByField = options.createdBy === true || options.createdBy === undefined
|
|
6
7
|
? 'createdBy'
|
|
@@ -8,7 +9,7 @@ function AuditPlugin(schema, options = {}) {
|
|
|
8
9
|
const updatedByField = options.updatedBy === true || options.updatedBy === undefined
|
|
9
10
|
? 'updatedBy'
|
|
10
11
|
: options.updatedBy;
|
|
11
|
-
const getUser = () => options.currentUser?.()
|
|
12
|
+
const getUser = () => options.currentUser?.() ?? 'UNKNOWN_USER';
|
|
12
13
|
const schemaAdditions = {};
|
|
13
14
|
if (createdByField)
|
|
14
15
|
schemaAdditions[createdByField] = { type: String };
|
|
@@ -26,7 +27,7 @@ function AuditPlugin(schema, options = {}) {
|
|
|
26
27
|
});
|
|
27
28
|
schema.pre(['findOneAndUpdate', 'updateOne', 'updateMany'], function () {
|
|
28
29
|
const user = getUser();
|
|
29
|
-
const update = this.getUpdate();
|
|
30
|
+
const update = (this.getUpdate() ?? {});
|
|
30
31
|
const $set = {};
|
|
31
32
|
const $setOnInsert = {};
|
|
32
33
|
if (updatedByField) {
|
|
@@ -41,5 +42,18 @@ function AuditPlugin(schema, options = {}) {
|
|
|
41
42
|
$setOnInsert: { ...update?.$setOnInsert, ...$setOnInsert },
|
|
42
43
|
});
|
|
43
44
|
});
|
|
45
|
+
schema.pre('insertMany', function (docs) {
|
|
46
|
+
if (!createdByField && !updatedByField)
|
|
47
|
+
return;
|
|
48
|
+
const user = getUser();
|
|
49
|
+
if (Array.isArray(docs)) {
|
|
50
|
+
docs.forEach((doc) => {
|
|
51
|
+
if (createdByField)
|
|
52
|
+
(0, lodash_1.set)(doc, createdByField, user);
|
|
53
|
+
if (updatedByField)
|
|
54
|
+
(0, lodash_1.set)(doc, updatedByField, user);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
44
58
|
}
|
|
45
59
|
//# sourceMappingURL=audit.plugin.js.map
|
package/dist/audit.plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audit.plugin.js","sourceRoot":"","sources":["../src/audit.plugin.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"audit.plugin.js","sourceRoot":"","sources":["../src/audit.plugin.ts"],"names":[],"mappings":";;AAUA,kCAmEC;AA3ED,mCAA6B;AAQ7B,SAAgB,WAAW,CAAC,MAAc,EAAE,UAAwB,EAAE;IAEpE,MAAM,cAAc,GAClB,OAAO,CAAC,SAAS,KAAK,IAAI,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS;QAC3D,CAAC,CAAC,WAAW;QACb,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IAExB,MAAM,cAAc,GAClB,OAAO,CAAC,SAAS,KAAK,IAAI,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS;QAC3D,CAAC,CAAC,WAAW;QACb,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IAExB,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,IAAI,cAAc,CAAC;IAGhE,MAAM,eAAe,GAAkC,EAAE,CAAC;IAC1D,IAAI,cAAc;QAAE,eAAe,CAAC,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IACvE,IAAI,cAAc;QAAE,eAAe,CAAC,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,M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,EAAE,CAAC;QAEvB,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBAEnB,IAAI,cAAc;oBAAE,IAAA,YAAG,EAAC,GAAG,EAAE,cAAc,EAAE,IAAI,CAAC,CAAC;gBACnD,IAAI,cAAc;oBAAE,IAAA,YAAG,EAAC,GAAG,EAAE,cAAc,EAAE,IAAI,CAAC,CAAC;YACrD,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lasnutrias/mongoose-audit-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Allows for createdBy and updatedBy fields automatically set on the entities",
|
|
5
5
|
"author": "@lasnutrias",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
"files": [
|
|
14
14
|
"dist/*.{ts,js,map}"
|
|
15
15
|
],
|
|
16
|
-
"dependencies": {},
|
|
17
16
|
"devDependencies": {
|
|
18
17
|
"@eslint/eslintrc": "^3.2.0",
|
|
19
18
|
"@eslint/js": "^9.18.0",
|
|
@@ -26,6 +25,7 @@
|
|
|
26
25
|
"@nestjs/testing": "^11.0.1",
|
|
27
26
|
"@types/express": "^5.0.0",
|
|
28
27
|
"@types/jest": "^30.0.0",
|
|
28
|
+
"@types/lodash": "^4.17.24",
|
|
29
29
|
"@types/node": "^22.10.7",
|
|
30
30
|
"@types/supertest": "^6.0.2",
|
|
31
31
|
"eslint": "^9.18.0",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"eslint-plugin-prettier": "^5.2.2",
|
|
34
34
|
"globals": "^16.0.0",
|
|
35
35
|
"jest": "^30.0.0",
|
|
36
|
+
"lodash": "^4.17.23",
|
|
36
37
|
"mongodb-memory-server": "^11.0.1",
|
|
37
38
|
"mongoose": "^9.2.1",
|
|
38
39
|
"prettier": "^3.4.2",
|
|
@@ -65,6 +66,9 @@
|
|
|
65
66
|
"coverageDirectory": "../coverage",
|
|
66
67
|
"testEnvironment": "node"
|
|
67
68
|
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"lodash": "^4.17.23"
|
|
71
|
+
},
|
|
68
72
|
"scripts": {
|
|
69
73
|
"build": "rimraf dist && tsc --build tsconfig.build.json",
|
|
70
74
|
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|