1
0

Create shopping server

This commit is contained in:
2025-07-18 10:48:00 +02:00
parent 6426e333f9
commit 99d531ba8c
9 changed files with 4108 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
const { DataTypes } = require('sequelize');
module.exports = (sequelize) => {
const ShoppingItem = sequelize.define('ShoppingItem', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
amount: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: '1',
},
checked: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
checkedAt: {
type: DataTypes.DATE,
allowNull: true,
},
date: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
}, {
timestamps: true,
hooks: {
beforeUpdate: (item, options) => {
// Set checkedAt timestamp when item is checked
if (item.checked && !item.previous('checked')) {
item.checkedAt = new Date();
} else if (!item.checked) {
item.checkedAt = null;
}
},
},
});
return ShoppingItem;
};

42
server/models/index.js Normal file
View File

@@ -0,0 +1,42 @@
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,
};