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 teams = sequelize.define( 'teams', { 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, }, ); teams.associate = (db) => { db.teams.belongsToMany(db.players, { as: 'players', foreignKey: { name: 'teams_playersId', }, constraints: false, through: 'teamsPlayersPlayers', }); db.teams.belongsToMany(db.players, { as: 'players_filter', foreignKey: { name: 'teams_playersId', }, constraints: false, through: 'teamsPlayersPlayers', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.teams.belongsTo(db.players, { as: 'captain', foreignKey: { name: 'captainId', }, constraints: false, }); db.teams.belongsTo(db.players, { as: 'vice_captain', foreignKey: { name: 'vice_captainId', }, constraints: false, }); db.teams.belongsTo(db.users, { as: 'createdBy', }); db.teams.belongsTo(db.users, { as: 'updatedBy', }); }; return teams; };