2026-02-20 07:20:01 +00:00

236 lines
2.7 KiB
JavaScript

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,
},
duration_seconds: {
type: DataTypes.INTEGER,
},
genre: {
type: DataTypes.ENUM,
values: [
"pop",
"rock",
"hip_hop",
"rap",
"electronic",
"dance",
"rnb",
"jazz",
"classical",
"metal",
"reggae",
"latin",
"folk",
"country",
"blues",
"other"
],
},
release_at: {
type: DataTypes.DATE,
},
explicit: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
lyrics: {
type: DataTypes.TEXT,
},
language: {
type: DataTypes.TEXT,
},
popularity_score: {
type: DataTypes.DECIMAL,
},
external_url: {
type: DataTypes.TEXT,
},
isrc: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
songs.associate = (db) => {
/// 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.favorites, {
as: 'favorites_song',
foreignKey: {
name: 'songId',
},
constraints: false,
});
db.songs.hasMany(db.play_history, {
as: 'play_history_song',
foreignKey: {
name: 'songId',
},
constraints: false,
});
//end loop
db.songs.belongsTo(db.artists, {
as: 'artist',
foreignKey: {
name: 'artistId',
},
constraints: false,
});
db.songs.belongsTo(db.albums, {
as: 'album',
foreignKey: {
name: 'albumId',
},
constraints: false,
});
db.songs.hasMany(db.file, {
as: 'audio_files',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.songs.getTableName(),
belongsToColumn: 'audio_files',
},
});
db.songs.belongsTo(db.users, {
as: 'createdBy',
});
db.songs.belongsTo(db.users, {
as: 'updatedBy',
});
};
return songs;
};