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 search_histories = sequelize.define( 'search_histories', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, search_date: { type: DataTypes.DATE, }, country: { type: DataTypes.ENUM, values: ['US', 'India', 'UK', 'Canada', 'Australia'], }, currency: { type: DataTypes.ENUM, values: ['USD', 'INR', 'GBP', 'EUR'], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); search_histories.associate = (db) => { db.search_histories.belongsToMany(db.keywords, { as: 'keywords', foreignKey: { name: 'search_histories_keywordsId', }, constraints: false, through: 'search_historiesKeywordsKeywords', }); db.search_histories.belongsToMany(db.keywords, { as: 'keywords_filter', foreignKey: { name: 'search_histories_keywordsId', }, constraints: false, through: 'search_historiesKeywordsKeywords', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.search_histories.hasMany(db.keywords, { as: 'keywords_search_history', foreignKey: { name: 'search_historyId', }, constraints: false, }); //end loop db.search_histories.belongsTo(db.users, { as: 'createdBy', }); db.search_histories.belongsTo(db.users, { as: 'updatedBy', }); }; return search_histories; };