fastify-auto-sqlapi 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md CHANGED
@@ -6,7 +6,7 @@ You are configuring a Fastify server that uses `fastify-auto-sqlapi` to auto-gen
6
6
 
7
7
  The plugin generates REST endpoints (search, get, insert, update, delete, bulk upsert, bulk delete) from database table definitions. No ORM — raw SQL. Supports **PostgreSQL**, **MySQL**, and **MariaDB**. The consumer defines table configurations, and the plugin handles routing, validation, and query execution.
8
8
 
9
- **The database schema is the API contract, on purpose.** The exposed shape follows the stored shape *by default*, and every part of that default is overridable — `readExclude`, `writeExclude`, a trimmed Schema, `schemaOverrides`, `computedFields`, `afterRead`, `operations`, relation `alias`/`fields`, `extraFilters`. Two consequences for how you work:
9
+ **The database schema is the API contract, on purpose.** The exposed shape follows the stored shape *by default*, and every part of that default is overridable — `readExclude`, `writeExclude`, `excludeColumns` (CLI config), `schemaOverrides`, `computedFields`, `afterRead`, `operations`, relation `alias`/`fields`, `extraFilters`. Two consequences for how you work:
10
10
 
11
11
  - Do not propose a DTO or mapping layer, and do not reshape a response by hand in a route wrapper. Reach for the lever that matches the concern (the table is in [ADR 0017](./docs/adr/0017-the-schema-is-the-contract.md)); when none fits, the operation is not CRUD — write a custom route and call `app.sqlApi.*` inside it, which keeps filters, joins, tenant scoping, hooks and validation.
12
12
  - The target surface is internal: back-office and admin tools where the schema *is* the domain model. If the user is building a **public or third-party API** — consumers they cannot redeploy with their schema, a versioned contract — say so, and keep those tables off the auto routes via `operations` rather than generating the surface from the schema.
package/AGENTS_BACKEND.md CHANGED
@@ -58,13 +58,16 @@ export default {
58
58
  dialect: 'postgres', // 'postgres' | 'mysql' | 'mariadb' (default: 'postgres')
59
59
  // envFile: '../../.env', // path to .env file, relative to cwd (default: '.env')
60
60
  // excludeTables: ['knex_*'],// tables invisible to schema generation (see below)
61
+ // excludeColumns: ['*.password_hash'], // columns invisible to schema generation (see below)
61
62
  };
62
63
  ```
63
64
 
64
- That's it — only these five fields exist. **No connection string, no migration path.** If the file is missing, defaults are used. The `dialect` can also be passed via CLI flag `--dialect mysql`. The `envFile` is useful in monorepo setups where the `.env` lives at the repo root.
65
+ That's it — only these six fields exist. **No connection string, no migration path.** If the file is missing, defaults are used. The `dialect` can also be passed via CLI flag `--dialect mysql`. The `envFile` is useful in monorepo setups where the `.env` lives at the repo root.
65
66
 
66
67
  **`excludeTables`** keeps migration bookkeeping, PostGIS internals and the like out of generation: exact table names or `*` globs, matched against the **DB table name** (not the generated schema name), with everything else literal. An excluded table is invisible to the generator — its schema file is not created, and one generated by an earlier run is removed as an orphan on the next full run, exactly like the schema of a dropped table. See [ADR 0009](./docs/adr/0009-schema-exclusion-config-blacklist.md).
67
68
 
69
+ **`excludeColumns`** does the same for single columns: `"<table>.<column>"` entries matched against the **DB names** (not the generated field names), with `*` as a wildcard on either side — `'user.password_hash'`, `'*.internal_*'`, `'audit_*.raw_payload'`. An excluded column does not exist for the API at all: it is not in the Schema, so no route reads, writes, filters or orders by it, and the manifest never lists it. This is the lever for *"this column must not exist for the API"*; `readExclude` / `writeExclude` on the table config are for a column that stays in the Schema and loses one side. A primary key column cannot be excluded (every route identifies rows with it), and a pattern that would leave a table with no columns fails the run: that is `excludeTables`' job. Never edit a generated Schema file by hand — it is overwritten on the next run. See [ADR 0019](./docs/adr/0019-column-exclusion-is-generation-time-config.md).
70
+
68
71
  ### 3. Configure database connection (env vars)
69
72
 
70
73
  The CLI reads DB connection from environment variables (NOT from the config file). `DATABASE_URL` takes priority for all dialects.
@@ -1014,9 +1017,9 @@ output, it is the part that does not depend on anyone opening the generated file
1014
1017
  - **All fields Optional in response**: `RETURNING *` may return any subset. Response schemas use `Type.Partial`.
1015
1018
  - **Nullable columns use `Nullable(T)` (type-array form)**: the generator emits `Type.Optional(Nullable(T))` for nullable columns — `Nullable` (exported by the package) produces `{ type: ['integer', 'null'] }`. Do NOT replace it with `Type.Union([T, Type.Null()])`: under Fastify's default Ajv `coerceTypes` a union corrupts values through its branches (`null` → `0` with the Null branch last, `0`/`""` → `null` with it first), while the type array validates and serializes NULL correctly. A bare `Type.Optional(T)` is also wrong: NULL serializes as `0`/`""`. In `filters`, an explicit `null` on a schema field builds `WHERE col IS NULL`.
1016
1019
  - **`excludeFromCreation`**: **IMPORTANT** — auto-increment PKs (e.g. `id` serial/auto_increment) MUST be listed here, otherwise INSERT will try to send them and fail. The CLI auto-detects this and adds it by default. Also useful for `createdAt`/`updatedAt` columns managed by DB defaults or hooks. It strips **client-supplied** values only: the payload is sanitized before `beforeInsert` runs, so a value the hook assigns to an excluded field (e.g. a server-generated TEXT id) DOES reach the INSERT. Same for the engine's FK auto-fill on secondaries — an excluded FK column does not suppress it. It is an **ergonomics tool for creation, NOT a field-level security mechanism**: it does not apply to updates — every Schema field is updatable by default, by design. Field-level update rules (`isAdmin`, roles, owner/tenant columns, state fields) are product logic: enforce them in `beforeUpdate` (silent strip) or `validate` (loud 400), or move the privileged transition to a dedicated endpoint and keep it off the auto routes via `operations`.
1017
- - **`readExclude`**: fields hidden from every read — not projected by search/get, omitted from read response schemas and from this table's default (`*`) join selection when it is a join target. Referencing one from `filters`, `conditions`, `orderBy`, `joinGroup` aggregations or an explicit join `selection` returns 400: hiding a field from output while allowing it to be filtered would leak its value by bisection. **Writes are unaffected** — insert/update/bulk still accept the field, so a column can be writable but never readable (password hash, access token). Primary keys cannot be excluded. Complementary to trimming the Schema, which removes the column from reads *and* writes. It is **per table and static**: a column hidden here is hidden from every caller, including admins. To keep a column readable on the table's own routes but not through a relation, trim the Schema the *relation* is declared with instead — see the read-grant warning under [buildRelation](#buildrelation-signature).
1018
- - **`writeExclude`**: the write-side counterpart of `readExclude` — fields removed from the insert, update and bulk-upsert bodies (main *and* secondaries) and dropped again inside the engines, which `sqlApi.*` reaches without those schemas. Reads are untouched: the field is still projected, filterable and orderable. It exists for the columns the database refuses to be told about. **A `GENERATED ALWAYS AS (<expr>)` column is already covered without any config**: the CLI records it in the Schema's `generatedFields`, and both engines reject a statement that so much as names such a column, so offering it as writable could only turn a client mistake into a `500`. The `writeExclude` key is for the rest — a column a trigger owns, one a migration is about to drop. It is **static and applies to everyone**, admins included; a rule that depends on *who* is asking is product logic and belongs in `beforeUpdate` (silent strip) or `validate` (loud 400), per [ADR 0004](./docs/adr/0004-updates-always-open.md). Not to be confused with `excludeFromCreation`, which whitelists **client input on insert only** and deliberately lets a hook assign the field afterwards — `writeExclude` runs **after** the hooks, because the column refuses the value whoever assigned it. `defineTable` rejects a field that is not in the schema, the primary key (the update body identifies the row with it) and a field that is also in `readExclude` (neither readable nor writable means: remove it from the Schema). See [ADR 0015](./docs/adr/0015-non-writable-columns.md), which narrows [ADR 0004](./docs/adr/0004-updates-always-open.md).
1019
- - **Database-computed and identity columns are detected by the CLI**: `sqlapi-generate-schema` reads `is_generated` (PostgreSQL) / `EXTRA` (MySQL, `VIRTUAL GENERATED` and `STORED GENERATED`) and prints the columns it found, per table, before writing anything — read that output. Generated columns land in the Schema's `generatedFields` and are named in a comment in the generated `Table*.ts`. Separately, `GENERATED ... AS IDENTITY` (PostgreSQL) has no `column_default` to reveal that the database fills it in: it is now flagged like MySQL's `AUTO_INCREMENT`, so the field is Optional and the CLI lists it under `excludeFromCreation` instead of emitting a mandatory primary key the database then refuses.
1020
+ - **`readExclude`**: fields hidden from every read — not projected by search/get, omitted from read response schemas and from this table's default (`*`) join selection when it is a join target. Referencing one from `filters`, `conditions`, `orderBy`, `joinGroup` aggregations or an explicit join `selection` returns 400: hiding a field from output while allowing it to be filtered would leak its value by bisection. **Writes are unaffected** — insert/update/bulk still accept the field, so a column can be writable but never readable (password hash, access token). Primary keys cannot be excluded. Complementary to `excludeColumns` in `sqlapi.config.ts`, which removes the column from the Schema, hence from reads *and* writes. It is **per table and static**: a column hidden here is hidden from every caller, including admins. To keep a column readable on the table's own routes but not through a relation, trim the Schema the *relation* is declared with instead — see the read-grant warning under [buildRelation](#buildrelation-signature).
1021
+ - **`writeExclude`**: the write-side counterpart of `readExclude` — fields removed from the insert, update and bulk-upsert bodies (main *and* secondaries) and dropped again inside the engines, which `sqlApi.*` reaches without those schemas. Reads are untouched: the field is still projected, filterable and orderable. It exists for the columns the database refuses to be told about. **A `GENERATED ALWAYS AS (<expr>)` column is already covered without any config**: the CLI records it in the Schema's `generatedFields`, and both engines reject a statement that so much as names such a column, so offering it as writable could only turn a client mistake into a `500`. The `writeExclude` key is for the rest — a column a trigger owns, one a migration is about to drop. It is **static and applies to everyone**, admins included; a rule that depends on *who* is asking is product logic and belongs in `beforeUpdate` (silent strip) or `validate` (loud 400), per [ADR 0004](./docs/adr/0004-updates-always-open.md). Not to be confused with `excludeFromCreation`, which whitelists **client input on insert only** and deliberately lets a hook assign the field afterwards — `writeExclude` runs **after** the hooks, because the column refuses the value whoever assigned it. `defineTable` rejects a field that is not in the schema, the primary key (the update body identifies the row with it) and a field that is also in `readExclude` (neither readable nor writable means: `excludeColumns` in `sqlapi.config.ts`). See [ADR 0015](./docs/adr/0015-non-writable-columns.md), which narrows [ADR 0004](./docs/adr/0004-updates-always-open.md).
1022
+ - **Database-computed and identity columns are detected by the CLI**: `sqlapi-generate-schema` reads `is_generated` (PostgreSQL) / `GENERATION_EXPRESSION` (MySQL, non-empty only for a real `GENERATED ALWAYS AS (...)` column; `EXTRA` is a fallback matched on `VIRTUAL GENERATED` / `STORED GENERATED`, never on the word alone — MySQL writes `DEFAULT_GENERATED` there for any default *expression*, `DEFAULT CURRENT_TIMESTAMP` included, and such a column is writable) and prints the columns it found, per table, before writing anything — read that output. Generated columns land in the Schema's `generatedFields` and are named in a comment in the generated `Table*.ts`. Separately, `GENERATED ... AS IDENTITY` (PostgreSQL) has no `column_default` to reveal that the database fills it in: it is now flagged like MySQL's `AUTO_INCREMENT`, so the field is Optional and the CLI lists it under `excludeFromCreation` instead of emitting a mandatory primary key the database then refuses.
1020
1023
  - **`upsertMap`**: when present for a schema, INSERT becomes upsert. PostgreSQL: `ON CONFLICT (...) DO UPDATE`. MySQL/MariaDB: `ON DUPLICATE KEY UPDATE`. Applies to both main and secondary tables.
1021
1024
  - **`schemaOverrides`**: narrow an auto-generated field to a stricter TypeBox type (e.g. `{ email: Type.String({ format: 'email' }) }`) where `information_schema` could only report `text`. **Write bodies only** — insert, update, bulk-upsert. An override rules what the API accepts *from now on*, while the rows already stored predate it: a column narrowed to `format: 'email'` today holds whatever was accepted last year, and one made mandatory today is `NULL` on every pre-existing row. Narrowing the response would publish a promise nobody can retroactively make true, so responses and the `filters` map keep the generated type. (Nothing would break at runtime either way — Fastify does not validate responses, and `fast-json-stringify` ignores `format`/`pattern`/`minLength`/`minimum`/`enum` entirely; it acts only on `type`, by coercion.) The override is taken **verbatim**: no `Type.Optional` makes the field mandatory, no `Nullable` rejects an explicit `null`, whatever the column allows — which is how you make a column the DB had to leave nullable (a new column on a populated table) mandatory going forward. The original Schema file is never modified; the primary key is mandatory in the update body (it identifies the row); overrides appear in Swagger.
1022
1025
  - **`afterRead`**: the read-side counterpart of `beforeInsert`/`beforeUpdate` — the place to decrypt a column, unpack a blob, rescale a stored unit. Called **once per result set** with camelCase rows mutated **in place** (a hook that calls a remote KMS gets one invocation for the page, not one per row), and awaited. Declared on the table that **owns** the column, and it follows a join: the hook runs wherever that table's rows surface, including through a `joinMultiple`/`joinLeft` declared on another table — `ctx.source` (`'get' | 'search' | 'joinMultiple' | 'joinLeft'`) and `ctx.alias` say which. Not called on `joinGroup` (aggregates, not table rows) nor on an empty result. Rows cannot be added or removed — the pagination `COUNT` has already run, so dropping rows would report a `total` that does not match; narrowing what a caller sees is `filters`/`tenantScope`/`readExclude`. On the main table it runs **after** the join side queries, which correlate on the values the database returned: a hook rewriting the column a relation joins on does not break the correlation. Two things it does not reach, by design: `filters`, `orderBy` and `compute*` run in SQL against the **stored** value (searching an encrypted column searches the ciphertext), and it transforms **values, not types** — the response is serialized against the generated schema, so a hook returning an object where the column is `text` yields `"[object Object]"`, and one returning `12.34` on an `integer` column yields `12`. Decrypting `text`→`text` or rescaling `numeric`→`numeric` is what it is for; if the stored and API types genuinely differ, that is a custom route.
@@ -1025,9 +1028,9 @@ output, it is the part that does not depend on anyone opening the generated file
1025
1028
  - **All hooks and validators receive camelCase records**: `validate` and the whole hook matrix (`beforeInsert`/`afterInsert`, `beforeUpdate`/`afterUpdate`, `beforeDelete`/`afterDelete`, `beforeBulkDelete`/`afterBulkDelete`, `afterRead`) get records keyed by schema field names (camelCase). Mutations propagate to the SQL (plugin converts to DB column format via `colMap` after the hook). The engine internally uses `snakecaseRecord(..., schema)` after user mutations to map field names to actual DB columns.
1026
1029
  - **Secondaries run the child table's `beforeInsert`**: a child row written alongside a main record is a write like any other — the child's `excludeFromCreation`, its `tenantScope` and its `beforeInsert` all apply, in that order, before the engine auto-fills the FK to main (which always wins over anything the payload or the hook put there). Its `validate` and `afterInsert` deliberately do **not** run: they change when a rejection or a side effect fires, which belongs to the host's write. See [ADR 0014](./docs/adr/0014-what-follows-a-write-join.md).
1027
1030
  - **Filters validation**: TypeBox schemas use `additionalProperties: false`. By default Fastify strips unknown fields silently. For 400 errors on unknown filters: `Fastify({ ajv: { customOptions: { removeAdditional: false } } })`.
1028
- - **Write-body whitelist**: insert/update/bulk-upsert bodies (`main` + secondaries items) are `additionalProperties: false`. Unknown properties are rejected with 400 — only columns in the generated Schema (as narrowed by `schemaOverrides`/`excludeFromCreation`) can be written, so no mass assignment of unexposed columns. Trim the Schema to keep sensitive columns unwritable.
1031
+ - **Write-body whitelist**: insert/update/bulk-upsert bodies (`main` + secondaries items) are `additionalProperties: false`. Unknown properties are rejected with 400 — only columns in the generated Schema (as narrowed by `schemaOverrides`/`excludeFromCreation`) can be written, so no mass assignment of unexposed columns. Exclude a sensitive column at generation time (`excludeColumns`) to keep it out of the Schema, hence unwritable and unreadable.
1029
1032
  - **Error responses**: errors the plugin raises itself are `4xx` with messages written for clients (validation, not found, tenant). Anything else — a driver constraint violation, a hook throwing without a `statusCode` — answers a fixed `{"statusCode":500,"error":"Internal Server Error","message":"Internal Server Error"}`: driver messages name tables, columns and constraints, and they stay on the server. **No status code is remapped** (a unique violation is a `500`, not a `409` — that mapping is product logic). The body carries `requestId` (the `reqId` Fastify logs on every line) so the real error is one grep away in the log, where it is also written via `request.log.error` — and attached as `err.cause`, so a consumer `setErrorHandler` can map `err.cause.code === '23505'` to `409` itself. **`exposeDebugInfo: true` is the only way to get the driver detail on the wire** — set it while developing, where a client building against the API cannot always read your log: the `500` then also carries `debugInfo` (`message`, `code`, `constraint`, `detail`, `stack`), additively, so the four fields above keep their production shape. Nothing else flips it: not `debug` (server-side only, and it belongs to whoever registers the plugin), not `NODE_ENV`. Errors thrown by `onRequests` hooks run before the handler and are unaffected.
1030
- - **Request limits**: `maxItemsPerPage` (default 1000) caps the search page size and is applied as the `LIMIT` even when no paginator is sent (so an empty-body search can't dump a whole table); over-limit `itemsPerPage` → 400. `maxBulkItems` (default 1000) caps the bulk array length via schema `maxItems` → 400. Programmatic `sqlApi.*` calls are uncapped.
1033
+ - **Request limits**: `maxItemsPerPage` (default 1000) caps the search page size and is applied as the `LIMIT` even when no paginator is sent (so an empty-body search can't dump a whole table); over-limit `itemsPerPage` → 400. `maxBulkItems` (default 1000) caps the bulk array length via schema `maxItems` → 400. Two further caps are **fixed, not configurable**, and bound how much work one search may ask for: at most **100 `conditions`** and **20 comma-separated `orderBy` parts** — each dotted condition and each 3-part `orderBy` token becomes its own correlated subquery, which `maxItemsPerPage` does not bound. Over either limit → 400; the request schema also caps `orderBy` at 1024 characters. Those two live in the **search engine**, not in the route, so they apply to programmatic `sqlApi.search()` as well, together with the `paginator` integer guard. What `sqlApi.*` does *not* inherit are the two configurable caps, which belong to the routes: pass `maxRows` to `sqlApi.search()` for the same `LIMIT` protection, and bound your own arrays before a bulk call.
1031
1034
  - **Tenant filtering**: when `tenantScope` is set on a table and `getTenantId` is provided in plugin options, all CRUD operations are automatically scoped to the tenant. `getTenantId` returning `null`/`undefined` = admin (no filter). Returning an array = multi-tenant user (IN clause). The tenant belongs to the *request*, not to the table it addresses: a scoped table is filtered even when reached as a join target or written as a secondary of an unscoped host, so `getTenantId` must tolerate a request with no authenticated user (`req.user?.x ?? null`).
1032
1035
 
1033
1036
  ---
@@ -77,7 +77,7 @@ Response (each section present only if requested):
77
77
 
78
78
  `orderBy` forms (comma-separable): `field [ASC|DESC]` | `alias.parentField` (joinLeft-eligible alias, adds LEFT JOIN) | `alias.fn.field` (fn: sum|min|max|avg|count|distinctCount — the joinGroup with that fn+field MUST be in the body; rows without children sort as 0). For 3-part orderBy and HAVING-style conditions: `aggregations.by` is allowed only when it equals the correlation FK (else 400); aggregation orderBy is rejected (400) on tables with distinctResults.
79
79
 
80
- Condition methods (params arity): `isEqual|isNotEqual|isGreater|isGreaterOrEqual|isLess|isLessOrEqual|isNotGreater|isNotGreaterOrEqual|isNotLess|isNotLessOrEqual` → `[v]`; `isLike|isNotLike|isILike|isNotILike` → `["%p%"]`; `isBetween|isNotBetween` → `[from,to]`; `isIn|isNotIn` → `[[v1,v2]]`; `isNull|isNotNull` → `[]`. Unknown field or method → 400.
80
+ Condition methods (params arity): `isEqual|isNotEqual|isGreater|isGreaterOrEqual|isLess|isLessOrEqual|isNotGreater|isNotGreaterOrEqual|isNotLess|isNotLessOrEqual` → `[v]`; `isLike|isNotLike|isILike|isNotILike` → `["%p%"]`; `isBetween|isNotBetween` → `[from,to]`; `isIn|isNotIn` → `[[v1,v2]]`; `isNull|isNotNull` → `[]`. Unknown field or method → 400; so is a `params` that is not an array or is shorter than the arity above (`isNull`/`isNotNull` take none).
81
81
 
82
82
  ## WRITES
83
83
 
@@ -108,4 +108,4 @@ Condition methods (params arity): `isEqual|isNotEqual|isGreater|isGreaterOrEqual
108
108
  "fields": [ { "path": "body.main.name", "code": "required", "message": "must have required property 'name'" } ] }
109
109
  ```
110
110
 
111
- Other: 404 (get/delete not found), 400 `itemsPerPage exceeds the maximum` (lower it), 500 raw DB errors (constraint violations surface unmapped).
111
+ Other: 404 (get/delete not found), 400 `itemsPerPage exceeds the maximum` (lower it), 500 raw DB errors (constraint violations surface unmapped). Fixed complexity caps, per search: **100 `conditions`** per section (top level and each join family) and **20** comma-separated `orderBy` parts (`orderBy` string ≤ 1024 chars) — over either → 400: collapse a long list of equality conditions into one `isIn`. `paginator.page` and `paginator.itemsPerPage` must be integers ≥ 1 — send numbers, not querystring strings.
@@ -1,5 +1,7 @@
1
1
  # Breaking Changes — Join API redesign
2
2
 
3
+ *Landed in **0.1.3**.*
4
+
3
5
  The join API was rewritten to be explicit, alias-based, and to add a real `LEFT JOIN` mode (`joinLeft`) for N:1 relationships. **No backward compatibility** — old names are rejected with 400. Both backend (table configuration) and frontend (request/response shapes) are affected. This document is the single source of truth for migrating either side.
4
6
 
5
7
  ## Why this changed
@@ -265,6 +267,8 @@ The engine rejects, with `statusCode: 400`, any of:
265
267
 
266
268
  # Breaking Change — computed field placeholders use `?` markers
267
269
 
270
+ *Landed in **0.1.6**.*
271
+
268
272
  `ComputedFieldExpr.expr` must now mark each bound value with `?`. The engine assigns the
269
273
  placeholder positions. Writing `$1` (or `db.ph(n)`) inside the expression no longer works and
270
274
  is rejected with a descriptive error.
@@ -320,6 +324,8 @@ values in the parameter order.
320
324
 
321
325
  # Breaking Change — by-single-id operations disabled for composite primary keys
322
326
 
327
+ *Landed in **0.1.11**.*
328
+
323
329
  Since **0.1.11**, tables whose `primary` is an array of more than one field no longer expose
324
330
  the operations that address a record by a single PK value:
325
331
 
@@ -385,6 +391,8 @@ endpoints previously behaved correctly for you — they are now gone and you mus
385
391
 
386
392
  # Breaking Change — unknown filter keys are rejected
387
393
 
394
+ *Landed in **0.1.14**.*
395
+
388
396
  `filters` keys that match nothing are now a `400`, on the main table and on every join family.
389
397
  Previously they were dropped without a word.
390
398
 
@@ -458,8 +466,103 @@ delegates to the target's own `filters()` and does run `extendedCondition`.
458
466
 
459
467
  ---
460
468
 
469
+ # Breaking Change — request limits, condition arity and tenant scope inside subqueries
470
+
471
+ *Landed in **0.2.0**.*
472
+
473
+ ## Why this changed
474
+
475
+ Four ways a single search request could do more than the plugin admitted, and one where the
476
+ declared scope was not the enforced one.
477
+
478
+ - `maxItemsPerPage` bounds the **rows a search returns**, not the work done to find them. Every
479
+ dotted `conditions` entry and every 3-part `orderBy` token becomes its own correlated subquery,
480
+ so an uncapped list turned one request into arbitrarily many.
481
+ - `paginator.page` and `paginator.itemsPerPage` are rendered straight into `LIMIT`/`OFFSET`.
482
+ `Paginator` is a compile-time type only, and `sqlApi.search()` is a documented public API a
483
+ consumer may hand an unvalidated querystring — the integer check has to exist at runtime.
484
+ - `key in map` and `map[key]` walk `Object.prototype`, so `constructor`, `toString`, `valueOf`,
485
+ `hasOwnProperty` and `__proto__` satisfied every "is this a known field" check and reached the
486
+ SQL builders, where they rendered as `undefined` or as a column that does not exist.
487
+ - A condition method was dispatched with whatever `params` carried. A missing or wrongly shaped
488
+ one threw a `TypeError` inside the builder, which the error handler can only report as a 500.
489
+ - `tenantScope` was applied to the main query and to the tables a request addressed directly. The
490
+ subqueries and child writes the same request produced carried no scope, so a scoped table was
491
+ still readable and writable through its own relations.
492
+
493
+ ## What changed
494
+
495
+ Every row below answers **400** where it previously did something else.
496
+
497
+ | Input | Rule now | Before |
498
+ |---|---|---|
499
+ | `conditions` | at most **100** entries, per section (top level and each join family) — schema `maxItems` plus an engine backstop | unbounded |
500
+ | `orderBy` | at most **20** comma-separated parts; the request schema also caps the string at **1024** characters | unbounded |
501
+ | `paginator` | `page` and `itemsPerPage` must both be integers `>= 1`, checked before any query runs | any value, rendered into `LIMIT`/`OFFSET` as-is |
502
+ | condition `params` | must be an array; single-value methods need 1 entry, `isBetween`/`isNotBetween` 2, `isIn`/`isNotIn` an array as the first entry | passed to the builder as-is → `TypeError` → 500 |
503
+ | field names | inherited `Object.prototype` keys are not fields, in `filters`, condition fields, `orderBy` and `selectComputed` | resolved as known fields and reached the SQL |
504
+
505
+ The two caps are exported as `MAX_CONDITIONS` and `MAX_ORDER_BY_PARTS`. Both checks live in the
506
+ **engine**, not in the route, which is what covers `sqlApi.search()` — no schema validates a
507
+ programmatic caller. `isNull`/`isNotNull` still take no params, and an explicit `undefined` handed
508
+ to `isIn` remains the documented no-op.
509
+
510
+ And `tenantScope` now follows the request into everything it touches:
511
+
512
+ - **secondaries** — a child row written alongside a main row is scoped and anchored by the child
513
+ table's own `tenantScope`;
514
+ - **`joinLeft`** — the predicate lands in the `ON` clause, so `LEFT` semantics survive;
515
+ - **`joinGroup` aggregation subqueries**, and the 3-part aggregation `orderBy` built on them —
516
+ they counted and summed every tenant's child rows, which disagreed with the scoped
517
+ `result.joinGroup` for the same alias and leaked the existence of the rest.
518
+
519
+ ## Who is affected
520
+
521
+ - **A client that sends more than 100 `conditions` or more than 20 `orderBy` parts.** Generated
522
+ filter UIs are the realistic case: a "select all" that emits one condition per row.
523
+ - **A caller that forwards a raw querystring to `sqlApi.search()`.** `itemsPerPage: '10'` is a
524
+ string and is now a 400: parse the querystring before you forward it.
525
+ - **Conditions assembled programmatically.** One built with a missing `params` used to reach the
526
+ builder; it is now refused up front, with the method named.
527
+ - **Any deployment using `tenantScope` with `secondaries`, `joinLeft` or `joinGroup`.**
528
+ Aggregates that used to count across tenants return smaller numbers, and an ordering built on
529
+ them changes with it. A secondary write that used to land in another tenant's rows is now
530
+ refused. This is the fix, not a regression — but a dashboard whose numbers were computed
531
+ cross-tenant will move.
532
+
533
+ Not affected: requests within the caps, a well-formed integer `paginator`, and any deployment
534
+ without `tenantScope`.
535
+
536
+ ## Migration
537
+
538
+ Split an oversized `conditions` list rather than raising anything — the caps are not configurable:
539
+
540
+ ```typescript
541
+ // Before: one request with 400 conditions
542
+ { conditions: ids.map((id) => ({ field: 'id', method: 'isEqual', params: [id] })) }
543
+
544
+ // After: one condition, one bound array
545
+ { conditions: [{ field: 'id', method: 'isIn', params: [ids] }] }
546
+ ```
547
+
548
+ Parse a querystring before it reaches `sqlApi.search()`:
549
+
550
+ ```typescript
551
+ const paginator = {
552
+ page: Number.parseInt(req.query.page ?? '1', 10),
553
+ itemsPerPage: Number.parseInt(req.query.itemsPerPage ?? '50', 10),
554
+ };
555
+ ```
556
+
557
+ Then re-read any number your product computes from `joinGroup`: if it was collected under a
558
+ tenant scope, it was counting rows that tenant cannot see, and it changes with this release.
559
+
560
+ ---
561
+
461
562
  # Breaking Change — the tenant follows the request, not the table it addresses
462
563
 
564
+ *Landed in **0.2.1**.*
565
+
463
566
  `tenantScope` on a table is now enforced even when the request was addressed to a **different**
464
567
  table that reaches it through a relation. Previously the tenant was resolved from the addressed
465
568
  table alone: if that table declared no `tenantScope`, no predicate was applied anywhere — not to
@@ -541,6 +644,8 @@ grant, and the scope is only the cap on it.
541
644
 
542
645
  # Breaking Change — secondaries run the child table's `beforeInsert`
543
646
 
647
+ *Landed in **0.2.2**.*
648
+
544
649
  ## Why this changed
545
650
 
546
651
  `processSecondaries` consulted the child table's configuration for its `excludeFromCreation` and
package/CHANGELOG.md CHANGED
@@ -7,7 +7,75 @@ All notable changes to this project are documented here. This project follows
7
7
 
8
8
  Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BREAKING_CHANGES.md)**.
9
9
 
10
- ## [Unreleased]
10
+ ## [0.2.4]
11
+
12
+ ### Added
13
+
14
+ - **`excludeColumns` config option** — the lever for *"this column must not exist for the API at
15
+ all"*. [ADR 0017](./docs/adr/0017-the-schema-is-the-contract.md), the README and `AGENTS.md`
16
+ all pointed at "trim it out of the Schema" for that case, and no such lever existed: Schema
17
+ files are generated and overwritten on every run, so the only way to drop a column was to
18
+ re-edit the file after each `sqlapi-generate-schema` — or to pair `readExclude` with
19
+ `writeExclude`, which `defineTable` refuses. `excludeColumns` in `sqlapi.config.ts` takes
20
+ `"<table>.<column>"` entries matched against the DB names, with `*` as a wildcard on either
21
+ side (`'user.password_hash'`, `'*.internal_*'`), and drops the column from the generated Schema
22
+ — its fields, `colMap` and `generatedFields` — so no route, filter, order or manifest ever
23
+ sees it. A primary key column is refused (every route identifies rows with it), and so is a
24
+ pattern that would leave a table with no columns: that is `excludeTables`' job. Applied after
25
+ `excludeTables`, so a column pattern is never checked against a table the generator does not
26
+ see. **Regenerate your schemas** after adding entries. See
27
+ [ADR 0019](./docs/adr/0019-column-exclusion-is-generation-time-config.md).
28
+
29
+ ### Changed
30
+
31
+ - **The docs no longer say "trim the Schema".** The README lever table, `AGENTS.md`,
32
+ `AGENTS_BACKEND.md` and the `defineTable` error for a field in both `readExclude` and
33
+ `writeExclude` now point at `excludeColumns`.
34
+
35
+ ## [0.2.3]
36
+
37
+ ### Fixed
38
+
39
+ - **MySQL read every default *expression* as a database-computed column.** The generator
40
+ matched `information_schema.columns.EXTRA` on the substring `generated`, and MySQL writes
41
+ `DEFAULT_GENERATED` there for **any** column whose default is an expression —
42
+ `DEFAULT CURRENT_TIMESTAMP` above all, but also `DEFAULT (UUID())`, `DEFAULT (CURRENT_DATE)`,
43
+ a JSON default. Every one of those columns came out of `sqlapi-generate-schema` listed under
44
+ `generatedFields`, which the runtime strips from the insert, update and bulk-upsert bodies and
45
+ again inside the engines: the column stopped being writable, silently, with no error anywhere —
46
+ a client sending it had the value dropped and read back whatever the default had produced. On a
47
+ schema that timestamps its tables, that is every table. Only `GENERATION_EXPRESSION` separates
48
+ the two cases — it is non-empty for a real `GENERATED ALWAYS AS (<expr>)` column and empty for a
49
+ default expression — and it is now what the flag reads, with the `EXTRA` markers
50
+ (`VIRTUAL GENERATED`, `STORED GENERATED`, `PERSISTENT GENERATED`) kept as a fallback for a
51
+ server or driver that does not return it. MySQL and MariaDB only; PostgreSQL was never
52
+ affected. **Regenerate your schemas** (`sqlapi-generate-schema`) to get the columns back.
53
+
54
+ - **The agent manifest's markdown never rendered `readOnly`.** The JSON manifest has carried the
55
+ flag since 0.2.2, but `/agent/manifest.md` — the rendering built to go into a system prompt —
56
+ dropped it, so the one consumer that reads the markdown, a model, kept putting non-writable
57
+ columns in write payloads and kept having them dropped. The notation now carries `(readOnly)`
58
+ alongside `(writeOnly)`, and the legend explains it.
59
+
60
+ - **`ApiRequest` was documented as exported and was not.** [ADR 0018](./docs/adr/0018-one-seam-to-the-http-framework.md)
61
+ and the 0.2.2 entry below both say the type is exported from the package; `src/index.ts` never
62
+ re-exported it, so a consumer typing a hook or a `getTenantId` against it had to import from a
63
+ `dist/` internal path. It is now part of the public type exports.
64
+
65
+ - **The upsert conflict guard failed open on a `NULL` owner in single-column tenant scopes.**
66
+ `col NOT IN (...)` evaluates to `NULL`, not `TRUE`, when the column is `NULL`, so a conflicting
67
+ row belonging to nobody slipped past the probe and could be claimed and overwritten by any
68
+ tenant's upsert. The single-column branch now reads `(col IS NULL OR col NOT IN (...))` —
69
+ an unowned row is foreign — which is what the `anyOf` branch and the read side already did.
70
+
71
+ - **Documentation corrections.** `AGENTS_BACKEND.md` stated that programmatic `sqlApi.*` calls
72
+ are uncapped. The two *configurable* caps do belong to the routes (`maxItemsPerPage`,
73
+ `maxBulkItems` — `sqlApi.search()` takes `maxRows` for the first), but the complexity caps
74
+ (100 `conditions`, 20 `orderBy` parts) and the `paginator` integer guard live in the engine and
75
+ have always applied to `sqlApi.search()` too. `AGENTS_BACKEND.md` and `README.md` now say which
76
+ cap sits where, and the fixed caps are documented at all.
77
+
78
+ ## [0.2.2]
11
79
 
12
80
  ### Added
13
81
 
@@ -50,6 +118,7 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
50
118
  `Table*.ts` the generator never overwrites. The CLI also prints the columns it found, per
51
119
  table, since a generated source file is not always read. See
52
120
  [ADR 0015](./docs/adr/0015-non-writable-columns.md).
121
+
53
122
  - **`writeExclude` on `defineTable`** — the write-side counterpart of `readExclude`, for the
54
123
  non-writable columns introspection cannot know about (one a trigger owns, one a migration is
55
124
  about to drop). Reads are untouched: the field stays projected, filterable and orderable. It
@@ -57,6 +126,7 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
57
126
  (ADR 0005): a hook cannot put back a value the database will refuse. `defineTable` rejects an
58
127
  unknown field, the primary key, and a field that is also `readExclude`d. It is static and
59
128
  applies to everyone — a rule that depends on *who* is asking is still product logic (ADR 0004).
129
+
60
130
  - **The agent manifest marks non-writable fields** `readOnly`, alongside the existing
61
131
  `writeOnly` for `readExclude`.
62
132
 
@@ -74,6 +144,70 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
74
144
  column a relation joins on cannot break the correlation. `SearchParams`/`GetParams` now carry
75
145
  the optional `request`, which `sqlApi.search()`/`sqlApi.get()` forward when given one.
76
146
 
147
+ ### Changed
148
+
149
+ - **The update body requires the primary key**, which identifies the row it updates
150
+ (`PUT /rest/:table` carries it in `main`). The generated PK field is `Optional` — it is
151
+ absent on insert — and that modifier reached the update body unchanged, so a request omitting
152
+ `main.id` built its `WHERE` on `undefined` instead of being rejected. The code comment said
153
+ "PK required" all along.
154
+
155
+ - **A secondary now runs the child table's `beforeInsert`.** `processSecondaries` already applied
156
+ the child's `excludeFromCreation` and its `tenantScope` but skipped its `beforeInsert`, so a
157
+ table whose hook *transformed* a value — encrypting a column, normalising a unit — stored the
158
+ raw client value whenever its rows arrived as another table's children, and the correct one
159
+ through its own route. Nothing failed at write time; the row read back as garbage later. The
160
+ hook runs after the exclusion strip and before the engine's FK auto-fill, which stays
161
+ authoritative, and receives the transaction connection. The child's `validate` and
162
+ `afterInsert` deliberately still do not run — see
163
+ [ADR 0014](./docs/adr/0014-what-follows-a-write-join.md). **Breaking** for a `beforeInsert`
164
+ written assuming it only ever ran on its own table's route.
165
+
166
+ - **The get response schema honours `readExclude`**, like the search response already did; it
167
+ previously advertised fields the engine never selects. It now lives in `lib/schema/get.ts`
168
+ (`GetTableResponse`) instead of inline in the route, which is how it had drifted.
169
+ `schemaOverrides` stays where it was — write bodies only: an override rules what the API
170
+ accepts from now on, while the rows already stored predate it, so narrowing the response
171
+ would promise something no one can retroactively make true.
172
+
173
+ ### Fixed
174
+
175
+ - **A `tenantScope` naming a schema field instead of a column now fails at startup.**
176
+ `tenantScope` names real DB columns — the scope is applied to the SQL, after the payload has
177
+ been converted, and `through.foreignField` belongs to a parent table that may have no config of
178
+ its own. Nothing checked it, so writing `organizationId` where the column is `organization_id`
179
+ passed `defineTable` and surfaced as `column "organizationId" does not exist` on every request
180
+ to the table: an opaque `500`, failing closed but saying nothing. All four positions are now
181
+ checked (`column`, each `anyOf` entry, `through.localField`, and `through.foreignField` against
182
+ the parent schema). The check is narrow by design — it fires only when the name *is* a schema
183
+ field whose column differs, so a column absent from the schema raises nothing and a camelCase
184
+ database, where `col()` is the identity, raises nothing either.
185
+
186
+ - **The `no-static-optional-peer` gate had never been able to fire.** `includeOnly: '^src/'` in
187
+ `.dependency-cruiser.mjs` dropped every npm edge from the dependency graph, so the rule meant to
188
+ stop a static import of `pg` / `mysql2` / `@fastify/*` — which breaks installs that use the other
189
+ dialect — matched nothing, ever. The graph now keeps npm dependencies as leaves, and the rule is
190
+ qualified to what its name says: static, value-level imports only, since a type-only import is
191
+ erased at compile time and a dynamic one is what the rule asks for. Both gates were verified by
192
+ introducing a violation and watching them fail.
193
+
194
+ - **`defineTable` never validated `primary`.** A primary key naming a field the schema does not
195
+ have passed startup untouched and became a raw SQL error on the first request — a `500`. It
196
+ now throws at `defineTable` time, naming the field and the operations that need it. The one
197
+ exception is exact: a table exposing only `search` and declaring its own `defaultOrder` never
198
+ reads `primary`, which is the shape generated for a view whose key could not be inferred.
199
+
200
+ - **PostgreSQL identity columns made a table impossible to insert into.**
201
+ `GENERATED ... AS IDENTITY` reports no `column_default`, which is what the generator read to
202
+ decide that the database fills a column in — so the primary key came out **mandatory** in the
203
+ write body while PostgreSQL rejected any explicit value for it (`Column "id" is an identity
204
+ column defined as GENERATED ALWAYS`). Identity columns are now flagged like MySQL's
205
+ `AUTO_INCREMENT`: the field is Optional and the CLI lists it under `excludeFromCreation`.
206
+
207
+ ## [0.2.1]
208
+
209
+ ### Added
210
+
77
211
  - **`tenantScope` over several owner columns — `{ anyOf: [...] }`.** A row that belongs to two
78
212
  parties and is visible to either — a message (`sender_id` / `recipient_id`), a transfer
79
213
  (`from_account_id` / `to_account_id`), a shift swap — could not be expressed before, leaving
@@ -91,6 +225,7 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
91
225
  byte-identical SQL. The three forms do not mix — `defineTable` throws at startup on `anyOf`
92
226
  combined with `column` or `through`, on an empty or malformed `anyOf`, and on a scope
93
227
  declaring no owner column at all.
228
+
94
229
  - `QueryClient.bulkInsertOrUpdate` takes an optional trailing `immutableCols`: columns written
95
230
  on insert but left untouched when the statement lands on an existing row. Additive.
96
231
 
@@ -111,6 +246,7 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
111
246
  their own scope emit unchanged SQL. Migration — including the `getTenantId` body that now needs
112
247
  to tolerate an unauthenticated request — in
113
248
  [BREAKING_CHANGES.md](./BREAKING_CHANGES.md#breaking-change--the-tenant-follows-the-request-not-the-table-it-addresses).
249
+
114
250
  - **Database errors no longer reach clients verbatim.** Any error escaping an auto-generated
115
251
  route that the plugin did not raise itself now answers `{"statusCode":500,"error":"Internal
116
252
  Server Error","message":"Internal Server Error"}` instead of the driver message, which names
@@ -131,6 +267,93 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
131
267
  controlled) rather than the response body (not, and these routes are open by default). See
132
268
  [ADR 0013](./docs/adr/0013-sanitized-db-errors.md).
133
269
 
270
+ ## [0.2.0]
271
+
272
+ ### Added
273
+
274
+ - `QueryClient.update` and `QueryClient.delete` take an optional trailing `extraCondition`
275
+ (a `ConditionBuilder`) merged into the `WHERE` with its values bound in order — how the tenant
276
+ row guard reaches a write. Additive.
277
+
278
+ ### Changed
279
+
280
+ - **BREAKING: `conditions` and `orderBy` are capped.** `maxItemsPerPage` bounds the rows a
281
+ search returns, not the work done to find them: every dotted `conditions` entry and every
282
+ 3-part `orderBy` token becomes its own correlated subquery, so an uncapped list turned one
283
+ request into arbitrarily many. `conditions` now takes at most **100** entries per section — the
284
+ top level and each join family — and `orderBy` at most **20** comma-separated parts; over
285
+ either, the answer is a **400**. The request schemas carry the same numbers (`maxItems`, plus a
286
+ 1024-character cap on the `orderBy` string), so Swagger advertises exactly what the engine
287
+ enforces. Both are exported as `MAX_CONDITIONS` and `MAX_ORDER_BY_PARTS`, and both are checked
288
+ **in the engine**, which is what covers `sqlApi.search()` — no schema validates a programmatic
289
+ caller. See [BREAKING_CHANGES.md](./BREAKING_CHANGES.md#breaking-change--request-limits-condition-arity-and-tenant-scope-inside-subqueries).
290
+
291
+ - **BREAKING: `paginator` is validated at runtime.** `page` and `itemsPerPage` are rendered
292
+ straight into `LIMIT`/`OFFSET`, and `Paginator` is a compile-time type only: nothing stopped a
293
+ string, a fraction, a zero or a `NaN` — or a SQL fragment, on the documented path where a
294
+ consumer hands `sqlApi.search()` an unvalidated querystring. Both must now be integers `>= 1`,
295
+ checked before any query runs; anything else is a **400**. A numeric *string* (`itemsPerPage:
296
+ '10'`) is rejected too, so a querystring must be parsed before it is forwarded.
297
+
298
+ - **BREAKING: a malformed condition `params` answers 400 instead of 500.** The method was
299
+ dispatched with whatever `params` carried, so a missing or wrongly shaped one threw a
300
+ `TypeError` inside the builder — which the error handler could only report as an
301
+ `Internal Server Error`. Arity is now checked once, at dispatch, for every call site:
302
+ `params` must be an array, the single-value methods need one entry, `isBetween`/`isNotBetween`
303
+ need two, and `isIn`/`isNotIn` need an array as their first entry. `isNull`/`isNotNull` still
304
+ take none, and an explicit `undefined` to `isIn` stays the documented no-op.
305
+
306
+ - **BREAKING: inherited `Object.prototype` keys are no longer field names.** `key in map` and
307
+ `map[key]` walk the prototype chain, so `constructor`, `toString`, `valueOf`, `hasOwnProperty`
308
+ and `__proto__` satisfied every "is this a known field" check and reached the SQL builders,
309
+ where they rendered as `undefined` or as a column that does not exist. Every allowlist and every
310
+ field lookup in the search engine now tests own properties, so an inherited name is simply
311
+ unknown — a **400** in `filters`, condition fields, `orderBy` and `selectComputed`.
312
+
313
+ - **BREAKING: `tenantScope` reaches the subqueries and the child writes of a request.** The
314
+ scope was applied to the main query and to the tables addressed directly, and nowhere else, so
315
+ the same request could still read and write other tenants' rows through its own joins:
316
+ **secondaries** were written without the child table's scope, **`joinLeft`** embedded the parent
317
+ unscoped, and the **`joinGroup` aggregation subqueries** — with the 3-part aggregation
318
+ `orderBy` built on them — counted and summed every tenant's child rows, disagreeing with the
319
+ scoped `result.joinGroup` for the same alias and leaking the existence of the rest. All four
320
+ now carry the scope of the table they touch; on `joinLeft` the predicate lands in the `ON`
321
+ clause, so `LEFT` semantics survive. Aggregates that used to count across tenants return
322
+ smaller numbers, and an ordering built on them changes with it.
323
+
324
+ ### Internal
325
+
326
+ - The quality gates became executable and zero-tolerance (eslint, jscpd, knip,
327
+ dependency-cruiser, lefthook, CI), `search.ts` was split into seven layered modules with an
328
+ acyclic graph, and `docs/INDEX.md` plus `npm run find-similar` were added as the anti-duplication
329
+ surface. No runtime behaviour changes with any of it. See
330
+ [ADR 0012](./docs/adr/0012-anti-duplication-tooling.md).
331
+
332
+ ## [0.1.14]
333
+
334
+ ### Added
335
+
336
+ - **`fields` allowlist on `buildRelation`** — restricts which of the target table's fields are
337
+ reachable through one relation, without hiding them from the table's own routes the way
338
+ `readExclude` would. The relation is declared against a schema narrowed to the list, so
339
+ `selection`, `filters`, `conditions`, `orderBy`, aggregations and the generated
340
+ request/response schemas all reject anything outside it with `400 Unknown field`; the default
341
+ `'*'` selection is spelled out into the allowed columns instead of emitting a real `SELECT *`;
342
+ and computed fields on the target resolve against the narrowed schema, so one reading an
343
+ excluded column is a 400 rather than a way around the list. Declaration-time checks: the list
344
+ must include the join field, and is rejected on `allowedWriteJoins` (write paths resolve
345
+ `upsertMap` by schema identity and must write every column the caller sent). Fail-closed by
346
+ design — see [ADR 0011](./docs/adr/0011-join-fields-allowlist.md) for allowlist vs blocklist.
347
+
348
+ - **[ADR 0010](./docs/adr/0010-joins-do-not-run-route-guards.md)** — a declared join is a read
349
+ grant: `onRequests` and `operations` are route-level and do not follow a join, so
350
+ `allowedReadJoins` grants read access to the target table under the *host* table's
351
+ authorization. Documents what does cross a join (`readExclude`, `tenantScope`, the relation's
352
+ schema), the trimmed-schema + explicit-`selection` pattern for column-level narrowing, and
353
+ the rejected alternatives (`canBeJoined` hook, `selection` as a ceiling).
354
+
355
+ ### Changed
356
+
134
357
  - **Unknown `filters` keys are now rejected with `400 Unknown filter field: <key>`** instead
135
358
  of being dropped in silence — on the main table and on every join family. The engine only
136
359
  ever visited keys matching a schema field, an `extraFilters` entry or a computed field, so a
@@ -140,64 +363,15 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
140
363
  covers `sqlApi.search()` as well as the HTTP routes. `undefined` values still mean "filter
141
364
  not supplied" and are ignored; an explicit `null` still filters by `IS NULL`. See
142
365
  [BREAKING_CHANGES.md](./BREAKING_CHANGES.md#breaking-change--unknown-filter-keys-are-rejected).
366
+
143
367
  - **`joinLeft.<alias>.filters` no longer advertises the parent's `extraFilters`.** They were
144
368
  present in the generated body schema and in Swagger, but `buildLeftJoinClauses` never runs
145
369
  the target's `extendedCondition` (its column references cannot be qualified with the
146
370
  `LEFT JOIN` alias), so the filter applied nothing. They are now rejected with a `400` naming
147
371
  the reason and pointing at `joinMustExist` on the same relation.
148
372
 
149
- - **The update body requires the primary key**, which identifies the row it updates
150
- (`PUT /rest/:table` carries it in `main`). The generated PK field is `Optional` — it is
151
- absent on insert — and that modifier reached the update body unchanged, so a request omitting
152
- `main.id` built its `WHERE` on `undefined` instead of being rejected. The code comment said
153
- "PK required" all along.
154
- - **A secondary now runs the child table's `beforeInsert`.** `processSecondaries` already applied
155
- the child's `excludeFromCreation` and its `tenantScope` but skipped its `beforeInsert`, so a
156
- table whose hook *transformed* a value — encrypting a column, normalising a unit — stored the
157
- raw client value whenever its rows arrived as another table's children, and the correct one
158
- through its own route. Nothing failed at write time; the row read back as garbage later. The
159
- hook runs after the exclusion strip and before the engine's FK auto-fill, which stays
160
- authoritative, and receives the transaction connection. The child's `validate` and
161
- `afterInsert` deliberately still do not run — see
162
- [ADR 0014](./docs/adr/0014-what-follows-a-write-join.md). **Breaking** for a `beforeInsert`
163
- written assuming it only ever ran on its own table's route.
164
- - **The get response schema honours `readExclude`**, like the search response already did; it
165
- previously advertised fields the engine never selects. It now lives in `lib/schema/get.ts`
166
- (`GetTableResponse`) instead of inline in the route, which is how it had drifted.
167
- `schemaOverrides` stays where it was — write bodies only: an override rules what the API
168
- accepts from now on, while the rows already stored predate it, so narrowing the response
169
- would promise something no one can retroactively make true.
170
-
171
373
  ### Fixed
172
374
 
173
- - **A `tenantScope` naming a schema field instead of a column now fails at startup.**
174
- `tenantScope` names real DB columns — the scope is applied to the SQL, after the payload has
175
- been converted, and `through.foreignField` belongs to a parent table that may have no config of
176
- its own. Nothing checked it, so writing `organizationId` where the column is `organization_id`
177
- passed `defineTable` and surfaced as `column "organizationId" does not exist` on every request
178
- to the table: an opaque `500`, failing closed but saying nothing. All four positions are now
179
- checked (`column`, each `anyOf` entry, `through.localField`, and `through.foreignField` against
180
- the parent schema). The check is narrow by design — it fires only when the name *is* a schema
181
- field whose column differs, so a column absent from the schema raises nothing and a camelCase
182
- database, where `col()` is the identity, raises nothing either.
183
- - **The `no-static-optional-peer` gate had never been able to fire.** `includeOnly: '^src/'` in
184
- `.dependency-cruiser.mjs` dropped every npm edge from the dependency graph, so the rule meant to
185
- stop a static import of `pg` / `mysql2` / `@fastify/*` — which breaks installs that use the other
186
- dialect — matched nothing, ever. The graph now keeps npm dependencies as leaves, and the rule is
187
- qualified to what its name says: static, value-level imports only, since a type-only import is
188
- erased at compile time and a dynamic one is what the rule asks for. Both gates were verified by
189
- introducing a violation and watching them fail.
190
- - **`defineTable` never validated `primary`.** A primary key naming a field the schema does not
191
- have passed startup untouched and became a raw SQL error on the first request — a `500`. It
192
- now throws at `defineTable` time, naming the field and the operations that need it. The one
193
- exception is exact: a table exposing only `search` and declaring its own `defaultOrder` never
194
- reads `primary`, which is the shape generated for a view whose key could not be inferred.
195
- - **PostgreSQL identity columns made a table impossible to insert into.**
196
- `GENERATED ... AS IDENTITY` reports no `column_default`, which is what the generator read to
197
- decide that the database fills a column in — so the primary key came out **mandatory** in the
198
- write body while PostgreSQL rejected any explicit value for it (`Column "id" is an identity
199
- column defined as GENERATED ALWAYS`). Identity columns are now flagged like MySQL's
200
- `AUTO_INCREMENT`: the field is Optional and the CLI lists it under `excludeFromCreation`.
201
375
  - **Join validation no longer depends on the main result set.** `joinMultiple` / `joinLeft` /
202
376
  `joinGroup` skip their side query entirely when the main query matched no rows, so everything
203
377
  validated inside it was skipped with it — the same request answered 400 or 200 depending on
@@ -206,25 +380,9 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
206
380
  filters are now validated up front, and the selection and aggregation lists are resolved
207
381
  before the empty-result bail-out.
208
382
 
209
- ### Added
383
+ ## [0.1.13]
210
384
 
211
- - **`fields` allowlist on `buildRelation`** — restricts which of the target table's fields are
212
- reachable through one relation, without hiding them from the table's own routes the way
213
- `readExclude` would. The relation is declared against a schema narrowed to the list, so
214
- `selection`, `filters`, `conditions`, `orderBy`, aggregations and the generated
215
- request/response schemas all reject anything outside it with `400 Unknown field`; the default
216
- `'*'` selection is spelled out into the allowed columns instead of emitting a real `SELECT *`;
217
- and computed fields on the target resolve against the narrowed schema, so one reading an
218
- excluded column is a 400 rather than a way around the list. Declaration-time checks: the list
219
- must include the join field, and is rejected on `allowedWriteJoins` (write paths resolve
220
- `upsertMap` by schema identity and must write every column the caller sent). Fail-closed by
221
- design — see [ADR 0011](./docs/adr/0011-join-fields-allowlist.md) for allowlist vs blocklist.
222
- - **[ADR 0010](./docs/adr/0010-joins-do-not-run-route-guards.md)** — a declared join is a read
223
- grant: `onRequests` and `operations` are route-level and do not follow a join, so
224
- `allowedReadJoins` grants read access to the target table under the *host* table's
225
- authorization. Documents what does cross a join (`readExclude`, `tenantScope`, the relation's
226
- schema), the trimmed-schema + explicit-`selection` pattern for column-level narrowing, and
227
- the rejected alternatives (`canBeJoined` hook, `selection` as a ceiling).
385
+ ### Added
228
386
 
229
387
  - **`excludeTables` config option** — blacklist for `sqlapi-generate-schema`. Tables
230
388
  listed in `excludeTables` in `sqlapi.config.ts` (exact names or `*` globs, e.g.