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.
66 lines (65 loc) • 1.46 kB
JavaScript
import { buildInputWhere, createExpr, mergeWhere } from "./where.js";
//#region src/core/index.ts
const aq = (state) => {
const where = (col, op, value) => {
const inputWhere = buildInputWhere(col, op, value);
const newWhere = mergeWhere(state?.where, inputWhere);
return aq({
...state,
where: newWhere
});
};
return {
toJSON: () => state || {},
where: (col, op, value) => {
if (col === null || col === void 0) return aq(state);
if (typeof col === "function") {
const cbWhere = col(createExpr())._q;
const changedWhere = state?.where ? {
op: "and",
conditions: [state.where, cbWhere]
} : cbWhere;
return aq({
...state,
where: changedWhere
});
}
if (col && typeof col === "object" && "op" in col) {
const whereObj = col;
const changedWhere = state?.where ? {
op: "and",
conditions: [state.where, whereObj]
} : whereObj;
return aq({
...state,
where: changedWhere
});
}
return where(col, op, value);
},
orderBy: (col, direction = "asc") => {
const field = Array.isArray(col) ? col : [col];
const newOrderBy = state?.orderBy ? [...state.orderBy, {
field,
direction
}] : [{
field,
direction
}];
return aq({
...state,
orderBy: newOrderBy
});
},
limit: (value) => aq({
...state,
limit: value
}),
offset: (value) => aq({
...state,
offset: value
})
};
};
//#endregion
export { aq };