agnostic-query
Version:
Type-safe fluent builder for portable query schemas. Runtime-agnostic, database-agnostic — the same QuerySchema drives Drizzle, Kysely, db0, or raw SQL.
79 lines (78 loc) • 1.51 kB
JavaScript
import { newWhere } from "./core/where.js";
import { parseOrderByExpression, parseWhereExpression } from "@tanstack/db";
//#region src/tanstack-db.ts
const fromTanDbWhere = (where) => where ? parseWhereExpression(where, { handlers: {
eq: (field, value) => ({
field,
op: "=",
value
}),
lt: (field, value) => ({
field,
op: "<",
value
}),
lte: (field, value) => ({
field,
op: "<=",
value
}),
gt: (field, value) => ({
field,
op: ">",
value
}),
gte: (field, value) => ({
field,
op: ">=",
value
}),
like: (field, value) => ({
field,
op: "like",
value
}),
ilike: (field, value) => ({
field,
op: "ilike",
value
}),
in: (field, values) => ({
field,
op: "in",
values
}),
isNull: (field) => ({
field,
op: "is null"
}),
isUndefined: (field) => ({
field,
op: "is null"
}),
and: (...conditions) => ({
op: "and",
conditions
}),
or: (...conditions) => ({
op: "or",
conditions
}),
not: (condition) => ({
op: "not",
condition
})
} }) : void 0;
const fromTanDbOrderBy = (orderBy) => {
if (!orderBy || orderBy.length === 0) return void 0;
return parseOrderByExpression(orderBy);
};
const fromTanDb = (loadSubsetOptions) => {
return {
limit: loadSubsetOptions?.limit,
where: newWhere(fromTanDbWhere(loadSubsetOptions?.where)).where(fromTanDbWhere(loadSubsetOptions?.cursor?.whereFrom)).toJSON(),
orderBy: fromTanDbOrderBy(loadSubsetOptions?.orderBy)
};
};
//#endregion
export { fromTanDb, fromTanDbOrderBy, fromTanDbWhere };