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 song_searches = sequelize.define( 'song_searches', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, query: { type: DataTypes.TEXT, }, search_mode: { type: DataTypes.ENUM, values: [ "lyrics", "artist" ], }, searched_at: { type: DataTypes.DATE, }, result_count: { type: DataTypes.INTEGER, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); song_searches.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.song_searches.hasMany(db.song_results, { as: 'song_results_search', foreignKey: { name: 'searchId', }, constraints: false, }); //end loop db.song_searches.belongsTo(db.users, { as: 'user', foreignKey: { name: 'userId', }, constraints: false, }); db.song_searches.belongsTo(db.users, { as: 'createdBy', }); db.song_searches.belongsTo(db.users, { as: 'updatedBy', }); }; return song_searches; };