Runtime
Every generated query function accepts a database handle wrapping your own driver:
const db = sqlite(new DatabaseSync('app.db')) // SQLite
const db = postgres(client) // Postgreshyper-sql doesn't bundle or import any database driver—you pass in whatever driver instance your app already uses.
SQLite
sqlite(raw) accepts any DatabaseSync-compatible database, such as Node's built-in node:sqlite. Queries execute synchronously. Under the hood, the handle caches each prepared statement on first use and reads rows in array mode for minimal overhead.
SQLite INTEGER columns map to JavaScript number by default. If a returned value falls outside JavaScript's safe integer range (±2⁵³), reading that row throws SqlDecodeError instead of silently losing precision. For 64-bit integers, add -- @type bigint on the column in schema.sql. See bigint.
Postgres
postgres(client) accepts any client implementing pg's query({ text, values, rowMode: 'array', types }) signature, including pg.Client. Queries return promises.
hyper-sql passes custom type parsers to your driver so values arrive in the exact format the mapper expects:
int8decodes tobigint(unless you configure"int8": "number").numericstays an exactstringstraight from Postgres.- Dates and timestamps stay raw
strings unless you enable"timestamp": "Date".
See Transactions in Postgres for db.transaction(...).
Running in Node
Node 24+ strips TypeScript types natively, so you can run your .ts entry point directly with hyper-sql's module loader hook:
node --import @exprtlabs/hyper-sql/register main.tsThe hook intercepts .sql imports and compiles them using the same logic as the TypeScript content mapper. It locates your project by walking up from the imported .sql file to the nearest tsconfig.json containing a hyper-sql contentMappers entry. Make sure your lock file is present and up to date.
Use tsc --runExternalCode to type-check the same project.
Be sure to install @types/node as a dev dependency and set "types": ["node"] in tsconfig.json; otherwise imports like node:fs and node:sqlite will fail with TS2591.
Tools without content mapper support
If you're using a bundler, test runner, or compiler that doesn't support TypeScript 7.1 content mappers yet, run hyper-sql emit. It writes a .sql.gen.ts file alongside every .sql file so any standard TypeScript tool can import and check them. See CLI.
Runtime validation
Every generated query validates each cell it reads against the types recorded in sql.lock. If the database returns an unexpected type at runtime, hyper-sql throws SqlDecodeError naming both the query and the column.
Queries run through unchecked<Row>(db, text, params, decode) are also validated through the decoder you provide. See Dynamic SQL at runtime.
Performance characteristics
hyper-sql is designed to stay close to raw driver speed without sacrificing type safety:
- Zero query-builder overhead: Static queries compile to pre-numbered SQL strings at build time, and dynamic queries (
/*if*/,/*on*/,/*sort*/) cache each rendered combination once on first use. - Prepared statement caching (SQLite): Each SQL string is prepared once per
sqlite(...)handle and reused synchronously with noPromiseoverhead. - Array-mode decoding: Both SQLite (
setReturnArrays(true)) and Postgres (rowMode: 'array') skip driver-side object construction, letting generated decoders unpack fixed indices (r[0],r[1], …) into monomorphic object literals. - Single-query SQL patterns: Native typing for nested
json_agg/json_group_arrayrows, list parameters, bulkunnestinserts, andMERGEhelps eliminate N+1 round-trips. - Native-ready: Zero production dependencies and reflection-free generated code compile cleanly under HyperTS strict mode as well as Node 24+.