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 crops = sequelize.define( 'crops', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); crops.associate = (db) => { db.crops.belongsToMany(db.farm_groups, { as: 'farm_groups', foreignKey: { name: 'crops_farm_groupsId', }, constraints: false, through: 'cropsFarm_groupsFarm_groups', }); db.crops.belongsToMany(db.farm_groups, { as: 'farm_groups_filter', foreignKey: { name: 'crops_farm_groupsId', }, constraints: false, through: 'cropsFarm_groupsFarm_groups', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.crops.hasMany(db.diagnoses, { as: 'diagnoses_crop', foreignKey: { name: 'cropId', }, constraints: false, }); //end loop db.crops.belongsTo(db.farmgroup, { as: 'farmgroup', foreignKey: { name: 'farmgroupId', }, constraints: false, }); db.crops.belongsTo(db.users, { as: 'createdBy', }); db.crops.belongsTo(db.users, { as: 'updatedBy', }); }; return crops; };