-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
26 lines (23 loc) · 927 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const connectDB = require('./config/database');
const File = require('./models/file');
const path = require('path');
const fs = require('fs');
connectDB();
async function deleteData() {
const dateBefore = new Date(Date.now() - (24 * 60 * 60 * 1000));
const expiredFiles = await File.find({createdAt: { $lt: dateBefore }});
if (expiredFiles.length) {
for (const removeFile of expiredFiles) {
try {
fs.unlinkSync(path.join(__dirname, removeFile.filePath));
await removeFile.remove();
console.log(`File Deleted ${removeFile.fileName}`);
} catch(error) {
console.log(`Error Occured ${removeFile.fileName} ${path.join(__dirname, removeFile.filePath)}`);
console.log(error);
}
}
}
console.log("Operation Completed");
}
deleteData().then(process.exit);