510 lines
12 KiB
JavaScript
510 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 Proxy_rulesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proxy_rules = await db.proxy_rules.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
rule_name: data.rule_name
|
|
||
|
|
null
|
|
,
|
|
|
|
protocol: data.protocol
|
|
||
|
|
null
|
|
,
|
|
|
|
listen_path: data.listen_path
|
|
||
|
|
null
|
|
,
|
|
|
|
target_base_url: data.target_base_url
|
|
||
|
|
null
|
|
,
|
|
|
|
forward_auth_header: data.forward_auth_header
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
strip_path_prefix: data.strip_path_prefix
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_enabled: data.is_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await proxy_rules.setEndpoint( data.endpoint || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return proxy_rules;
|
|
}
|
|
|
|
|
|
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 proxy_rulesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
rule_name: item.rule_name
|
|
||
|
|
null
|
|
,
|
|
|
|
protocol: item.protocol
|
|
||
|
|
null
|
|
,
|
|
|
|
listen_path: item.listen_path
|
|
||
|
|
null
|
|
,
|
|
|
|
target_base_url: item.target_base_url
|
|
||
|
|
null
|
|
,
|
|
|
|
forward_auth_header: item.forward_auth_header
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
strip_path_prefix: item.strip_path_prefix
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_enabled: item.is_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const proxy_rules = await db.proxy_rules.bulkCreate(proxy_rulesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return proxy_rules;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const proxy_rules = await db.proxy_rules.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.rule_name !== undefined) updatePayload.rule_name = data.rule_name;
|
|
|
|
|
|
if (data.protocol !== undefined) updatePayload.protocol = data.protocol;
|
|
|
|
|
|
if (data.listen_path !== undefined) updatePayload.listen_path = data.listen_path;
|
|
|
|
|
|
if (data.target_base_url !== undefined) updatePayload.target_base_url = data.target_base_url;
|
|
|
|
|
|
if (data.forward_auth_header !== undefined) updatePayload.forward_auth_header = data.forward_auth_header;
|
|
|
|
|
|
if (data.strip_path_prefix !== undefined) updatePayload.strip_path_prefix = data.strip_path_prefix;
|
|
|
|
|
|
if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await proxy_rules.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.endpoint !== undefined) {
|
|
await proxy_rules.setEndpoint(
|
|
|
|
data.endpoint,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return proxy_rules;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proxy_rules = await db.proxy_rules.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of proxy_rules) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of proxy_rules) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return proxy_rules;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proxy_rules = await db.proxy_rules.findByPk(id, options);
|
|
|
|
await proxy_rules.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await proxy_rules.destroy({
|
|
transaction
|
|
});
|
|
|
|
return proxy_rules;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const proxy_rules = await db.proxy_rules.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!proxy_rules) {
|
|
return proxy_rules;
|
|
}
|
|
|
|
const output = proxy_rules.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.endpoint = await proxy_rules.getEndpoint({
|
|
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.llm_endpoints,
|
|
as: 'endpoint',
|
|
|
|
where: filter.endpoint ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.endpoint.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
endpoint_name: {
|
|
[Op.or]: filter.endpoint.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.rule_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'proxy_rules',
|
|
'rule_name',
|
|
filter.rule_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.listen_path) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'proxy_rules',
|
|
'listen_path',
|
|
filter.listen_path,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.target_base_url) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'proxy_rules',
|
|
'target_base_url',
|
|
filter.target_base_url,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.protocol) {
|
|
where = {
|
|
...where,
|
|
protocol: filter.protocol,
|
|
};
|
|
}
|
|
|
|
if (filter.forward_auth_header) {
|
|
where = {
|
|
...where,
|
|
forward_auth_header: filter.forward_auth_header,
|
|
};
|
|
}
|
|
|
|
if (filter.strip_path_prefix) {
|
|
where = {
|
|
...where,
|
|
strip_path_prefix: filter.strip_path_prefix,
|
|
};
|
|
}
|
|
|
|
if (filter.is_enabled) {
|
|
where = {
|
|
...where,
|
|
is_enabled: filter.is_enabled,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.proxy_rules.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(
|
|
'proxy_rules',
|
|
'rule_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.proxy_rules.findAll({
|
|
attributes: [ 'id', 'rule_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['rule_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.rule_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|