-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b54f713
commit c1cfc70
Showing
14 changed files
with
303 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const logEntrySchema = new mongoose.Schema({ | ||
timestamp: { | ||
type: Date, | ||
default: Date.now, | ||
required: true | ||
}, | ||
user: { | ||
type: String, | ||
required: true, | ||
}, | ||
logMessage: { | ||
type: String, | ||
required: true | ||
} | ||
}); | ||
|
||
// Middleware to ensure only the latest 200 entries are kept | ||
logEntrySchema.pre('save', async function (next) { | ||
try { | ||
const count = await mongoose.models.LogEntry.countDocuments(); | ||
if (count >= 200) { | ||
// Find and delete the oldest entry | ||
const oldestEntry = await mongoose.models.LogEntry.findOne().sort({ timestamp: 1 }); | ||
|
||
if (oldestEntry) { | ||
await oldestEntry.deleteOne(); | ||
} | ||
} | ||
next(); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
const LogEntry = mongoose.model('LogEntry', logEntrySchema); | ||
|
||
module.exports = LogEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.