39380-vm/backend/src/db/api/audio_features.js
2026-03-30 02:47:34 +00:00

720 lines
18 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Audio_featuresDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const audio_features = await db.audio_features.create(
{
id: data.id || undefined,
source: data.source
||
null
,
tempo_bpm: data.tempo_bpm
||
null
,
musical_key: data.musical_key
||
null
,
mode: data.mode
||
null
,
energy: data.energy
||
null
,
danceability: data.danceability
||
null
,
instrumentalness: data.instrumentalness
||
null
,
vocalness: data.vocalness
||
null
,
acousticness: data.acousticness
||
null
,
valence: data.valence
||
null
,
loudness_db: data.loudness_db
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await audio_features.setTrack( data.track || null, {
transaction,
});
return audio_features;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const audio_featuresData = data.map((item, index) => ({
id: item.id || undefined,
source: item.source
||
null
,
tempo_bpm: item.tempo_bpm
||
null
,
musical_key: item.musical_key
||
null
,
mode: item.mode
||
null
,
energy: item.energy
||
null
,
danceability: item.danceability
||
null
,
instrumentalness: item.instrumentalness
||
null
,
vocalness: item.vocalness
||
null
,
acousticness: item.acousticness
||
null
,
valence: item.valence
||
null
,
loudness_db: item.loudness_db
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const audio_features = await db.audio_features.bulkCreate(audio_featuresData, { transaction });
// For each item created, replace relation files
return audio_features;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const audio_features = await db.audio_features.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.source !== undefined) updatePayload.source = data.source;
if (data.tempo_bpm !== undefined) updatePayload.tempo_bpm = data.tempo_bpm;
if (data.musical_key !== undefined) updatePayload.musical_key = data.musical_key;
if (data.mode !== undefined) updatePayload.mode = data.mode;
if (data.energy !== undefined) updatePayload.energy = data.energy;
if (data.danceability !== undefined) updatePayload.danceability = data.danceability;
if (data.instrumentalness !== undefined) updatePayload.instrumentalness = data.instrumentalness;
if (data.vocalness !== undefined) updatePayload.vocalness = data.vocalness;
if (data.acousticness !== undefined) updatePayload.acousticness = data.acousticness;
if (data.valence !== undefined) updatePayload.valence = data.valence;
if (data.loudness_db !== undefined) updatePayload.loudness_db = data.loudness_db;
updatePayload.updatedById = currentUser.id;
await audio_features.update(updatePayload, {transaction});
if (data.track !== undefined) {
await audio_features.setTrack(
data.track,
{ transaction }
);
}
return audio_features;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const audio_features = await db.audio_features.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of audio_features) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of audio_features) {
await record.destroy({transaction});
}
});
return audio_features;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const audio_features = await db.audio_features.findByPk(id, options);
await audio_features.update({
deletedBy: currentUser.id
}, {
transaction,
});
await audio_features.destroy({
transaction
});
return audio_features;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const audio_features = await db.audio_features.findOne(
{ where },
{ transaction },
);
if (!audio_features) {
return audio_features;
}
const output = audio_features.get({plain: true});
output.track = await audio_features.getTrack({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.tracks,
as: 'track',
where: filter.track ? {
[Op.or]: [
{ id: { [Op.in]: filter.track.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.track.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.musical_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'audio_features',
'musical_key',
filter.musical_key,
),
};
}
if (filter.tempo_bpmRange) {
const [start, end] = filter.tempo_bpmRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tempo_bpm: {
...where.tempo_bpm,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tempo_bpm: {
...where.tempo_bpm,
[Op.lte]: end,
},
};
}
}
if (filter.energyRange) {
const [start, end] = filter.energyRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
energy: {
...where.energy,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
energy: {
...where.energy,
[Op.lte]: end,
},
};
}
}
if (filter.danceabilityRange) {
const [start, end] = filter.danceabilityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
danceability: {
...where.danceability,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
danceability: {
...where.danceability,
[Op.lte]: end,
},
};
}
}
if (filter.instrumentalnessRange) {
const [start, end] = filter.instrumentalnessRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
instrumentalness: {
...where.instrumentalness,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
instrumentalness: {
...where.instrumentalness,
[Op.lte]: end,
},
};
}
}
if (filter.vocalnessRange) {
const [start, end] = filter.vocalnessRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
vocalness: {
...where.vocalness,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
vocalness: {
...where.vocalness,
[Op.lte]: end,
},
};
}
}
if (filter.acousticnessRange) {
const [start, end] = filter.acousticnessRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
acousticness: {
...where.acousticness,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
acousticness: {
...where.acousticness,
[Op.lte]: end,
},
};
}
}
if (filter.valenceRange) {
const [start, end] = filter.valenceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
valence: {
...where.valence,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
valence: {
...where.valence,
[Op.lte]: end,
},
};
}
}
if (filter.loudness_dbRange) {
const [start, end] = filter.loudness_dbRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
loudness_db: {
...where.loudness_db,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
loudness_db: {
...where.loudness_db,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.source) {
where = {
...where,
source: filter.source,
};
}
if (filter.mode) {
where = {
...where,
mode: filter.mode,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.audio_features.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'audio_features',
'musical_key',
query,
),
],
};
}
const records = await db.audio_features.findAll({
attributes: [ 'id', 'musical_key' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['musical_key', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.musical_key,
}));
}
};