@timescaledb/utils
Version:
This package contains utilities like formatting and escaping sql strings plus other helper functions.
34 lines • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWhereClause = buildWhereClause;
const sql_1 = require("./sql");
function buildWhereClause(where, startParamIndex = 1) {
const conditions = [];
const params = [];
let paramIndex = startParamIndex;
for (const [column, condition] of Object.entries(where)) {
const escapedColumn = (0, sql_1.escapeIdentifier)(column);
if (typeof condition === 'object' && !Array.isArray(condition) && !(condition instanceof Date)) {
for (const [operator, value] of Object.entries(condition)) {
if (Array.isArray(value) && (operator === 'IN' || operator === 'NOT IN')) {
const placeholders = value.map(() => `$${paramIndex++}`).join(', ');
conditions.push(`${escapedColumn} ${operator} (${placeholders})`);
params.push(...value);
}
else {
conditions.push(`${escapedColumn} ${operator} $${paramIndex++}`);
params.push(value);
}
}
}
else {
conditions.push(`${escapedColumn} = $${paramIndex++}`);
params.push(condition);
}
}
return {
sql: conditions.join(' AND '),
params,
};
}
//# sourceMappingURL=build-where.js.map