33830/backend/src/db/models/products.js
2025-09-02 17:37:39 +00:00

76 lines
1.5 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,
},
product_name: {
type: DataTypes.TEXT,
},
price: {
type: DataTypes.DECIMAL,
},
stock_quantity: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
products.associate = (db) => {
db.products.belongsToMany(db.orders, {
as: 'orders',
foreignKey: {
name: 'products_ordersId',
},
constraints: false,
through: 'productsOrdersOrders',
});
db.products.belongsToMany(db.orders, {
as: 'orders_filter',
foreignKey: {
name: 'products_ordersId',
},
constraints: false,
through: 'productsOrdersOrders',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};