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 union = sequelize.define( 'union', { 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, }, ); union.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.union.hasMany(db.users, { as: 'users_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.analytics, { as: 'analytics_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.courses, { as: 'courses_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.discussion_boards, { as: 'discussion_boards_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.enrollments, { as: 'enrollments_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.instructors, { as: 'instructors_union', foreignKey: { name: 'unionId', }, constraints: false, }); db.union.hasMany(db.students, { as: 'students_union', foreignKey: { name: 'unionId', }, constraints: false, }); //end loop db.union.belongsTo(db.users, { as: 'createdBy', }); db.union.belongsTo(db.users, { as: 'updatedBy', }); }; return union; };