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 job_postings = sequelize.define( 'job_postings', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, start_date: { type: DataTypes.DATE, }, end_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); job_postings.associate = (db) => { db.job_postings.belongsToMany(db.carers, { as: 'interested_carers', foreignKey: { name: 'job_postings_interested_carersId', }, constraints: false, through: 'job_postingsInterested_carersCarers', }); db.job_postings.belongsToMany(db.carers, { as: 'interested_carers_filter', foreignKey: { name: 'job_postings_interested_carersId', }, constraints: false, through: 'job_postingsInterested_carersCarers', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.job_postings.belongsTo(db.care_homes, { as: 'care_home', foreignKey: { name: 'care_homeId', }, constraints: false, }); db.job_postings.belongsTo(db.company, { as: 'company', foreignKey: { name: 'companyId', }, constraints: false, }); db.job_postings.belongsTo(db.users, { as: 'createdBy', }); db.job_postings.belongsTo(db.users, { as: 'updatedBy', }); }; return job_postings; };