Skip to content

Examples ​

The examples/ directory in the repository contains a small, self-contained project for every hyper-sql feature. Each folder includes:

  • schema.sql — the database schema
  • src/*.sql — the query files
  • main.ts — the TypeScript code calling those queries
  • expected.txt — the exact output from running main.ts

Click any link below to open that project straight in the playground, where you can inspect the generated TypeScript, tweak the SQL, and run it live. The SQLite examples model a bookshop; the Postgres examples model flights and bookings.

Start here ​

ExampleWhat it showsOpen
One query, one functionA single-statement .sql file exports one typed function.SQLite
ParametersUse :name placeholders in SQL and pass values as a typed object.SQLite · Postgres
One row or manyChoose between returning a single row or an array of rows.SQLite · Postgres
Multiple queries in one fileUse -- @name to export several functions from a single .sql file.SQLite · Postgres
Type annotationsOverride or brand a column, expression, or parameter type.SQLite · Postgres

Writing data ​

ExampleWhat it showsOpen
INSERT with RETURNINGGet the newly inserted rows back, typed from the RETURNING clause.SQLite · Postgres
Bulk insertsThree ways to insert multiple rows in a single statement.SQLite · Postgres
INSERT from SELECTInsert rows from a subquery or from an array parameter.SQLite
Optional INSERT columnsOmit columns from an INSERT when the caller leaves them undefined.SQLite · Postgres
Partial UPDATEUpdate only the columns the caller provides.SQLite · Postgres
MERGEUpsert rows in one statement, with or without RETURNING.Postgres
Write and read in one queryRun an INSERT inside a CTE, then join and return the inserted rows.Postgres

Dynamic queries ​

ExampleWhat it showsOpen
Optional filtersInclude a WHERE condition only when its parameter is provided.SQLite · Postgres
Boolean flagsToggle SQL fragments on or off with a boolean flag.SQLite · Postgres
Dynamic ORDER BYLet callers pick from a safe, fixed list of sort orders.SQLite · Postgres
Search pageCombine optional filters, boolean flags, and dynamic sorting in one query.SQLite · Postgres

Lists, arrays, and JSON ​

ExampleWhat it showsOpen
Match a list (SQLite)Pass an array parameter and match it with IN + json_each.SQLite
Match a list (Postgres)Pass an array parameter and match it with = ANY.Postgres
Array parametersPass Postgres arrays and expand them into rows with unnest.Postgres
Array columnsRead Postgres array columns as typed TypeScript arrays.Postgres
Rows from JSONQuery json_each and json_tree without manual annotations.SQLite
Table functions and VALUESRead rows from LATERAL, unnest, generate_series, and VALUES.Postgres
Nested rowsFetch child rows as a typed JSON array inside each parent row.SQLite · Postgres

Nullability ​

ExampleWhat it showsOpen
LEFT JOINColumns from the right-hand table automatically become nullable.SQLite · Postgres
FULL JOINColumns from both tables automatically become nullable.SQLite · Postgres
CTE behind an outer joinA CTE on the outer side of a LEFT JOIN produces nullable columns.SQLite · Postgres
coalesceA non-null fallback argument makes the result non-null.SQLite · Postgres
Aggregates on empty tablescount() returns 0, while sum() and max() can return null.SQLite · Postgres
Scalar subqueriesA subquery in the SELECT list is typed as nullable.SQLite · Postgres
ROLLUP subtotalsGrouped columns become nullable in ROLLUP subtotal rows.Postgres
ViewsQuery a view like a table, with all view columns typed as nullable.SQLite · Postgres

Column types ​

ExampleWhat it showsOpen
64-bit bigints (SQLite)Read SQLite INTEGER columns as bigint for values beyond ±2^53.SQLite
int8 as bigintPostgres int8 maps to bigint by default.Postgres
int8 as numberMap all Postgres int8 columns to number via project options.Postgres
Booleans in SQLiteRead and write 0/1 INTEGER columns as TypeScript booleans.SQLite
ANY columnsHandle mixed-type ANY columns in SQLite STRICT tables.SQLite
Timestamps as DateDecode and bind Postgres timestamps as UTC Date objects.Postgres
EnumsPostgres enums map automatically to string literal unions.Postgres
Validated JSON columnsType a JSON column using a runtime type guard function.SQLite · Postgres

TypeScript errors in main.ts ​

Host examples ​

These examples rely on Node modules (such as node:fs or pg), so they run locally rather than in the browser playground:

  • examples/host/node-pg/
  • examples/host/node-sqlite/
  • examples/host/transaction/
  • examples/host/unchecked-postgres/
  • examples/host/unchecked-sqlite/

Error examples ​

examples/errors/ contains a dedicated reproduction project for every error message hyper-sql and hyper-sql pull can report. Each folder has a failing query plus a README.md explaining the error and how to fix it. Browse the full catalog—with direct playground links—on the Errors reference page.