Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 50 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class PagiHelp {

columnNameConverter = (x) => x;

toCamelCase = (str) => {
return str.replace(/_([a-zA-Z0-9])/g, (_, char) => {
return /[a-zA-Z]/.test(char) ? char.toUpperCase() : char;
});
};

columNames = (arr) =>
arr.map((a) => {
if (a.prefix) {
Expand Down Expand Up @@ -82,7 +88,7 @@ class PagiHelp {
returnString +=
this.tupleCreator(schemaObject, replacements, asItIs) + " AND ";
} else {
let subString = "( ";
let subString = "(";
for (let subObject of schemaObject) {
subString += this.genSchema(subObject, replacements, asItIs) + " OR ";
}
Expand All @@ -109,12 +115,6 @@ class PagiHelp {
let filterConditions = [];

if (filters && filters.length > 0) {
// Function to convert snake_case to camelCase
const toCamelCase = (str) => {
return str.replace(/_([a-zA-Z0-9])/g, (_, char) => {
return /[a-zA-Z]/.test(char) ? char.toUpperCase() : char;
});
};

const processCondition = (condition) => {
if (Array.isArray(condition[0])) {
Expand All @@ -126,8 +126,8 @@ class PagiHelp {
// Find the column matching the alias
let column = columnList.find(col => col.alias === field);
if (!column) {
const camelCaseField = toCamelCase(field);
column = columnList.find(col => toCamelCase(col.alias) === camelCaseField);
const camelCaseField = this.toCamelCase(field);
column = columnList.find(col => this.toCamelCase(col.alias) === camelCaseField);
}

// if field matches "prefix.name"
Expand Down Expand Up @@ -184,35 +184,32 @@ class PagiHelp {
"`" +
joinQuery;

let totalCountQuery =
let totalCountQuery =
"SELECT COUNT(*) AS countValue " +
" FROM `" +
tableName +
"`" +
joinQuery;

let replacements = [];
let whereQuery = " WHERE ";
let whereQuery = "";

if (additionalWhereConditions.length > 0) {
whereQuery += this.genSchema(additionalWhereConditions, replacements, true) + " AND ";
whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + this.genSchema(additionalWhereConditions, replacements, true);
}
if (filterConditions.length > 0) {
whereQuery += this.genSchema(filterConditions, replacements, false) + " AND ";
whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + this.genSchema(filterConditions, replacements, false);
}

if(searchColumnList && searchColumnList.length>0 && paginationObject.search !== "") {
whereQuery = whereQuery + "( ";
if(searchColumnList && searchColumnList.length > 0 && paginationObject.search !== "") {
whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + "(";
for (let column of searchColumnList) {
whereQuery = whereQuery + column + " LIKE ? OR ";
replacements.push(`%${paginationObject.search}%`);
whereQuery += column + " LIKE ? OR ";
replacements.push(`%${paginationObject.search}%`);
}
whereQuery = rtrim(whereQuery, "OR ");
whereQuery = whereQuery + " )";
} else {
whereQuery = rtrim(whereQuery, "AND ");
whereQuery = whereQuery.replace(/\s*(AND|OR)\s*$/i, "");
whereQuery += ")";
}


query = query + whereQuery;
countQuery = countQuery + whereQuery;
Expand Down Expand Up @@ -305,25 +302,44 @@ class PagiHelp {
}

let sort = paginationObject.sort;
if (sort && Object.keys(sort).length !== 0) {
for(let i = 0; i < sort.sorts.length; i++) {
if(!allowedSorts.includes(sort.sorts[i].toUpperCase()))
throw "INVALID SORT VALUE";
sort.sorts[i] = sort.sorts[i].toUpperCase()
let hasSortQuery = options.some(opt => opt.sortQuery);
if (!hasSortQuery && sort && Object.keys(sort).length !== 0) {
for (let i = 0; i < sort.sorts.length; i++) {
if (!allowedSorts.includes(sort.sorts[i].toUpperCase()))
throw "INVALID SORT VALUE";
sort.sorts[i] = sort.sorts[i].toUpperCase();
}
for (let i = 0; i < sort.attributes.length; i++) {
orderByQuery =
orderByQuery +
"" +
this.columnNameConverter(SqlString.escapeId(sort.attributes[i])) +
"" +
sort.sorts[i] +
",";
let attr = sort.attributes[i];
let aliasMatch = null;
for (let option of options) {
let found = option.columnList.find(col => col.alias === attr);
if (!found) {
const camelCaseAttr = this.toCamelCase(attr);
found = option.columnList.find(col => this.toCamelCase(col.alias) === camelCaseAttr);
}
if (found) {
aliasMatch = found.alias;
break;
}
}
if (aliasMatch) {
orderByQuery += `${aliasMatch} ${sort.sorts[i]},`;
} else {
orderByQuery += this.columnNameConverter(SqlString.escapeId(attr)) + " " + sort.sorts[i] + ",";
}
}
orderByQuery = rtrim(orderByQuery, ",");
query = query + orderByQuery;
}

for (let option of options) {
if (option.sortQuery) {
query = query + " " + option.sortQuery.trim();
break;
}
}

if (paginationObject.pageNo && paginationObject.itemsPerPage) {
let offset =
(paginationObject.pageNo - 1) * paginationObject.itemsPerPage;
Expand Down