Skip to content

Runtime ​

Every generated query function accepts a database handle wrapping your own driver:

ts
const db = sqlite(new DatabaseSync('app.db'))   // SQLite
const db = postgres(client)                     // Postgres

hyper-sql doesn't bundle or import any database driver—you pass in whatever driver instance your app already uses.

What happens at runtime on each query callA typed call uses pre-numbered SQL (or a cached dynamic form), executes via your own database driver in array mode with prepared-statement caching on SQLite, validates each positional cell against sql.lock, and returns typed Row objects or throws SqlDecodeError.1. Pre-numbered SQL?1, ?2 / $1, $2Static SQL string, orcached dynamic FormZero AST / builder2. Your Drivernode:sqlite / pgCached prepared stmt(SQLite) · array row modeNo bundled driver3. Positional Checksr[0], r[1], r[2]Type + safe-int + JSONguards per columnSqlDecodeError on drift4. Typed ResultRow / Row[]Fixed-shape objectliterals (V8 & native)100% type-proven
Every call skips query-builder AST work, reads rows in array mode, and checks each cell before returning typed objects.

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:

  • int8 decodes to bigint (unless you configure "int8": "number").
  • numeric stays an exact string straight 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:

sh
node --import @exprtlabs/hyper-sql/register main.ts

The 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 no Promise overhead.
  • 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_array rows, list parameters, bulk unnest inserts, and MERGE helps 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+.