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 teachers = sequelize.define( 'teachers', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, resume: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); teachers.associate = (db) => { db.teachers.belongsToMany(db.reviews, { as: 'reviews', foreignKey: { name: 'teachers_reviewsId', }, constraints: false, through: 'teachersReviewsReviews', }); db.teachers.belongsToMany(db.reviews, { as: 'reviews_filter', foreignKey: { name: 'teachers_reviewsId', }, constraints: false, through: 'teachersReviewsReviews', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.teachers.hasMany(db.reviews, { as: 'reviews_teacher', foreignKey: { name: 'teacherId', }, constraints: false, }); //end loop db.teachers.belongsTo(db.users, { as: 'user', foreignKey: { name: 'userId', }, constraints: false, }); db.teachers.hasMany(db.file, { as: 'resume_file', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.teachers.getTableName(), belongsToColumn: 'resume_file', }, }); db.teachers.hasMany(db.file, { as: 'video', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.teachers.getTableName(), belongsToColumn: 'video', }, }); db.teachers.belongsTo(db.users, { as: 'createdBy', }); db.teachers.belongsTo(db.users, { as: 'updatedBy', }); }; return teachers; };