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 optimization_runs = sequelize.define( 'optimization_runs', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, run_start: { type: DataTypes.DATE, }, run_end: { type: DataTypes.DATE, }, assigned_count: { type: DataTypes.INTEGER, }, efficiency: { type: DataTypes.DECIMAL, }, full_pieces_count: { type: DataTypes.INTEGER, }, cut_pieces_count: { type: DataTypes.INTEGER, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); optimization_runs.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.optimization_runs.belongsTo(db.users, { as: 'created_by', foreignKey: { name: 'created_byId', }, constraints: false, }); db.optimization_runs.belongsTo(db.panels, { as: 'panel', foreignKey: { name: 'panelId', }, constraints: false, }); db.optimization_runs.hasMany(db.file, { as: 'scrap_snapshot', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.optimization_runs.getTableName(), belongsToColumn: 'scrap_snapshot', }, }); db.optimization_runs.belongsTo(db.users, { as: 'createdBy', }); db.optimization_runs.belongsTo(db.users, { as: 'updatedBy', }); }; return optimization_runs; };