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 profiles = sequelize.define( 'profiles', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, first_name: { type: DataTypes.TEXT, }, last_name: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); profiles.associate = (db) => { db.profiles.belongsToMany(db.games, { as: 'games', foreignKey: { name: 'profiles_gamesId', }, constraints: false, through: 'profilesGamesGames', }); db.profiles.belongsToMany(db.games, { as: 'games_filter', foreignKey: { name: 'profiles_gamesId', }, constraints: false, through: 'profilesGamesGames', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.profiles.hasMany(db.games, { as: 'games_initiator', foreignKey: { name: 'initiatorId', }, constraints: false, }); db.profiles.hasMany(db.games, { as: 'games_opponent', foreignKey: { name: 'opponentId', }, constraints: false, }); //end loop db.profiles.belongsTo(db.users, { as: 'user', foreignKey: { name: 'userId', }, constraints: false, }); db.profiles.hasMany(db.file, { as: 'photos', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.profiles.getTableName(), belongsToColumn: 'photos', }, }); db.profiles.belongsTo(db.users, { as: 'createdBy', }); db.profiles.belongsTo(db.users, { as: 'updatedBy', }); }; return profiles; };