DBQC for TypeScript
DBQC schema discovery generates typed table values. Import those values and write queries in TypeScript; DBQC keeps the relational dependency graph, orders CTEs, checks types, and compiles the same query to every supported SQL dialect.
import { orders, restaurants } from "./dbqc-schema.ts";
import {
deleteFrom, desc, insert, params, recursive, rowNumber, sum, update,
} from "./dbqc-dsl.ts";
The ordinary case
const ids = restaurants
.where({ city: "New York" })
.pluck("id");
For several columns, use the generated fields:
const directory = restaurants
.where({ city: "New York", status: "active" })
.select(restaurants.id, restaurants.name);
The object form is typed: misspelled columns and values of the wrong TypeScript type fail locally. Equality is DBQC equality (IS NOT DISTINCT FROM), not JavaScript equality. The compiler may emit = when nullability proves it equivalent.
Predicates and joins
TypeScript cannot overload ===, <, or >. Generated fields therefore expose typed SQL operations, and ordinary values are lifted automatically:
const highValue = restaurants
.join(orders, restaurants.id.eq(orders.restaurantId))
.where(orders.total.gt("100.00"))
.select(restaurants.name, orders.total)
.orderBy(desc(orders.total));
No column name or SQL type is repeated. .eq() is null-safe equality; .neq(), .lt(), .lte(), .gt(), and .gte() have the corresponding DBQC semantics. Boolean predicates compose with .and(), .or(), and .not().
const openInNewYork = restaurants.where(
restaurants.city.eq("New York").and(restaurants.status.neq("closed")),
);
Use .leftJoin(), .rightJoin(), or .lateralJoin() when those SQL join semantics are intended.
Runtime parameters
Parameters are a relational source, so dependency ordering remains automatic:
const input = params<{ restaurantId: string }>({ restaurantId: "uuid" });
const byId = restaurants
.join(input, restaurants.id.eq(input.field("restaurantId")))
.select(restaurants.id, restaurants.name);
const compiled = await byId.compile("psql");
console.log(compiled.sql);
console.log(compiled.paramOrder); // ["restaurantId"]
Bind runtime values in paramOrder; DBQC never interpolates them into SQL.
Aggregates and HAVING
const revenue = orders
.groupBy(orders.restaurantId)
.having(sum(orders.total).gt("0.00"))
.select(
orders.restaurantId,
sum(orders.total).as("revenue"),
);
Only computed output needs a name. Grouping columns retain their generated names.
Reusable queries and CTE ordering
Name a relational component once and reuse it:
const revenue = orders
.groupBy(orders.restaurantId)
.select(
orders.restaurantId,
sum(orders.total).as("revenue"),
)
.named("revenue");
const ranked = restaurants
.join(revenue, restaurants.id.eq(revenue.field("restaurantId")))
.where(revenue.field("revenue").gt("1000.00"))
.select(
restaurants.name,
revenue.field("revenue"),
);
DBQC derives the dependency DAG and emits CTEs in valid order. Callers compose queries; they do not manually maintain a WITH list.
Windows, JSON, and pagination
const feed = orders
.select(
orders.id,
orders.restaurantId,
rowNumber()
.over([orders.restaurantId], [desc(orders.createdAt)])
.as("number"),
)
.page(25, 0, desc(orders.createdAt));
const contacts = restaurants.select(
restaurants.id,
restaurants.metadata.jsonPathText("$.contact.email").as("email"),
);
Window and JSON expressions remain typed relational expressions and therefore participate in the same dependency analysis.
Writes
Discovered types also lift ordinary values in DML:
const created = insert(restaurants, [{
id: crypto.randomUUID(),
name: "Saffron",
city: "New York",
cuisine: "Indian",
status: "active",
}], ["id"]);
const moved = update(
restaurants,
{ city: "Brooklyn" },
restaurants.id.eq("8ddf6d4d-0be1-44df-b26c-bf33c612eb79"),
["id"],
);
const removed = deleteFrom(
restaurants,
restaurants.status.eq("closed"),
["id"],
);
insertOrUpdate(), insertFrom(), updateFrom(), conflict policies, locks, transactions, procedures, and returning columns use the same typed facade.
Recursive queries
Recursive references are dynamic, so their output heading is declared once:
const tree = recursive(
"tree",
{ id: "uuid", parentId: "uuid" },
roots.select(roots.id, roots.parentId),
(tree) => nodes
.join(tree, nodes.parentId.eq(tree.field("id")))
.select(nodes.id, nodes.parentId),
);
Use recursiveDistinct() when recursive UNION semantics are required for deduplication or cycle termination.
Compile and execute
const compiled = await ranked.compile("psql");
const result = await db.query(compiled.sql, compiled.paramOrder.map(readInput));
DBQC compiles and describes the result; your normal database driver executes it. Set DBQC_ENDPOINT and DBQC_KEY, or pass endpoint credentials to the low-level compiler client.
Escape hatch
raw() and rawSource() exist for vendor features DBQC does not model. They are explicit escape hatches: prefer generated fields and the typed facade for normal queries so DBQC can retain type checks, dependency ordering, and portability.