tuain-dbpool-generic
Version:
Database pool interface to perform standard database operations as part of the Tuain Application Development Framework
208 lines (192 loc) • 6.87 kB
JavaScript
function processColumns(knexPool, queryObject) {
const columns = queryObject.queryFields;
const aliases = queryObject.fieldAliases;
const selectColumns = {};
const columnsArgs = {};
for (let index = 0; index < columns.length; index++) {
const column = columns[index];
const alias = aliases[index];
selectColumns[alias] = column;
columnsArgs[alias] = knexPool.raw(column);
}
return [selectColumns, columnsArgs];
}
function processTable(queryObject) {
let tableObj = {};
const tableSource = queryObject.dataSource[0].split(' ');
const table = tableSource[1];
const tableAlias = tableSource[2] || '';
if (tableAlias) {
tableObj[tableAlias] = table;
} else {
tableObj = table;
}
return tableObj;
}
function processFixedJoins(queryObject) {
let joinArgs = '';
if (queryObject.dataSource.length > 1) {
for (let index = 1; index < queryObject.dataSource.length; index++) {
const fixedJoin = queryObject.dataSource[index];
joinArgs += fixedJoin;
joinArgs += ' ';
}
}
return joinArgs;
}
function processOrder(dataConstraints, selectColumns, query) {
if (dataConstraints && dataConstraints.ordering) {
const orderArgs = [];
for (let index = 0; index < dataConstraints.ordering.length; index++) {
const tmp = dataConstraints.ordering[index];
const order = { column: selectColumns[tmp.field], order: tmp.order };
orderArgs.push(order);
}
query.orderBy(orderArgs);
}
}
function completeFilters(queryObject, recordData, dataConstraints) {
const filters = [];
const predefinedFilters = queryObject.defaultFilters;
if (predefinedFilters) {
const predefinedFields = Object.keys(predefinedFilters);
for (let index = 0; index < predefinedFields.length; index++) {
const fieldName = predefinedFields[index];
const fieldValue = predefinedFilters[fieldName];
const aliasIndex = queryObject.fieldAliases.indexOf(fieldName);
if (aliasIndex >= 0) {
setFilter(fieldName, fieldValue, filters);
}
}
}
if (recordData) {
const recordDataFields = Object.keys(recordData);
for (let index = 0; index < recordDataFields.length; index++) {
const fieldName = recordDataFields[index];
const fieldValue = recordData[fieldName];
if (fieldValue !== null) {
const aliasIndex = queryObject.fieldAliases.indexOf(fieldName);
if (aliasIndex >= 0) {
setFilter(fieldName, fieldValue, filters);
} else if (queryObject.dynamicFilters) {
completeDynamicFilters(fieldName, fieldValue, queryObject, filters);
}
}
}
}
if (filters.length > 0) {
dataConstraints = dataConstraints || {};
dataConstraints.filtersByValue = dataConstraints.filtersByValue ? dataConstraints.filtersByValue : [];
dataConstraints.filtersByValue = filters.concat(dataConstraints.filtersByValue);
}
return dataConstraints;
}
function processSimpleFilters(queryObject, dataConstraints, selectColumns, query) {
if (dataConstraints && dataConstraints.simpleFilter) {
query.where((builder) => {
for (let x = 0; x < dataConstraints.simpleFilter.length; x++) {
const filterValue = `%${dataConstraints.simpleFilter[x]}%`;
groupOrSimpleFilter(queryObject, selectColumns, filterValue, builder);
}
});
}
}
function processFiltersByValue(queryObject, dataConstraints, selectColumns, query) {
let joinArgs = '';
for (let x = 0; x < dataConstraints.filtersByValue.length; x++) {
const filter = dataConstraints.filtersByValue[x];
if (selectColumns[filter.field]) {
if (filter.operator.includes('like')) {
query.where(selectColumns[filter.field], filter.operator, `%${filter.value}%`);
} else {
query.where(selectColumns[filter.field], filter.operator, filter.value);
}
}
let dynamicFilter;
if (queryObject.dynamicFilters) {
dynamicFilter = queryObject.dynamicFilters.find(dynFilter => dynFilter.fieldAlias === filter.field);
}
if (dynamicFilter) {
if (dynamicFilter.sourceName) {
const src = queryObject.dynamicSources.find(source => source.sourceName === dynamicFilter.sourceName);
// Join adicional por filtros, si ya se encuentra no se concatena de nuevo
if (src && !joinArgs.includes(src.dataSource)) {
joinArgs += src.dataSource;
joinArgs += ' ';
}
}
if (filter.operator.includes('like')) {
query.where(dynamicFilter.fieldName, filter.operator, `%${filter.value}%`);
} else {
query.where(dynamicFilter.fieldName, filter.operator, filter.value);
}
}
}
return joinArgs;
}
function completeDeleteFilters(recordData, whereFields) {
const filterByValue = [];
if (whereFields && recordData) {
for (let index = 0; index < whereFields.length; index++) {
const restriction = whereFields[index];
const whereAlias = Object.keys(restriction)[0];
if (recordData[whereAlias]) {
filterByValue.push({
field: whereAlias,
operator: '=',
value: recordData[whereAlias],
});
}
}
}
return filterByValue;
}
function processWhereSentence(whereFields, filters, builder) {
for (let index = 0; index < whereFields.length; index++) {
const restriction = whereFields[index];
const whereAlias = Object.keys(restriction)[0];
const whereColumn = Object.values(restriction)[0];
const filterObj = filters?.find(filter => filter.field === whereAlias);
if (!filterObj) {
throw new Error(`Error dataConstraint ${whereAlias} is required`);
}
const whereOperator = filterObj.operator;
const whereValue = filterObj.value;
builder.where(whereColumn, whereOperator, whereValue);
}
}
function completeDynamicFilters(fieldName, fieldValue, queryObject, filters) {
const existOnDynamicFilters = queryObject.dynamicFilters.find(filter => filter.fieldAlias === fieldName);
if (existOnDynamicFilters) {
setFilter(fieldName, fieldValue, filters);
}
}
function setFilter(field, value, filters) {
if (Array.isArray(value)) {
filters.push({ field, operator: 'in', value });
} else {
filters.push({ field, operator: '=', value });
}
}
function groupOrSimpleFilter(queryObject, selectColumns, filterValue, builder) {
let flag = false;
builder.where((orBuilder) => {
for (let y = 0; y < queryObject.simpleSearchFields.length; y++) {
const fieldAlias = queryObject.simpleSearchFields[y];
orBuilder.orWhere(selectColumns[fieldAlias], 'like', filterValue);
flag = true;
}
});
return flag;
}
module.exports = {
processColumns,
processTable,
processFixedJoins,
processOrder,
completeFilters,
processSimpleFilters,
processFiltersByValue,
completeDeleteFilters,
processWhereSentence,
};