@justair/justair-library 3.2.1 → 3.3.1
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/src/index.js +11 -4
- package/src/models/measurements.js +88 -11
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,7 +3,12 @@ import {
|
|
|
3
3
|
configurationsSchema,
|
|
4
4
|
Configurations,
|
|
5
5
|
} from "./models/configurations.js";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
measurementsSchema,
|
|
8
|
+
Measurements,
|
|
9
|
+
Audit,
|
|
10
|
+
auditSchema,
|
|
11
|
+
} from "./models/measurements.js";
|
|
7
12
|
import {
|
|
8
13
|
monitorRequestsSchema,
|
|
9
14
|
MonitorRequests,
|
|
@@ -31,7 +36,7 @@ import { announcementSchema, Announcements } from "./models/announcements.js";
|
|
|
31
36
|
import { jobsSchema, Jobs } from "./models/jobs.js";
|
|
32
37
|
import { apiKeySchema, ApiKey } from "./models/apiKey.js";
|
|
33
38
|
import { UsageMetrics, usageMetricsSchema } from "./models/usageMetrics.js";
|
|
34
|
-
import Database from "./config/db.js";
|
|
39
|
+
import Database from "./config/db.js"; // Import the new Database class
|
|
35
40
|
import CustomLogger from "./config/logger.js";
|
|
36
41
|
|
|
37
42
|
export function createLoggerInstance({ DATADOG_API_KEY, APPLICATION_NAME }) {
|
|
@@ -39,7 +44,7 @@ export function createLoggerInstance({ DATADOG_API_KEY, APPLICATION_NAME }) {
|
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
export {
|
|
42
|
-
Database,
|
|
47
|
+
Database, // Export the Database class
|
|
43
48
|
adminSchema,
|
|
44
49
|
Admin,
|
|
45
50
|
configurationsSchema,
|
|
@@ -76,4 +81,6 @@ export {
|
|
|
76
81
|
ApiKey,
|
|
77
82
|
UsageMetrics,
|
|
78
83
|
usageMetricsSchema,
|
|
79
|
-
|
|
84
|
+
Audit,
|
|
85
|
+
auditSchema,
|
|
86
|
+
};
|
|
@@ -1,18 +1,95 @@
|
|
|
1
|
-
import mongoose from
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
// Audit Schema
|
|
4
|
+
const auditSchema = mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
7
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
6
8
|
timeUpdated: Date,
|
|
9
|
+
deletedAt: { type: Date, default: Date.now },
|
|
7
10
|
measurements: Object,
|
|
8
|
-
monitorState: String
|
|
9
|
-
},
|
|
10
|
-
|
|
11
|
+
monitorState: String,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
timestamps: true,
|
|
15
|
+
}
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const Audit = mongoose.model("Audit", auditSchema);
|
|
19
|
+
|
|
20
|
+
// Measurements Schema
|
|
21
|
+
const measurementsSchema = mongoose.Schema(
|
|
22
|
+
{
|
|
23
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
24
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
25
|
+
timeUpdated: Date,
|
|
26
|
+
measurements: Object,
|
|
27
|
+
monitorState: String,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
timestamps: true,
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
measurementsSchema.index({ monitorId: 1 });
|
|
35
|
+
measurementsSchema.index({ timeUpdated: 1 });
|
|
36
|
+
|
|
37
|
+
// Pre-hook to log single document deletions
|
|
38
|
+
measurementsSchema.pre("findOneAndDelete", async function () {
|
|
39
|
+
const docToDelete = await this.model.findOne(this.getFilter());
|
|
40
|
+
if (docToDelete) {
|
|
41
|
+
console.log("Logging findOneAndDelete to audit", docToDelete);
|
|
42
|
+
const auditLog = new Audit({
|
|
43
|
+
monitorId: docToDelete.monitorId,
|
|
44
|
+
orgId: docToDelete.orgId,
|
|
45
|
+
timeUpdated: docToDelete.timeUpdated,
|
|
46
|
+
measurements: docToDelete.measurements,
|
|
47
|
+
monitorState: docToDelete.monitorState,
|
|
48
|
+
deletedAt: new Date(),
|
|
49
|
+
});
|
|
50
|
+
await auditLog.save();
|
|
51
|
+
}
|
|
11
52
|
});
|
|
12
53
|
|
|
13
|
-
|
|
14
|
-
measurementsSchema.
|
|
54
|
+
// Pre-hook to log multiple document deletions
|
|
55
|
+
measurementsSchema.pre("deleteMany", async function () {
|
|
56
|
+
console.log("deleteMany pre-hook triggered");
|
|
57
|
+
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
58
|
+
|
|
59
|
+
if (docsToDelete.length) {
|
|
60
|
+
console.log(`Logging ${docsToDelete.length} documents to audit`);
|
|
61
|
+
const auditLogs = docsToDelete.map((doc) => ({
|
|
62
|
+
monitorId: doc.monitorId,
|
|
63
|
+
orgId: doc.orgId,
|
|
64
|
+
timeUpdated: doc.timeUpdated,
|
|
65
|
+
measurements: doc.measurements,
|
|
66
|
+
monitorState: doc.monitorState,
|
|
67
|
+
deletedAt: new Date(),
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
await Audit.insertMany(auditLogs);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Pre-hook to log a single document deletion (for deleteOne)
|
|
75
|
+
measurementsSchema.pre("deleteOne", async function () {
|
|
76
|
+
console.log("deleteOne pre-hook triggered");
|
|
77
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
78
|
+
|
|
79
|
+
if (docToDelete) {
|
|
80
|
+
console.log("Logging deleteOne to audit", docToDelete);
|
|
81
|
+
const auditLog = new Audit({
|
|
82
|
+
monitorId: docToDelete.monitorId,
|
|
83
|
+
orgId: docToDelete.orgId,
|
|
84
|
+
timeUpdated: docToDelete.timeUpdated,
|
|
85
|
+
measurements: docToDelete.measurements,
|
|
86
|
+
monitorState: docToDelete.monitorState,
|
|
87
|
+
deletedAt: new Date(),
|
|
88
|
+
});
|
|
89
|
+
await auditLog.save();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
15
92
|
|
|
16
|
-
const Measurements = mongoose.model(
|
|
93
|
+
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
17
94
|
|
|
18
|
-
export
|
|
95
|
+
export { measurementsSchema, Measurements, Audit, auditSchema };
|