@dynamatix/gb-schemas 0.21.14 → 0.21.15
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 +2 -2
- package/utils/encryption.middleware.js +60 -44
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamatix/gb-schemas",
|
|
3
|
-
"version": "0.21.
|
|
4
|
-
"description": "All the schemas for gatehouse bank back-end",
|
|
3
|
+
"version": "0.21.15",
|
|
4
|
+
"description": "All the schemas for gatehouse bank back-end.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -7,7 +7,7 @@ dotenv.config();
|
|
|
7
7
|
const EXCLUDED_COLLECTIONS = process.env.EXCLUDED_COLLECTIONS?.split(",") || [];
|
|
8
8
|
|
|
9
9
|
export default function mongooseEncryption(schema, options) {
|
|
10
|
-
console.log("========MONGOOSE ENCRYPTION IS IN PROGRESS
|
|
10
|
+
console.log("========MONGOOSE ENCRYPTION IS IN PROGRESS");
|
|
11
11
|
|
|
12
12
|
const collectionName = options?.collection || schema.options.collection;
|
|
13
13
|
|
|
@@ -16,74 +16,90 @@ export default function mongooseEncryption(schema, options) {
|
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// Encrypt query parameters before execution
|
|
20
20
|
schema.pre(['find', 'findOne', 'findById'], function (next) {
|
|
21
|
-
|
|
22
|
-
this.
|
|
21
|
+
this.getQuery(); // Ensure the query is processed
|
|
22
|
+
const encryptedQuery = encryptObject(this.getQuery(), collectionName);
|
|
23
|
+
this.setQuery(encryptedQuery);
|
|
23
24
|
next();
|
|
24
25
|
});
|
|
25
26
|
|
|
26
|
-
// ** Decrypt result after querying **
|
|
27
|
-
schema.post(['find', 'findOne', 'findById'], function (docs) {
|
|
28
|
-
if (Array.isArray(docs)) {
|
|
29
|
-
docs.forEach((doc) => doc && Object.assign(doc, decryptObject(doc.toObject(), collectionName)));
|
|
30
|
-
} else if (docs) {
|
|
31
|
-
Object.assign(docs, decryptObject(docs.toObject(), collectionName));
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
27
|
|
|
35
|
-
//
|
|
36
|
-
schema.pre(['save',
|
|
37
|
-
|
|
28
|
+
// Encrypt before saving (e.g., insert, update)
|
|
29
|
+
schema.pre(['save','create'], function (next) {
|
|
30
|
+
// Encrypt the document before saving to the database
|
|
31
|
+
this.set(encryptObject(this.toObject(), collectionName)); // Encrypt the document
|
|
38
32
|
next();
|
|
39
33
|
});
|
|
40
34
|
|
|
41
|
-
//
|
|
42
|
-
schema.pre(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
// Encrypt before update operations (single or multiple documents)
|
|
36
|
+
schema.pre("update", function (result) {
|
|
37
|
+
if (Array.isArray(result)) {
|
|
38
|
+
result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName)));
|
|
39
|
+
} else {
|
|
40
|
+
result.set(encryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
46
41
|
}
|
|
47
|
-
next();
|
|
48
42
|
});
|
|
49
43
|
|
|
50
|
-
//
|
|
51
|
-
schema.pre("
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
operations.forEach((operation) => {
|
|
55
|
-
if (operation.insertOne?.document) {
|
|
56
|
-
operation.insertOne.document = encryptObject(operation.insertOne.document, collectionName);
|
|
57
|
-
} else if (operation.updateOne?.update) {
|
|
58
|
-
operation.updateOne.update = encryptObject(operation.updateOne.update, collectionName);
|
|
59
|
-
} else if (operation.updateMany?.update) {
|
|
60
|
-
operation.updateMany.update = encryptObject(operation.updateMany.update, collectionName);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
44
|
+
// Encrypt after updateOne operations
|
|
45
|
+
schema.pre(["updateOne","findOneAndUpdate"], function (result) {
|
|
46
|
+
if (result && result.toObject) {
|
|
47
|
+
result.set(encryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
63
48
|
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Encrypt before bulkWrite operations
|
|
52
|
+
schema.pre("bulkWrite", function (next, operations) {
|
|
53
|
+
operations.forEach(operation => {
|
|
54
|
+
if (operation.insertOne && operation.insertOne.document) {
|
|
55
|
+
operation.insertOne.document = encryptObject(operation.insertOne.document, collectionName);
|
|
56
|
+
} else if (operation.updateOne && operation.updateOne.update) {
|
|
57
|
+
operation.updateOne.update = encryptObject(operation.updateOne.update, collectionName);
|
|
58
|
+
} else if (operation.updateMany && operation.updateMany.update) {
|
|
59
|
+
operation.updateMany.update = encryptObject(operation.updateMany.update, collectionName);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
64
62
|
next();
|
|
65
63
|
});
|
|
64
|
+
|
|
66
65
|
|
|
67
|
-
//
|
|
68
|
-
schema.post(
|
|
66
|
+
// Decrypt result after querying
|
|
67
|
+
schema.post(['find', 'findOne', 'findById'], function (docs) {
|
|
69
68
|
if (Array.isArray(docs)) {
|
|
70
|
-
docs.forEach((doc) =>
|
|
69
|
+
docs.forEach((doc) => doc && doc.set(decryptObject(doc.toObject(), collectionName)));
|
|
70
|
+
} else if (docs) {
|
|
71
|
+
docs.set(decryptObject(docs.toObject(), collectionName));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Decrypt after update operations (single or multiple documents)
|
|
76
|
+
schema.post("update", function (result) {
|
|
77
|
+
if (Array.isArray(result)) {
|
|
78
|
+
result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName)));
|
|
71
79
|
} else {
|
|
72
|
-
|
|
80
|
+
result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
73
81
|
}
|
|
74
82
|
});
|
|
75
83
|
|
|
76
|
-
//
|
|
77
|
-
schema.post(
|
|
84
|
+
// Decrypt after updateOne operations
|
|
85
|
+
schema.post("updateOne", function (result) {
|
|
78
86
|
if (result && result.toObject) {
|
|
79
|
-
|
|
87
|
+
result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
|
|
80
88
|
}
|
|
81
89
|
});
|
|
82
90
|
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
|
|
92
|
+
// Decrypt after findOneAndUpdate operations
|
|
93
|
+
schema.post(["findOneAndUpdate","findOneAndDelete","deleteOne","remove"], function (doc) {
|
|
85
94
|
if (doc) {
|
|
86
|
-
|
|
95
|
+
doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Decrypt after deleteMany operations
|
|
100
|
+
schema.post("deleteMany", function (result) {
|
|
101
|
+
if (Array.isArray(result)) {
|
|
102
|
+
result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName))); // Decrypt document fields
|
|
87
103
|
}
|
|
88
104
|
});
|
|
89
105
|
}
|