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 students = sequelize.define( 'students', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, student_name: { type: DataTypes.TEXT, }, student_identifier: { type: DataTypes.TEXT, }, is_active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); students.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.students.hasMany(db.session_participants, { as: 'session_participants_student', foreignKey: { name: 'studentId', }, constraints: false, }); db.students.hasMany(db.exam_attempts, { as: 'exam_attempts_student', foreignKey: { name: 'studentId', }, constraints: false, }); db.students.hasMany(db.report_cards, { as: 'report_cards_student', foreignKey: { name: 'studentId', }, constraints: false, }); //end loop db.students.belongsTo(db.schools, { as: 'school', foreignKey: { name: 'schoolId', }, constraints: false, }); db.students.belongsTo(db.users, { as: 'parent_user', foreignKey: { name: 'parent_userId', }, constraints: false, }); db.students.belongsTo(db.users, { as: 'createdBy', }); db.students.belongsTo(db.users, { as: 'updatedBy', }); }; return students; };