43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const { Sequelize } = require('sequelize');
|
|
const path = require('path');
|
|
|
|
// Initialize Sequelize with SQLite
|
|
const sequelize = new Sequelize({
|
|
dialect: 'sqlite',
|
|
storage: path.join(__dirname, '../database.sqlite'),
|
|
logging: false, // Set to console.log to see SQL queries
|
|
});
|
|
|
|
// Import models
|
|
const ShoppingItem = require('./ShoppingItem')(sequelize);
|
|
|
|
// Function to clean up checked items older than 2 hours
|
|
const cleanupCheckedItems = async () => {
|
|
try {
|
|
const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000);
|
|
const deletedCount = await ShoppingItem.destroy({
|
|
where: {
|
|
checked: true,
|
|
checkedAt: {
|
|
[Sequelize.Op.lt]: twoHoursAgo,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (deletedCount > 0) {
|
|
console.log(`Cleaned up ${deletedCount} checked items older than 2 hours`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error cleaning up checked items:', error);
|
|
}
|
|
};
|
|
|
|
// Run cleanup every 30 minutes
|
|
setInterval(cleanupCheckedItems, 30 * 60 * 1000);
|
|
|
|
module.exports = {
|
|
sequelize,
|
|
ShoppingItem,
|
|
cleanupCheckedItems,
|
|
};
|