Initial commit - Church Music Database
This commit is contained in:
46
new-site/backend/utils/queryBuilder.js
Normal file
46
new-site/backend/utils/queryBuilder.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* SQL Query Builder Utilities
|
||||
*/
|
||||
|
||||
/**
|
||||
* Build dynamic WHERE clause from conditions
|
||||
* @param {Array} conditions - Array of SQL conditions
|
||||
* @returns {string} WHERE clause or empty string
|
||||
*/
|
||||
const buildWhereClause = (conditions) => {
|
||||
return conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
|
||||
};
|
||||
|
||||
/**
|
||||
* Build LIMIT/OFFSET clause
|
||||
* @param {number} limit - Max results
|
||||
* @param {number} offset - Results to skip
|
||||
* @param {number} paramOffset - Current parameter index
|
||||
* @returns {Object} { clause, params }
|
||||
*/
|
||||
const buildPagination = (limit, offset, paramOffset = 1) => {
|
||||
return {
|
||||
clause: ` LIMIT $${paramOffset} OFFSET $${paramOffset + 1}`,
|
||||
params: [parseInt(limit) || 100, parseInt(offset) || 0],
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Build search condition for multiple fields
|
||||
* @param {string} searchTerm - Search term
|
||||
* @param {Array} fields - Fields to search in
|
||||
* @param {number} paramIndex - Parameter index
|
||||
* @returns {string} SQL condition
|
||||
*/
|
||||
const buildSearchCondition = (searchTerm, fields, paramIndex) => {
|
||||
const conditions = fields.map(
|
||||
(field) => `LOWER(${field}) LIKE $${paramIndex}`,
|
||||
);
|
||||
return `(${conditions.join(" OR ")})`;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
buildWhereClause,
|
||||
buildPagination,
|
||||
buildSearchCondition,
|
||||
};
|
||||
Reference in New Issue
Block a user