@dynamatix/gb-schemas 0.18.2 → 0.18.3

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.
@@ -1,4 +1,5 @@
1
1
  import mongoose from "mongoose";
2
+ import mongooseEncryption from "../utils/encryption.middleware";
2
3
 
3
4
  const Schema = mongoose.Schema;
4
5
 
@@ -31,5 +32,6 @@ const solicitorSchema = new Schema({
31
32
  telephone: { type: String,},
32
33
  });
33
34
 
35
+ solicitorSchema.plugin(mongooseEncryption);
34
36
  const SolicitorModel = mongoose.model("Solicitor", solicitorSchema);
35
37
  export default SolicitorModel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/gb-schemas",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "description": "All the schemas for gatehouse bank back-end.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,109 @@
1
+ import dotenv from 'dotenv';
2
+ import { decryptObject, encryptObject } from '@dynamatix/gb-schemas/utils';
3
+
4
+ dotenv.config();
5
+
6
+ // Read excluded collections from .env
7
+ const EXCLUDED_COLLECTIONS = process.env.EXCLUDED_COLLECTIONS?.split(",") || [];
8
+
9
+ export default function mongooseEncryption(schema, options) {
10
+ console.log("========MONGOOSE ENCRYPTION IS IN PROGRESS");
11
+
12
+ const collectionName = options?.collection || schema.options.collection;
13
+
14
+ // Skip encryption for excluded collections
15
+ if (EXCLUDED_COLLECTIONS.includes(collectionName)) {
16
+ return;
17
+ }
18
+
19
+ // Encrypt before saving (e.g., insert, update)
20
+ schema.pre("save", function (next) {
21
+ // Encrypt the document before saving to the database
22
+ this.set(encryptObject(this.toObject(), collectionName)); // Encrypt the document
23
+ next();
24
+ });
25
+
26
+ // Encrypt before creating a document
27
+ schema.pre("create", function (next) {
28
+ // Encrypt only the fields before saving to the database
29
+ this.set(encryptObject(this.toObject(), collectionName)); // Encrypt the document
30
+ next();
31
+ });
32
+
33
+ // Decrypt after creating a document
34
+ schema.post("create", function (docs) {
35
+ for (let doc of docs) {
36
+ doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt document fields
37
+ }
38
+ });
39
+
40
+ // Decrypt after update operations (single or multiple documents)
41
+ schema.post("update", function (result) {
42
+ if (Array.isArray(result)) {
43
+ result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName)));
44
+ } else {
45
+ result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
46
+ }
47
+ });
48
+
49
+ // Decrypt after updateOne operations
50
+ schema.post("updateOne", function (result) {
51
+ if (result && result.toObject) {
52
+ result.set(decryptObject(result.toObject(), collectionName)); // Decrypt single document
53
+ }
54
+ });
55
+
56
+ // Decrypt after find operations (e.g., find, findMany)
57
+ schema.post("find", function (docs) {
58
+ console.log("find", docs);
59
+ docs.forEach(doc => {
60
+ doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt document fields
61
+ });
62
+ });
63
+
64
+ // Decrypt after findById operations
65
+ schema.post("findById", function (doc) {
66
+ console.log("findById", doc);
67
+ if (doc) doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
68
+ });
69
+
70
+ // Decrypt after findOne operations
71
+ schema.post("findOne", function (doc) {
72
+ console.log("findOne", doc);
73
+ if (doc) doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
74
+ });
75
+
76
+ // Decrypt after findOneAndUpdate operations
77
+ schema.post("findOneAndUpdate", function (doc) {
78
+ console.log("findOneAndUpdate", doc);
79
+ if (doc) {
80
+ doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
81
+ }
82
+ });
83
+
84
+ // Decrypt after findOneAndDelete operations
85
+ schema.post("findOneAndDelete", function (doc) {
86
+ console.log("findOneAndDelete", doc);
87
+ if (doc) doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
88
+ });
89
+
90
+ // Decrypt after deleteMany operations
91
+ schema.post("deleteMany", function (result) {
92
+ console.log("deleteMany", result);
93
+ if (Array.isArray(result)) {
94
+ result.forEach(doc => doc.set(decryptObject(doc.toObject(), collectionName))); // Decrypt document fields
95
+ }
96
+ });
97
+
98
+ // Decrypt after deleteOne operations
99
+ schema.post("deleteOne", function (doc) {
100
+ console.log("deleteOne", doc);
101
+ if (doc) doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
102
+ });
103
+
104
+ // Decrypt after remove operations
105
+ schema.post("remove", function (doc) {
106
+ console.log("remove", doc);
107
+ if (doc) doc.set(decryptObject(doc.toObject(), collectionName)); // Decrypt single document
108
+ });
109
+ }