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 songs = sequelize.define( 'songs', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, genre: { type: DataTypes.ENUM, values: ['pop', 'rock', 'jazz', 'classical', 'hiphop'], }, release_date: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); songs.associate = (db) => { db.songs.belongsToMany(db.users, { as: 'collaborators', foreignKey: { name: 'songs_collaboratorsId', }, constraints: false, through: 'songsCollaboratorsUsers', }); db.songs.belongsToMany(db.users, { as: 'collaborators_filter', foreignKey: { name: 'songs_collaboratorsId', }, constraints: false, through: 'songsCollaboratorsUsers', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.songs.hasMany(db.comments, { as: 'comments_song', foreignKey: { name: 'songId', }, constraints: false, }); db.songs.hasMany(db.duets, { as: 'duets_original_song', foreignKey: { name: 'original_songId', }, constraints: false, }); db.songs.hasMany(db.likes, { as: 'likes_song', foreignKey: { name: 'songId', }, constraints: false, }); db.songs.hasMany(db.reports, { as: 'reports_reported_content', foreignKey: { name: 'reported_contentId', }, constraints: false, }); //end loop db.songs.belongsTo(db.users, { as: 'artist', foreignKey: { name: 'artistId', }, constraints: false, }); db.songs.hasMany(db.file, { as: 'audio_file', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.songs.getTableName(), belongsToColumn: 'audio_file', }, }); db.songs.belongsTo(db.users, { as: 'createdBy', }); db.songs.belongsTo(db.users, { as: 'updatedBy', }); }; return songs; };