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 readings = sequelize.define( 'readings', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, date: { type: DataTypes.DATE, }, book: { type: DataTypes.TEXT, }, chapter: { type: DataTypes.INTEGER, }, verses: { type: DataTypes.TEXT, }, translation: { type: DataTypes.TEXT, }, notes: { type: DataTypes.TEXT, }, tags: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); readings.associate = (db) => { db.readings.belongsToMany(db.verses, { as: 'versesref', foreignKey: { name: 'readings_versesrefId', }, constraints: false, through: 'readingsVersesrefVerses', }); db.readings.belongsToMany(db.verses, { as: 'versesref_filter', foreignKey: { name: 'readings_versesrefId', }, constraints: false, through: 'readingsVersesrefVerses', }); db.readings.belongsToMany(db.verses, { as: 'verses_select', foreignKey: { name: 'readings_verses_selectId', }, constraints: false, through: 'readingsVerses_selectVerses', }); db.readings.belongsToMany(db.verses, { as: 'verses_select_filter', foreignKey: { name: 'readings_verses_selectId', }, constraints: false, through: 'readingsVerses_selectVerses', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.readings.hasMany(db.notes, { as: 'notes_reading', foreignKey: { name: 'readingId', }, constraints: false, }); //end loop db.readings.belongsTo(db.users, { as: 'user', foreignKey: { name: 'userId', }, constraints: false, }); db.readings.belongsTo(db.books, { as: 'bookref', foreignKey: { name: 'bookrefId', }, constraints: false, }); db.readings.belongsTo(db.chapters, { as: 'chapterref', foreignKey: { name: 'chapterrefId', }, constraints: false, }); db.readings.belongsTo(db.translations, { as: 'translationref', foreignKey: { name: 'translationrefId', }, constraints: false, }); db.readings.belongsTo(db.books, { as: 'book_select', foreignKey: { name: 'book_selectId', }, constraints: false, }); db.readings.belongsTo(db.chapters, { as: 'chapter_select', foreignKey: { name: 'chapter_selectId', }, constraints: false, }); db.readings.belongsTo(db.translations, { as: 'translation_select', foreignKey: { name: 'translation_selectId', }, constraints: false, }); db.readings.belongsTo(db.users, { as: 'createdBy', }); db.readings.belongsTo(db.users, { as: 'updatedBy', }); }; return readings; };