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 topics = sequelize.define( 'topics', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); topics.associate = (db) => { db.topics.belongsToMany(db.questions, { as: 'questions', foreignKey: { name: 'topics_questionsId', }, constraints: false, through: 'topicsQuestionsQuestions', }); db.topics.belongsToMany(db.questions, { as: 'questions_filter', foreignKey: { name: 'topics_questionsId', }, constraints: false, through: 'topicsQuestionsQuestions', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.topics.hasMany(db.questions, { as: 'questions_topic', foreignKey: { name: 'topicId', }, constraints: false, }); //end loop db.topics.belongsTo(db.categories, { as: 'category', foreignKey: { name: 'categoryId', }, constraints: false, }); db.topics.belongsTo(db.users, { as: 'createdBy', }); db.topics.belongsTo(db.users, { as: 'updatedBy', }); }; return topics; };