31559/backend/src/db/api/emulator_settings.js
2025-05-15 16:29:00 +00:00

344 lines
8.2 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 Emulator_settingsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const emulator_settings = await db.emulator_settings.create(
{
id: data.id || undefined,
fps_cap: data.fps_cap || null,
audio_type: data.audio_type || null,
hd_shaders: data.hd_shaders || false,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await emulator_settings.setUser(data.user || null, {
transaction,
});
return emulator_settings;
}
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 emulator_settingsData = data.map((item, index) => ({
id: item.id || undefined,
fps_cap: item.fps_cap || null,
audio_type: item.audio_type || null,
hd_shaders: item.hd_shaders || false,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const emulator_settings = await db.emulator_settings.bulkCreate(
emulator_settingsData,
{ transaction },
);
// For each item created, replace relation files
return emulator_settings;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const emulator_settings = await db.emulator_settings.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.fps_cap !== undefined) updatePayload.fps_cap = data.fps_cap;
if (data.audio_type !== undefined)
updatePayload.audio_type = data.audio_type;
if (data.hd_shaders !== undefined)
updatePayload.hd_shaders = data.hd_shaders;
updatePayload.updatedById = currentUser.id;
await emulator_settings.update(updatePayload, { transaction });
if (data.user !== undefined) {
await emulator_settings.setUser(
data.user,
{ transaction },
);
}
return emulator_settings;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const emulator_settings = await db.emulator_settings.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of emulator_settings) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of emulator_settings) {
await record.destroy({ transaction });
}
});
return emulator_settings;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const emulator_settings = await db.emulator_settings.findByPk(id, options);
await emulator_settings.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await emulator_settings.destroy({
transaction,
});
return emulator_settings;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const emulator_settings = await db.emulator_settings.findOne(
{ where },
{ transaction },
);
if (!emulator_settings) {
return emulator_settings;
}
const output = emulator_settings.get({ plain: true });
output.user = await emulator_settings.getUser({
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.users,
as: 'user',
where: filter.user
? {
[Op.or]: [
{
id: {
[Op.in]: filter.user
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
firstName: {
[Op.or]: filter.user
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.fps_capRange) {
const [start, end] = filter.fps_capRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
fps_cap: {
...where.fps_cap,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
fps_cap: {
...where.fps_cap,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.audio_type) {
where = {
...where,
audio_type: filter.audio_type,
};
}
if (filter.hd_shaders) {
where = {
...where,
hd_shaders: filter.hd_shaders,
};
}
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.emulator_settings.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('emulator_settings', 'audio_type', query),
],
};
}
const records = await db.emulator_settings.findAll({
attributes: ['id', 'audio_type'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['audio_type', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.audio_type,
}));
}
};