generates typed query builders from .sql files.
instead of reimplementing a sql parser and schema catalog like sqlc, queries are prepared against a real node:sqlite database (result columns are typed from StatementSync#columns() and schema (pragma table_xinfo))
requires node.js >= 24.10 (DatabaseSync#setAuthorizer).
import { DatabaseSync } from "node:sqlite";
import { generateDirectory } from "sql-codegen";
// set up database as it would also be configured at runtime
using db = new DatabaseSync(":memory:");
db.exec(schema);
db.function("my_function", (value) => value);
generateDirectory({
db,
// literal type discriminator on `Query` / `BoundQuery`
database: "main",
// every `<name>.sql` produces a `<name>.ts` next to it
directory: "src/query/main",
});generate(source, options) returns the generated module for a single file's contents instead of writing it.
generated files import Query and BoundQuery from sql-codegen/types (the runtime contract in src/types.ts).
the import is type-only, so sql-codegen can remain a dev dependency.
(typesVersions makes the subpath resolvable for consumers using legacy "moduleResolution": "node", which ignores exports.)
to import them from elsewhere, set typesImport to a module specifier relative to generated files.
-- name: GetDevice :one
-- column: duplicates text not null
-- any other comment is kept as documentation
select
id,
name,
(select json_group_array(id) from device d1 where d1.canonical_id = d.id) duplicates
from
device d
where
id = @id;- every statement is preceded by
-- name: <Name> <command>and terminated by;(or the end of the file):one→ single row (or none),:many→ rows,:exec→ no rows- the name prefix determines the connection mode:
Get→"r";Insert,Update,Upsert,Delete→"w". reading queries that write are rejected (detected with an authorizer while preparing)
- parameters are named (
@name,:name,$name) and are rewritten to numbered parameters (?1,?2, …) in order of first appearance. positional parameters (?,?1) are rejected, as they can't be named. parameters are typed asSQLInputValue, inferring them would require examining the query plan -- column: <name> <declared type> [not null]annotations (between-- name:and the statement) type result columns. the declared type is interpreted with sqlite's affinity rules, like the type of a table column. quote names that contain whitespace (-- column: "some name" text). annotations that match the inferred type and null-ability are rejected as redundant, so they can't go stale as inference improves
| affinity of origin column | integerMode: "number" |
integerMode: "bigint" |
|---|---|---|
| integer | number |
bigint |
| real | number |
number |
| numeric | number |
number | bigint |
| text | string |
string |
| blob | Uint8Array |
Uint8Array |
none / any |
unknown |
unknown |
columns are nullable unless their origin column is declared not null (or is a non-nullable primary key).
columns that don't originate from a table column are typed from the expression when it can never yield null,
matched against the result list of the outermost select (or returning clause):
| expression | affinity |
|---|---|
count, row_number, rank, dense_rank, random, changes, total_changes, last_insert_rowid |
integer |
total, percent_rank, cume_dist |
real |
typeof, quote, concat, json_array, json_object, json_group_array, json_group_object |
text |
jsonb_array, jsonb_object, jsonb_group_array, jsonb_group_object |
blob |
[not] exists (…), … is [not] null, … isnull, … notnull |
integer |
integer, real, text ('…'), blob (x'…') literals, current_date / current_time / current_timestamp |
matching |
function calls may carry filter (…) and over (…) clauses. in compound selects, every arm has to agree
sqlite only reports the origin of a result column, not how it got there, so inference is deliberately simple:
- any other expression (e.g.
case,coalesce, arithmetic, scalar subqueries,cast(…)) is typedunknownand needs a-- column:annotation.cast(… as …)can't replace annotations, sqlite doesn't report a declared type for it - when a statement contains an outer join (
left/right/full), every column is considered nullable - compound selects (
union, …) report the origin of the first select, scalar subqueries the origin of their result column, so null-ability may be too narrow (e.g. a scalar subquery without rows yieldsnull) - result column names have to be unique, they'd otherwise collapse in object row mode
- any authorizer installed on the provided database is replaced while preparing, and removed afterwards
part of the repository's pnpm workspace, dependencies are installed by pnpm install in the repository root.
pnpm run check # tsc
pnpm run lint # biome (formatting, lint, import order)
pnpm run format # apply biome fixes
pnpm test