TypeScript implementation of an SQL builder. It provides a fluent API to build SQL queries in a type-safe manner.
Install the package using npm:
npm add @jakub.knejzlik/ts-queryImport the necessary classes and functions from the package:
import { Q } from "@jakub.knejzlik/ts-query";Create an instance of the Query class and use its methods to build your SQL query:
const query = new Query();
Q.select().from("users");
console.log(query.toSQL(Q.flavors.mysql));For more detailed usage instructions and API documentation, visit the Github pages.
Deserialization alone does not authorize a query. Opt in to structural validation, reject raw SQL, restrict functions, inspect the AST for your table/column policy, and then apply trusted transforms before rendering with bindings:
import { Q, Cond, Fn, SelectQuery, walk } from "@jakub.knejzlik/ts-query";
// Client: request a versioned wire envelope.
const input = Q.select()
.field(Fn.count("u.id"), "count")
.from("users", "u")
.where(Cond.equal("u.active", 1))
.serialize({ version: 1 });
// Server: validate before generating SQL.
const query = Q.deserialize(input, {
allowRaw: false,
functions: ["COUNT"],
maxDepth: 0,
});
if (!(query instanceof SelectQuery)) throw new Error("Only SELECT is allowed");
walk(query, {
onTable(name) {
if (name !== "users") throw new Error(`Table denied: ${name}`);
},
onColumn(table, name) {
if (
(table !== undefined && table !== "u") ||
!["id", "active"].includes(name)
) {
throw new Error(`Column denied: ${table ?? ""}.${name}`);
}
},
});
// Supply tenant identity from the authenticated server context.
const tenantId = "tenant-123";
const { sql, bindings } = query.toSQLWithBindings(Q.flavors.sqlite, {
transformTable: (name) =>
Q.select().from(name).where(Cond.equal("tenant_id", tenantId)),
});
// sql: SELECT COUNT(`u`.`id`) AS `count` FROM
// (SELECT * FROM `users` WHERE `tenant_id` = ?) AS `u` WHERE `u`.`active` = ?
// bindings: ["tenant-123", 1]
// With a Cloudflare D1 binding named db:
// const rows = await db.prepare(sql).bind(...bindings).all();toSQLWithBindings(flavor?, options?): SQLWithBindings is available on SELECT,
INSERT, UPDATE, DELETE, expressions, conditions, and CREATE TABLE/VIEW AS SELECT
builders. The standalone toSQLWithBindings(query, flavor?, options?) also accepts
any ISequelizable. SQLWithBindings is { sql: string; bindings: unknown[] }.
The options are the existing ISequelizableOptions transforms. The example accepts
only a root SELECT before adding its trusted derived table. Rendering uses
? for SQLite/MySQL and $1, $2, ... for PostgreSQL; custom flavors can supply
parameterPlaceholder(index: number): string (one-based).
Values in conditions, IN lists, typed function arguments, mutations, and
LIMIT/OFFSET become bindings in SQL occurrence order. Identifiers stay quoted.
Use Q.value(value) for literals; explicit raw SQL and legacy SQL expression
text remain SQL; legacy raw NULL literal markers are bound as null.
Bindings preserve JavaScript values, including Date, boolean,
and bigint; your database adapter must support or convert those types.
DATEADD binds the SQLite modifier or PostgreSQL interval as a single string;
MySQL keeps its validated interval unit as SQL syntax and binds the amount.
Without transforms, existing toSQL() output is unchanged.
Transforms reach every nested SELECT in the supplied AST, including condition
subqueries, function arguments, JOIN ON, HAVING, GROUP BY, ORDER BY, UNION operands,
INSERT SELECT, UPDATE sub-selects, and CREATE TABLE/VIEW AS SELECT. Both inline
and bound rendering use the same transforms. Custom flavor overrides of
escapeFunction(fn, options?) and escapeOperation(fn, options?) must forward
options to their arguments' toSQL(flavor, options) calls. Mutation targets continue to use
transformInsertMutation, transformUpdateMutation, and transformDeleteMutation;
transformTable scopes their SELECT sources. A replacement returned by
transformTable is trusted SQL-builder AST: that table callback is disabled within
the replacement to prevent recursive wrapping. Scope any additional tables your
replacement introduces in the trusted callback itself.
Use Cond.exists(subquery) for policy quantifiers such as “has any matching
patient”, and Cond.notExists(subquery) for “has none”. Unlike NOT IN,
NOT EXISTS is unaffected by NULL values in the selected subquery column:
const matchingPatient = Q.select()
.field("p.id")
.from("patients", "p")
.where(Cond.columnEqual("p.id", "j.patient_id"));
const policyQuery = Q.select()
.field("j.id")
.from("jobs", "j")
.where(Cond.exists(matchingPatient)); // or Cond.notExists(matchingPatient)
const result = policyQuery.toSQLWithBindings(Q.flavors.sqlite, {
transformTable: (name) =>
Q.select().from(name).where(Cond.equal("organization_id", tenantId)),
});
// SELECT `j`.`id` FROM (SELECT * FROM `jobs` WHERE `organization_id` = ?) AS `j`
// WHERE EXISTS (SELECT `p`.`id` FROM
// (SELECT * FROM `patients` WHERE `organization_id` = ?) AS `p`
// WHERE `p`.`id` = `j`.`patient_id`)
// bindings: [tenantId, tenantId]Give correlated tables explicit aliases (j and p here). Table transforms keep
those aliases on their derived-table wrappers and do not rewrite column references;
j.patient_id therefore still refers to the wrapped outer row. The wrapper must
expose the referenced columns (for example, via SELECT *). Without an explicit
alias, derived tables use the existing default t; original table-name qualifiers
are not automatically remapped. EXISTS predicates support legacy and versioned
query serialization, strict deserialization, walk, and summarize; their SELECT
adds one subquery level for both maxDepth validation and summary depth.
Q.deserialize(input: string, options?: DeserializeOptions) accepts plain JSON
or the existing gzip/base64 compressed payload. Its new options are all optional:
allowRaw?: booleandefaults to the existing permissive behavior. Setting it tofalserejects raw expression nodes, includingQ.raw()andQ.null(), and legacy free-form SQL such as"COUNT(id)". UseFn.count("id")andQ.value(null)instead. Empty IN/NOT IN builders currently serialize an internal raw NULL marker, so their serialized form is also rejected in this mode. Strict mode permits simple identifiers and explicit literal/column encodings; malformed identifiers, operators, modifiers, and node shapes are rejected.functions?: string[]is a case-insensitive allowlist of typed function names. Omission allows all names;[]denies all. Literal strings that resemble function encodings remain data. An allowlist cannot inspect SQL accepted withallowRaw: true.maxDepth?: numberis a non-negative integer. Root depth is zero, and each nested query (including an IN subquery, INSERT SELECT, UPDATE scalar subquery, union operand, or CREATE AS SELECT child) adds one. Omission imposes no limit.
Guarded deserialization failures throw the exported DeserializeError with a stable code:
raw_not_allowed, function_not_allowed, max_depth_exceeded, invalid_node,
or unsupported_version.
walk(query: GuardQuery, visitor: QueryVisitor): void visits SELECT and mutation
trees, as well as CREATE TABLE/VIEW AS SELECT wrappers. Callbacks are optional:
interface QueryVisitor {
onTable?(name: string, alias: string | undefined, depth: number): void;
onColumn?(table: string | undefined, name: string): void;
onFunction?(name: string): void;
onSubquery?(depth: number): void;
onUnion?(): void;
onRaw?(): void;
onMutation?(kind: "insert" | "update" | "delete"): void;
}Traversal visits occurrences without modifying the tree or applying transforms.
onSubquery includes union operands. Column table values are written
qualifiers (possibly aliases), not resolved physical table names. Raw SQL is
reported without attempting to parse its contents. A callback can reject a query
by throwing. summarize(query: GuardQuery): QuerySummary returns
{ tables: string[], columns: QueryColumn[], functions: string[], maxDepth: number, hasRaw: boolean, hasUnion: boolean, kind: MetadataOperationType };
QueryColumn is { table?: string; name: string }. Names are deduplicated in
first-visit order. GuardQuery, QueryVisitor, QuerySummary, QueryColumn,
MutationKind, SQLWithBindings, DeserializeOptions, and DeserializeErrorCode
are exported from the package root.
serialize({ version: 1 }) opts in to a root v: 1 member, for example:
{
"v": 1,
"type": "SelectQuery",
"tables": [{ "type": "Table", "source": "users" }]
}Version 1 retains the existing AST fields and expression encodings:
!!!<JSON> for values, !D!<epoch milliseconds>!! for dates,
!!<JSON>!! for raw SQL, FN(<JSON array>) for functions, and
OP(<JSON array>) for arithmetic. Query children are nested objects; UPDATE
values and function/condition expressions may contain serialized strings.
The version applies to the whole tree, so children need no version member.
serialize({ version: 1, compress: true }) compresses that same envelope.
deserialize accepts both unversioned payloads and v: 1, and rejects any
other explicit version with DeserializeError("unsupported_version").
Default serialize() and toJSON() retain the legacy format and bytes.
Run the test suite using the test script:
npm run testContributions are welcome. Please submit a pull request or create an issue on the Github repository.
This project is licensed under the MIT License.