45 lines
1.2 KiB
JavaScript
45 lines
1.2 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: process.env.NODE_ENV === 'development' ? console.log : false
|
|
});
|
|
|
|
// Import and initialize models
|
|
const createShoppingItem = require('./ShoppingItem');
|
|
const createCalendarUser = require('./CalendarUser');
|
|
|
|
const ShoppingItem = createShoppingItem(sequelize);
|
|
const CalendarUser = createCalendarUser(sequelize);
|
|
|
|
// Function to clean up checked shopping items older than 24 hours
|
|
const cleanupCheckedItems = async () => {
|
|
try {
|
|
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
|
const deletedCount = await ShoppingItem.destroy({
|
|
where: {
|
|
checked: true,
|
|
updatedAt: {
|
|
[Sequelize.Op.lt]: oneDayAgo
|
|
}
|
|
}
|
|
});
|
|
|
|
if (deletedCount > 0) {
|
|
console.log(`Cleaned up ${deletedCount} checked shopping items older than 24 hours`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error during cleanup:', error);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
sequelize,
|
|
ShoppingItem,
|
|
CalendarUser,
|
|
cleanupCheckedItems
|
|
};
|