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 matches = sequelize.define( 'matches', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, start_time: { type: DataTypes.DATE, }, end_time: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); matches.associate = (db) => { db.matches.belongsToMany(db.teams, { as: 'teams', foreignKey: { name: 'matches_teamsId', }, constraints: false, through: 'matchesTeamsTeams', }); db.matches.belongsToMany(db.teams, { as: 'teams_filter', foreignKey: { name: 'matches_teamsId', }, constraints: false, through: 'matchesTeamsTeams', }); db.matches.belongsToMany(db.streams, { as: 'streams', foreignKey: { name: 'matches_streamsId', }, constraints: false, through: 'matchesStreamsStreams', }); db.matches.belongsToMany(db.streams, { as: 'streams_filter', foreignKey: { name: 'matches_streamsId', }, constraints: false, through: 'matchesStreamsStreams', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.matches.hasMany(db.streams, { as: 'streams_match', foreignKey: { name: 'matchId', }, constraints: false, }); //end loop db.matches.belongsTo(db.users, { as: 'createdBy', }); db.matches.belongsTo(db.users, { as: 'updatedBy', }); }; return matches; };