Create shopping server
This commit is contained in:
50
server/models/ShoppingItem.js
Normal file
50
server/models/ShoppingItem.js
Normal 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;
|
||||
};
|
Reference in New Issue
Block a user