From 41c0134cb5f140f7f65329b82847db19de6d280e Mon Sep 17 00:00:00 2001 From: Atallah Date: Sat, 4 Nov 2023 15:21:56 +0200 Subject: [PATCH] craete image model --- models/imagesModle.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 models/imagesModle.js diff --git a/models/imagesModle.js b/models/imagesModle.js new file mode 100644 index 0000000..cb90535 --- /dev/null +++ b/models/imagesModle.js @@ -0,0 +1,43 @@ +const DataTypes = require('sequelize'); +const sequelize = require('../utils/dataBaseConnection'); +const Product = require('./productModel'); + +const Image = sequelize.define('image', { + id: { + type: DataTypes.INTEGER(15), + allowNull: false, + autoIncrement: true, + primaryKey: true + }, + imageUrl: { + type: DataTypes.STRING(255), + allowNull: false, + validate: { + notEmpty: { + msg: 'Image URL cannot be empty', + }, + isURL: { + msg: 'Invalid URL format', + }, + }, + }, + productId: { + type: DataTypes.INTEGER(15), + allowNull: false, + references: { + model: Product, + key: 'id', + onDelete: 'CASCADE', + }, + }, +}, { + freezeTableName: true, + timestamps: false, +}); + +// Associations +Image.belongsTo(Product, { onDelete: 'cascade', hooks: true }, { foreignKey: 'productId' }); +Product.hasMany(Image, { foreignKey: 'productId' }); + + +module.exports = Image;