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.
22 lines (21 loc) • 654 B
JavaScript
import { buildWhere } from "../sql/common.js";
import { fieldToStr, toSql } from "../sql/sqlite.js";
//#region src/db0/sqlite.ts
const toDb0Where = (where) => {
if (!where) return;
return buildWhere(where, fieldToStr, () => "?");
};
const toDb0OrderBy = (orderBy) => {
if (!orderBy) return;
return {
sql: (Array.isArray(orderBy) ? orderBy : [orderBy]).map((c) => `${fieldToStr(c.field)} ${c.direction.toUpperCase()}`).join(", "),
params: []
};
};
const toDb0 = (db, json) => {
const result = toSql(json);
if (!result) return [];
return db.prepare(result.sql).all(...result.params);
};
//#endregion
export { toDb0, toDb0OrderBy, toDb0Where };