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 schemasrc/*.sql— the query filesmain.ts— the TypeScript code calling those queriesexpected.txt— the exact output from runningmain.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
| Example | What it shows | Open |
|---|---|---|
| One query, one function | A single-statement .sql file exports one typed function. | SQLite |
| Parameters | Use :name placeholders in SQL and pass values as a typed object. | SQLite · Postgres |
| One row or many | Choose between returning a single row or an array of rows. | SQLite · Postgres |
| Multiple queries in one file | Use -- @name to export several functions from a single .sql file. | SQLite · Postgres |
| Type annotations | Override or brand a column, expression, or parameter type. | SQLite · Postgres |
Writing data
| Example | What it shows | Open |
|---|---|---|
| INSERT with RETURNING | Get the newly inserted rows back, typed from the RETURNING clause. | SQLite · Postgres |
| Bulk inserts | Three ways to insert multiple rows in a single statement. | SQLite · Postgres |
| INSERT from SELECT | Insert rows from a subquery or from an array parameter. | SQLite |
| Optional INSERT columns | Omit columns from an INSERT when the caller leaves them undefined. | SQLite · Postgres |
| Partial UPDATE | Update only the columns the caller provides. | SQLite · Postgres |
| MERGE | Upsert rows in one statement, with or without RETURNING. | Postgres |
| Write and read in one query | Run an INSERT inside a CTE, then join and return the inserted rows. | Postgres |
Dynamic queries
| Example | What it shows | Open |
|---|---|---|
| Optional filters | Include a WHERE condition only when its parameter is provided. | SQLite · Postgres |
| Boolean flags | Toggle SQL fragments on or off with a boolean flag. | SQLite · Postgres |
| Dynamic ORDER BY | Let callers pick from a safe, fixed list of sort orders. | SQLite · Postgres |
| Search page | Combine optional filters, boolean flags, and dynamic sorting in one query. | SQLite · Postgres |
Lists, arrays, and JSON
| Example | What it shows | Open |
|---|---|---|
| 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 parameters | Pass Postgres arrays and expand them into rows with unnest. | Postgres |
| Array columns | Read Postgres array columns as typed TypeScript arrays. | Postgres |
| Rows from JSON | Query json_each and json_tree without manual annotations. | SQLite |
| Table functions and VALUES | Read rows from LATERAL, unnest, generate_series, and VALUES. | Postgres |
| Nested rows | Fetch child rows as a typed JSON array inside each parent row. | SQLite · Postgres |
Nullability
| Example | What it shows | Open |
|---|---|---|
| LEFT JOIN | Columns from the right-hand table automatically become nullable. | SQLite · Postgres |
| FULL JOIN | Columns from both tables automatically become nullable. | SQLite · Postgres |
| CTE behind an outer join | A CTE on the outer side of a LEFT JOIN produces nullable columns. | SQLite · Postgres |
| coalesce | A non-null fallback argument makes the result non-null. | SQLite · Postgres |
| Aggregates on empty tables | count() returns 0, while sum() and max() can return null. | SQLite · Postgres |
| Scalar subqueries | A subquery in the SELECT list is typed as nullable. | SQLite · Postgres |
| ROLLUP subtotals | Grouped columns become nullable in ROLLUP subtotal rows. | Postgres |
| Views | Query a view like a table, with all view columns typed as nullable. | SQLite · Postgres |
Column types
| Example | What it shows | Open |
|---|---|---|
| 64-bit bigints (SQLite) | Read SQLite INTEGER columns as bigint for values beyond ±2^53. | SQLite |
| int8 as bigint | Postgres int8 maps to bigint by default. | Postgres |
| int8 as number | Map all Postgres int8 columns to number via project options. | Postgres |
| Booleans in SQLite | Read and write 0/1 INTEGER columns as TypeScript booleans. | SQLite |
| ANY columns | Handle mixed-type ANY columns in SQLite STRICT tables. | SQLite |
| Timestamps as Date | Decode and bind Postgres timestamps as UTC Date objects. | Postgres |
| Enums | Postgres enums map automatically to string literal unions. | Postgres |
| Validated JSON columns | Type a JSON column using a runtime type guard function. | SQLite · Postgres |
TypeScript errors in main.ts
- Accessing a nullable column
- Wrong parameter type
- Reading an unselected column
- Misspelled parameter name
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.