39628-vm/backend/src/db/api/env_var_snapshots.js
2026-04-14 00:58:20 +00:00

480 lines
12 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 Env_var_snapshotsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const env_var_snapshots = await db.env_var_snapshots.create(
{
id: data.id || undefined,
snapshot_name: data.snapshot_name
||
null
,
captured_at: data.captured_at
||
null
,
visibility: data.visibility
||
null
,
filter_regex: data.filter_regex
||
null
,
raw_output: data.raw_output
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await env_var_snapshots.setPlatform_ai_config( data.platform_ai_config || null, {
transaction,
});
return env_var_snapshots;
}
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 env_var_snapshotsData = data.map((item, index) => ({
id: item.id || undefined,
snapshot_name: item.snapshot_name
||
null
,
captured_at: item.captured_at
||
null
,
visibility: item.visibility
||
null
,
filter_regex: item.filter_regex
||
null
,
raw_output: item.raw_output
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const env_var_snapshots = await db.env_var_snapshots.bulkCreate(env_var_snapshotsData, { transaction });
// For each item created, replace relation files
return env_var_snapshots;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const env_var_snapshots = await db.env_var_snapshots.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.snapshot_name !== undefined) updatePayload.snapshot_name = data.snapshot_name;
if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
if (data.visibility !== undefined) updatePayload.visibility = data.visibility;
if (data.filter_regex !== undefined) updatePayload.filter_regex = data.filter_regex;
if (data.raw_output !== undefined) updatePayload.raw_output = data.raw_output;
updatePayload.updatedById = currentUser.id;
await env_var_snapshots.update(updatePayload, {transaction});
if (data.platform_ai_config !== undefined) {
await env_var_snapshots.setPlatform_ai_config(
data.platform_ai_config,
{ transaction }
);
}
return env_var_snapshots;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const env_var_snapshots = await db.env_var_snapshots.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of env_var_snapshots) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of env_var_snapshots) {
await record.destroy({transaction});
}
});
return env_var_snapshots;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const env_var_snapshots = await db.env_var_snapshots.findByPk(id, options);
await env_var_snapshots.update({
deletedBy: currentUser.id
}, {
transaction,
});
await env_var_snapshots.destroy({
transaction
});
return env_var_snapshots;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const env_var_snapshots = await db.env_var_snapshots.findOne(
{ where },
{ transaction },
);
if (!env_var_snapshots) {
return env_var_snapshots;
}
const output = env_var_snapshots.get({plain: true});
output.platform_ai_config = await env_var_snapshots.getPlatform_ai_config({
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.platform_ai_configs,
as: 'platform_ai_config',
where: filter.platform_ai_config ? {
[Op.or]: [
{ id: { [Op.in]: filter.platform_ai_config.split('|').map(term => Utils.uuid(term)) } },
{
platform_name: {
[Op.or]: filter.platform_ai_config.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.snapshot_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'env_var_snapshots',
'snapshot_name',
filter.snapshot_name,
),
};
}
if (filter.filter_regex) {
where = {
...where,
[Op.and]: Utils.ilike(
'env_var_snapshots',
'filter_regex',
filter.filter_regex,
),
};
}
if (filter.raw_output) {
where = {
...where,
[Op.and]: Utils.ilike(
'env_var_snapshots',
'raw_output',
filter.raw_output,
),
};
}
if (filter.captured_atRange) {
const [start, end] = filter.captured_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.visibility) {
where = {
...where,
visibility: filter.visibility,
};
}
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.env_var_snapshots.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(
'env_var_snapshots',
'snapshot_name',
query,
),
],
};
}
const records = await db.env_var_snapshots.findAll({
attributes: [ 'id', 'snapshot_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['snapshot_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.snapshot_name,
}));
}
};