kysely-mapper
Version:
Flexible Kysely-based utility for mapping between tables and objects
54 lines • 2.14 kB
JavaScript
/**
* Returns a query builder that constrains the provided query builder
* according to the provided query filter or binary operation.
* @param base The Kysely mapper that is used to create references.
* @param qb The query builder to constrain.
* @param filterOrLHS The query filter or left-hand side of a binary operation.
* @param op The operator of a binary operation.
* @param rhs The right-hand side of a binary operation.
* @returns A query builder constrained for the provided query filter
* or binary operation.
*/
export function applyQueryFilter(db, qb, keyColumns, filterOrLHS, op, rhs) {
// Process a binary operation.
if (op !== undefined) {
return qb.where(filterOrLHS, op, rhs);
}
const filter = filterOrLHS;
if (typeof filter === 'object') {
// Process a key tuple filter.
if (Array.isArray(filter)) {
keyColumns.forEach((column, i) => {
qb = qb.where(db.dynamic.ref(column), '=', filter[i]);
});
return qb;
}
// Process a query expression filter. Check for expressions
// first because they could potentially be plain objects.
if ('expressionType' in filter) {
return qb.where(filter);
}
// Process a field matching filter. `{}` matches all rows.
if (filter.constructor === Object) {
for (const [column, value] of Object.entries(filter)) {
if (Array.isArray(value)) {
qb = qb.where(db.dynamic.ref(column), 'in', value);
}
else {
qb = qb.where(db.dynamic.ref(column), '=', value);
}
}
return qb;
}
}
// Process a where expression factory.
if (typeof filter === 'function') {
return qb.where(filter);
}
// Process a single key filter, expressed as a primitive value.
if (keyColumns.length === 1) {
return qb.where(db.dynamic.ref(keyColumns[0]), '=', filter);
}
throw Error('Unrecognized query filter');
}
//# sourceMappingURL=apply-query-filter.js.map