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 poultry_farms = sequelize.define( 'poultry_farms', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, number_of_chicks: { type: DataTypes.INTEGER, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); poultry_farms.associate = (db) => { db.poultry_farms.belongsToMany(db.products, { as: 'products', foreignKey: { name: 'poultry_farms_productsId', }, constraints: false, through: 'poultry_farmsProductsProducts', }); db.poultry_farms.belongsToMany(db.products, { as: 'products_filter', foreignKey: { name: 'poultry_farms_productsId', }, constraints: false, through: 'poultry_farmsProductsProducts', }); db.poultry_farms.belongsToMany(db.exit_vouchers, { as: 'exit_vouchers', foreignKey: { name: 'poultry_farms_exit_vouchersId', }, constraints: false, through: 'poultry_farmsExit_vouchersExit_vouchers', }); db.poultry_farms.belongsToMany(db.exit_vouchers, { as: 'exit_vouchers_filter', foreignKey: { name: 'poultry_farms_exit_vouchersId', }, constraints: false, through: 'poultry_farmsExit_vouchersExit_vouchers', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.poultry_farms.hasMany(db.exit_vouchers, { as: 'exit_vouchers_poultry_farm', foreignKey: { name: 'poultry_farmId', }, constraints: false, }); db.poultry_farms.hasMany(db.products, { as: 'products_poultry_farm', foreignKey: { name: 'poultry_farmId', }, constraints: false, }); //end loop db.poultry_farms.belongsTo(db.users, { as: 'createdBy', }); db.poultry_farms.belongsTo(db.users, { as: 'updatedBy', }); }; return poultry_farms; };