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 question_banks = sequelize.define( 'question_banks', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, bank_name: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, is_shared: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); question_banks.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.question_banks.hasMany(db.questions, { as: 'questions_question_bank', foreignKey: { name: 'question_bankId', }, constraints: false, }); //end loop db.question_banks.belongsTo(db.subjects, { as: 'subject', foreignKey: { name: 'subjectId', }, constraints: false, }); db.question_banks.belongsTo(db.users, { as: 'owner_teacher', foreignKey: { name: 'owner_teacherId', }, constraints: false, }); db.question_banks.belongsTo(db.schools, { as: 'school', foreignKey: { name: 'schoolId', }, constraints: false, }); db.question_banks.belongsTo(db.users, { as: 'createdBy', }); db.question_banks.belongsTo(db.users, { as: 'updatedBy', }); }; return question_banks; };