@dynamatix/gb-schemas 0.23.4 → 0.23.6
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/package.json +1 -1
- package/utils/encryption.js +1 -1
- package/utils/encryption.middleware.js +30 -24
package/package.json
CHANGED
package/utils/encryption.js
CHANGED
|
@@ -56,7 +56,7 @@ export const encryptObject = (obj, collectionName) => {
|
|
|
56
56
|
|
|
57
57
|
let encryptedObj = {};
|
|
58
58
|
for (const key in obj) {
|
|
59
|
-
if (key === '_id' || isObjectIdOrArray(obj[key])) {
|
|
59
|
+
if (key === '_id' || isObjectIdOrArray(obj[key]) || key.startsWith("$")) {
|
|
60
60
|
encryptedObj[key] = obj[key]; // Keep _id and ObjectId references unchanged
|
|
61
61
|
} else {
|
|
62
62
|
try {
|
|
@@ -32,20 +32,28 @@ export default function mongooseEncryption(schema, options) {
|
|
|
32
32
|
next();
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
// Encrypt before
|
|
36
|
-
schema.pre("update", function (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
// 🔹 Encrypt fields before `updateMany` or `update`
|
|
36
|
+
schema.pre(["update", "updateMany"], function (next) {
|
|
37
|
+
const update = this.getUpdate();
|
|
38
|
+
|
|
39
|
+
if (!update) return next(); // ✅ Skip if no update object
|
|
40
|
+
|
|
41
|
+
// ✅ Ensure we are not modifying _id or ObjectIds
|
|
42
|
+
this.setUpdate(encryptObject(update, this.mongooseCollection.name));
|
|
43
|
+
|
|
44
|
+
next();
|
|
42
45
|
});
|
|
43
46
|
|
|
44
|
-
// Encrypt
|
|
45
|
-
schema.pre(["updateOne","findOneAndUpdate"], function (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
// 🔹 Encrypt fields before `updateOne` or `findOneAndUpdate`
|
|
48
|
+
schema.pre(["updateOne", "findOneAndUpdate"], function (next) {
|
|
49
|
+
const update = this.getUpdate();
|
|
50
|
+
|
|
51
|
+
if (!update) return next(); // ✅ Skip if no update object
|
|
52
|
+
|
|
53
|
+
// ✅ Encrypt only the modified fields
|
|
54
|
+
this.setUpdate(encryptObject(update, this.mongooseCollection.name));
|
|
55
|
+
|
|
56
|
+
next();
|
|
49
57
|
});
|
|
50
58
|
|
|
51
59
|
// Encrypt before bulkWrite operations
|
|
@@ -113,32 +121,30 @@ export default function mongooseEncryption(schema, options) {
|
|
|
113
121
|
});
|
|
114
122
|
|
|
115
123
|
|
|
116
|
-
|
|
117
|
-
|
|
118
124
|
// Decrypt after update operations (single or multiple documents)
|
|
119
|
-
schema.post("update", function (result) {
|
|
125
|
+
schema.post(["update","create","save"], function (result, next) {
|
|
120
126
|
if (Array.isArray(result)) {
|
|
121
127
|
result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName)));
|
|
122
128
|
} else {
|
|
123
129
|
result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
124
130
|
}
|
|
131
|
+
next();
|
|
125
132
|
});
|
|
126
133
|
|
|
127
|
-
// Decrypt after updateOne operations
|
|
128
|
-
schema.post("updateOne", function (result) {
|
|
129
|
-
if (result && result.toObject) {
|
|
130
|
-
result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
|
|
135
134
|
// Decrypt after findOneAndUpdate operations
|
|
136
|
-
schema.post(["findOneAndUpdate","findOneAndDelete","deleteOne","remove"], function (doc) {
|
|
135
|
+
schema.post(["findOneAndUpdate","updateOne","findOneAndDelete","deleteOne","remove"], function (doc) {
|
|
137
136
|
if (doc) {
|
|
138
137
|
doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
|
|
139
138
|
}
|
|
140
139
|
});
|
|
141
140
|
|
|
141
|
+
// Decrypt after deleteMany operations
|
|
142
|
+
schema.post("updateMany", function (result) {
|
|
143
|
+
if (Array.isArray(result)) {
|
|
144
|
+
result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName))); // Decrypt document fields
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
142
148
|
// Decrypt after deleteMany operations
|
|
143
149
|
schema.post("deleteMany", function (result) {
|
|
144
150
|
if (Array.isArray(result)) {
|