first commit
This commit is contained in:
20
mongodb/config/index.ts
Normal file
20
mongodb/config/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export async function connectMongo() {
|
||||
if (mongoose.connection.readyState === 1) return;
|
||||
|
||||
await mongoose.connect(process.env.MONGO_URI!, {
|
||||
maxPoolSize: 20,
|
||||
serverSelectionTimeoutMS: 5000,
|
||||
});
|
||||
|
||||
console.log("MongoDB connected");
|
||||
|
||||
mongoose.connection.on("error", (err) => {
|
||||
console.error("MongoDB connection error:", err);
|
||||
});
|
||||
|
||||
mongoose.connection.on("disconnected", () => {
|
||||
console.warn("MongoDB disconnected");
|
||||
});
|
||||
}
|
||||
53
mongodb/models/index.ts
Normal file
53
mongodb/models/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import mongoose, {Schema} from "mongoose";
|
||||
|
||||
// API Request Logs
|
||||
const ApiRequestLogSchema = new Schema({
|
||||
method: {type: String, required: true},
|
||||
path: {type: String, required: true},
|
||||
status: {type: Number, required: true},
|
||||
durationMs: {type: Number, required: true},
|
||||
userId: {type: String, required: false},
|
||||
role: {type: String},
|
||||
createdAt: {type: Date, default: Date.now},
|
||||
});
|
||||
|
||||
export const ApiRequestLog = mongoose.model(
|
||||
"ApiRequestLog",
|
||||
ApiRequestLogSchema
|
||||
);
|
||||
|
||||
// Error Logs
|
||||
const ErrorLogSchema = new Schema({
|
||||
message: {type: String, required: true},
|
||||
stack: {type: String},
|
||||
severity: {type: String, default: "error"},
|
||||
createdAt: {type: Date, default: Date.now},
|
||||
});
|
||||
|
||||
export const ErrorLog = mongoose.model("ErrorLog", ErrorLogSchema);
|
||||
|
||||
// Security Event Logs
|
||||
const SecurityEventSchema = new Schema({
|
||||
type: {type: String, required: true},
|
||||
userId: {type: String},
|
||||
ip: {type: String},
|
||||
createdAt: {type: Date, default: Date.now},
|
||||
});
|
||||
|
||||
export const SecurityEventLog = mongoose.model(
|
||||
"SecurityEventLog",
|
||||
SecurityEventSchema
|
||||
);
|
||||
|
||||
// Performance Logs
|
||||
const PerformanceLogSchema = new Schema({
|
||||
metric: {type: String, required: true},
|
||||
valueMs: {type: Number, required: true},
|
||||
endpoint: {type: String},
|
||||
createdAt: {type: Date, default: Date.now},
|
||||
});
|
||||
|
||||
export const PerformanceLog = mongoose.model(
|
||||
"PerformanceLog",
|
||||
PerformanceLogSchema
|
||||
);
|
||||
Reference in New Issue
Block a user