38871-vm/backend/src/db/models/products.js
2026-02-28 15:48:11 +00:00

144 lines
1.9 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const products = sequelize.define(
'products',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
brand: {
type: DataTypes.TEXT,
},
size_label: {
type: DataTypes.TEXT,
},
unit_price: {
type: DataTypes.DECIMAL,
},
price_per_unit: {
type: DataTypes.DECIMAL,
},
unit_name: {
type: DataTypes.TEXT,
},
is_available: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
product_url: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
products.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.products.hasMany(db.item_matches, {
as: 'item_matches_matched_product',
foreignKey: {
name: 'matched_productId',
},
constraints: false,
});
//end loop
db.products.belongsTo(db.shops, {
as: 'shop',
foreignKey: {
name: 'shopId',
},
constraints: false,
});
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};