fastify-auto-sqlapi 0.5.1 → 1.0.0

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.
Files changed (212) hide show
  1. package/AGENTS.md +5 -5
  2. package/AGENTS_BACKEND.md +166 -130
  3. package/AGENTS_FRONTEND.md +60 -38
  4. package/BREAKING_CHANGES.md +9 -893
  5. package/CHANGELOG.md +246 -15
  6. package/README.md +71 -45
  7. package/dist/bin/generate-schema.js +12 -12
  8. package/dist/bin/generate-tables.js +1 -2
  9. package/dist/bin/utils.d.ts +2 -6
  10. package/dist/bin/utils.d.ts.map +1 -1
  11. package/dist/bin/utils.js +13 -33
  12. package/dist/index.d.ts +13 -33
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +6 -37
  15. package/dist/lib/adapters/mysql-adapter.d.ts +5 -2
  16. package/dist/lib/adapters/mysql-adapter.d.ts.map +1 -1
  17. package/dist/lib/adapters/pg-adapter.d.ts +5 -2
  18. package/dist/lib/adapters/pg-adapter.d.ts.map +1 -1
  19. package/dist/lib/agent/manifest.d.ts +27 -10
  20. package/dist/lib/agent/manifest.d.ts.map +1 -1
  21. package/dist/lib/agent/manifest.js +43 -26
  22. package/dist/lib/agent/tool-schemas.d.ts +5 -4
  23. package/dist/lib/agent/tool-schemas.d.ts.map +1 -1
  24. package/dist/lib/agent/tool-schemas.js +5 -5
  25. package/dist/lib/cli/config.d.ts +37 -2
  26. package/dist/lib/cli/config.d.ts.map +1 -1
  27. package/dist/lib/cli/config.js +106 -16
  28. package/dist/lib/cli/mysql-introspect.d.ts +6 -1
  29. package/dist/lib/cli/mysql-introspect.d.ts.map +1 -1
  30. package/dist/lib/cli/mysql-introspect.js +15 -15
  31. package/dist/lib/cli/pg-introspect.d.ts +0 -1
  32. package/dist/lib/cli/pg-introspect.d.ts.map +1 -1
  33. package/dist/lib/cli/pg-introspect.js +2 -13
  34. package/dist/lib/cli/tables-codegen.d.ts +0 -4
  35. package/dist/lib/cli/tables-codegen.d.ts.map +1 -1
  36. package/dist/lib/cli/tables-codegen.js +24 -87
  37. package/dist/lib/db.d.ts +42 -17
  38. package/dist/lib/db.d.ts.map +1 -1
  39. package/dist/lib/db.js +114 -40
  40. package/dist/lib/dialect.d.ts +5 -0
  41. package/dist/lib/dialect.d.ts.map +1 -1
  42. package/dist/lib/dialect.js +3 -0
  43. package/dist/lib/engine/bulk/bulk-delete.d.ts +5 -0
  44. package/dist/lib/engine/bulk/bulk-delete.d.ts.map +1 -1
  45. package/dist/lib/engine/bulk/bulk-delete.js +66 -30
  46. package/dist/lib/engine/bulk/bulk-upsert.d.ts.map +1 -1
  47. package/dist/lib/engine/bulk/bulk-upsert.js +45 -26
  48. package/dist/lib/engine/read-hooks.d.ts +3 -3
  49. package/dist/lib/engine/read-hooks.d.ts.map +1 -1
  50. package/dist/lib/engine/read-hooks.js +3 -3
  51. package/dist/lib/engine/rest/delete.d.ts.map +1 -1
  52. package/dist/lib/engine/rest/delete.js +12 -18
  53. package/dist/lib/engine/rest/get.js +8 -8
  54. package/dist/lib/engine/rest/insert.d.ts.map +1 -1
  55. package/dist/lib/engine/rest/insert.js +31 -18
  56. package/dist/lib/engine/rest/update.d.ts.map +1 -1
  57. package/dist/lib/engine/rest/update.js +34 -17
  58. package/dist/lib/engine/search/aggregations.d.ts +3 -3
  59. package/dist/lib/engine/search/aggregations.d.ts.map +1 -1
  60. package/dist/lib/engine/search/aggregations.js +20 -16
  61. package/dist/lib/engine/search/conditions.d.ts +9 -5
  62. package/dist/lib/engine/search/conditions.d.ts.map +1 -1
  63. package/dist/lib/engine/search/conditions.js +31 -20
  64. package/dist/lib/engine/search/fields.d.ts +44 -11
  65. package/dist/lib/engine/search/fields.d.ts.map +1 -1
  66. package/dist/lib/engine/search/fields.js +93 -27
  67. package/dist/lib/engine/search/join-families.d.ts +7 -7
  68. package/dist/lib/engine/search/join-families.d.ts.map +1 -1
  69. package/dist/lib/engine/search/join-families.js +84 -75
  70. package/dist/lib/engine/search/joins.d.ts +15 -7
  71. package/dist/lib/engine/search/joins.d.ts.map +1 -1
  72. package/dist/lib/engine/search/joins.js +23 -13
  73. package/dist/lib/engine/search/order-by.d.ts +11 -12
  74. package/dist/lib/engine/search/order-by.d.ts.map +1 -1
  75. package/dist/lib/engine/search/order-by.js +24 -28
  76. package/dist/lib/engine/search/search.d.ts.map +1 -1
  77. package/dist/lib/engine/search/search.js +111 -64
  78. package/dist/lib/engine/validate.d.ts +21 -3
  79. package/dist/lib/engine/validate.d.ts.map +1 -1
  80. package/dist/lib/engine/validate.js +106 -16
  81. package/dist/lib/engine/write-helpers.d.ts +69 -16
  82. package/dist/lib/engine/write-helpers.d.ts.map +1 -1
  83. package/dist/lib/engine/write-helpers.js +257 -105
  84. package/dist/lib/errors.d.ts +42 -10
  85. package/dist/lib/errors.d.ts.map +1 -1
  86. package/dist/lib/errors.js +69 -15
  87. package/dist/lib/nullable.d.ts +3 -2
  88. package/dist/lib/nullable.d.ts.map +1 -1
  89. package/dist/lib/nullable.js +14 -5
  90. package/dist/lib/read-access.d.ts +25 -16
  91. package/dist/lib/read-access.d.ts.map +1 -1
  92. package/dist/lib/read-access.js +33 -17
  93. package/dist/lib/schema/bulk-delete.d.ts +13 -7
  94. package/dist/lib/schema/bulk-delete.d.ts.map +1 -1
  95. package/dist/lib/schema/bulk-delete.js +18 -8
  96. package/dist/lib/schema/bulk-upsert.d.ts +3 -3
  97. package/dist/lib/schema/bulk-upsert.d.ts.map +1 -1
  98. package/dist/lib/schema/bulk-upsert.js +9 -14
  99. package/dist/lib/schema/get.d.ts +1 -1
  100. package/dist/lib/schema/get.d.ts.map +1 -1
  101. package/dist/lib/schema/get.js +2 -2
  102. package/dist/lib/schema/helpers.d.ts +41 -23
  103. package/dist/lib/schema/helpers.d.ts.map +1 -1
  104. package/dist/lib/schema/helpers.js +80 -33
  105. package/dist/lib/schema/insert.d.ts +1 -1
  106. package/dist/lib/schema/insert.d.ts.map +1 -1
  107. package/dist/lib/schema/insert.js +12 -20
  108. package/dist/lib/schema/search.d.ts +2 -13
  109. package/dist/lib/schema/search.d.ts.map +1 -1
  110. package/dist/lib/schema/search.js +64 -61
  111. package/dist/lib/schema/update.d.ts +1 -1
  112. package/dist/lib/schema/update.d.ts.map +1 -1
  113. package/dist/lib/schema/update.js +6 -13
  114. package/dist/lib/sql-api.d.ts +47 -25
  115. package/dist/lib/sql-api.d.ts.map +1 -1
  116. package/dist/lib/sql-api.js +70 -27
  117. package/dist/lib/table-helpers.d.ts +92 -14
  118. package/dist/lib/table-helpers.d.ts.map +1 -1
  119. package/dist/lib/table-helpers.js +367 -128
  120. package/dist/lib/tenant.d.ts +16 -22
  121. package/dist/lib/tenant.d.ts.map +1 -1
  122. package/dist/lib/tenant.js +38 -54
  123. package/dist/lib/write-access.d.ts +15 -9
  124. package/dist/lib/write-access.d.ts.map +1 -1
  125. package/dist/lib/write-access.js +12 -4
  126. package/dist/routes/auto/agent-manifest.routes.d.ts +1 -1
  127. package/dist/routes/auto/agent-manifest.routes.js +2 -2
  128. package/dist/routes/auto/bulk-delete.routes.d.ts +2 -2
  129. package/dist/routes/auto/bulk-delete.routes.d.ts.map +1 -1
  130. package/dist/routes/auto/bulk-delete.routes.js +1 -7
  131. package/dist/routes/auto/bulk-upsert.routes.d.ts +2 -2
  132. package/dist/routes/auto/bulk-upsert.routes.d.ts.map +1 -1
  133. package/dist/routes/auto/bulk-upsert.routes.js +2 -2
  134. package/dist/routes/auto/delete.routes.d.ts +2 -2
  135. package/dist/routes/auto/delete.routes.d.ts.map +1 -1
  136. package/dist/routes/auto/delete.routes.js +8 -10
  137. package/dist/routes/auto/get.routes.d.ts +2 -2
  138. package/dist/routes/auto/get.routes.d.ts.map +1 -1
  139. package/dist/routes/auto/get.routes.js +5 -5
  140. package/dist/routes/auto/insert.routes.d.ts +2 -2
  141. package/dist/routes/auto/insert.routes.d.ts.map +1 -1
  142. package/dist/routes/auto/insert.routes.js +1 -7
  143. package/dist/routes/auto/plugin.d.ts +6 -0
  144. package/dist/routes/auto/plugin.d.ts.map +1 -1
  145. package/dist/routes/auto/plugin.js +60 -36
  146. package/dist/routes/auto/route-helpers.d.ts +26 -12
  147. package/dist/routes/auto/route-helpers.d.ts.map +1 -1
  148. package/dist/routes/auto/route-helpers.js +50 -19
  149. package/dist/routes/auto/search.routes.d.ts +2 -2
  150. package/dist/routes/auto/search.routes.d.ts.map +1 -1
  151. package/dist/routes/auto/search.routes.js +36 -24
  152. package/dist/routes/auto/update.routes.d.ts +2 -2
  153. package/dist/routes/auto/update.routes.d.ts.map +1 -1
  154. package/dist/routes/auto/update.routes.js +1 -8
  155. package/dist/routes/sql-api-decorator.d.ts +30 -12
  156. package/dist/routes/sql-api-decorator.d.ts.map +1 -1
  157. package/dist/routes/sql-api-decorator.js +44 -27
  158. package/dist/types/cli.d.ts +17 -6
  159. package/dist/types/cli.d.ts.map +1 -1
  160. package/dist/types/computed.d.ts +1 -1
  161. package/dist/types/computed.d.ts.map +1 -1
  162. package/dist/types/conditions.d.ts +3 -1
  163. package/dist/types/conditions.d.ts.map +1 -1
  164. package/dist/types/db.d.ts +44 -4
  165. package/dist/types/db.d.ts.map +1 -1
  166. package/dist/types/dml.d.ts +26 -17
  167. package/dist/types/dml.d.ts.map +1 -1
  168. package/dist/types/hooks.d.ts +62 -0
  169. package/dist/types/hooks.d.ts.map +1 -0
  170. package/dist/types/hooks.js +1 -0
  171. package/dist/types/join.d.ts +25 -8
  172. package/dist/types/join.d.ts.map +1 -1
  173. package/dist/types/plugin.d.ts +58 -19
  174. package/dist/types/plugin.d.ts.map +1 -1
  175. package/dist/types/plugin.js +2 -0
  176. package/dist/types/request.d.ts +2 -2
  177. package/dist/types/schema.d.ts +3 -3
  178. package/dist/types/schema.d.ts.map +1 -1
  179. package/dist/types/search.d.ts +28 -13
  180. package/dist/types/search.d.ts.map +1 -1
  181. package/dist/types/search.js +2 -1
  182. package/dist/types/table.d.ts +109 -94
  183. package/dist/types/table.d.ts.map +1 -1
  184. package/dist/types/table.js +7 -2
  185. package/dist/types/tenant.d.ts +28 -2
  186. package/dist/types/tenant.d.ts.map +1 -1
  187. package/dist/types/tenant.js +7 -1
  188. package/dist/types/validation.d.ts +69 -13
  189. package/dist/types/validation.d.ts.map +1 -1
  190. package/dist/types.d.ts +1 -0
  191. package/dist/types.d.ts.map +1 -1
  192. package/dist/types.js +1 -0
  193. package/docs/GLOSSARY.md +11 -5
  194. package/docs/adr/0003-bulk-not-transactional.md +2 -1
  195. package/docs/adr/0006-raw-db-errors.md +2 -1
  196. package/docs/adr/0013-sanitized-db-errors.md +2 -1
  197. package/docs/adr/0014-what-follows-a-write-join.md +1 -1
  198. package/docs/adr/0022-stay-on-0x-while-every-consumer-is-first-party.md +88 -0
  199. package/docs/adr/0023-a-write-runs-in-a-transaction-or-not-at-all.md +60 -0
  200. package/docs/adr/0024-the-consumer-maps-driver-codes-through-dberrors.md +78 -0
  201. package/docs/adr/0025-validatebulk-replaces-validate-in-a-bulk-upsert.md +46 -0
  202. package/docs/adr/0026-update-carries-its-primary-key-in-main.md +43 -0
  203. package/docs/adr/0027-the-1-0-contract.md +74 -0
  204. package/docs/adr/0028-select-star-is-the-default-projection.md +64 -0
  205. package/docs/adr/0029-a-write-names-its-child-rows-by-action.md +96 -0
  206. package/docs/adr/0030-a-child-row-is-validated-by-its-own-table.md +117 -0
  207. package/docs/adr/0031-an-insert-excluded-upsert-key-addresses-an-existing-row.md +58 -0
  208. package/docs/adr/0032-tenant-scope-names-schema-fields.md +56 -0
  209. package/docs/adr/README.md +12 -1
  210. package/docs/upgrading/0.x.md +906 -0
  211. package/docs/upgrading/1.0.md +628 -0
  212. package/package.json +4 -5
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAMzF;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQ3H;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,gBAAgB,GACvB,IAAI,CAMN;AAoCD,kFAAkF;AAClF,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAGnD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,MAAM,GAAG,SAAS,GAC5B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASzB;AAED,KAAK,iBAAiB,GAAG,CACvB,IAAI,EAAE,cAAc,EACpB,aAAa,EAAE,MAAM,GAAG,SAAS,KAC9B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAwB7B;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE;IAAE,aAAa,EAAE,OAAO,CAAC;IAAC,eAAe,EAAE,iBAAiB,CAAC;IAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,GAC7F,IAAI,CA8BN;AAED,qGAAqG;AACrG,eAAO,MAAM,mBAAmB,EAAE,iBACmB,CAAC;AAEtD,uDAAuD;AACvD,eAAO,MAAM,uBAAuB,EAAE,iBACG,CAAC;AAE1C,4DAA4D;AAC5D,wBAAgB,cAAc,CAC5B,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,MAAM,GAAG,SAAS,GAC5B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAOzB"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAc,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvE,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,QAAQ,EAAkB,MAAM,gBAAgB,CAAC;AAM9F;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,SAAS,EAAE,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQ3I;AAED,4FAA4F;AAC5F,eAAO,MAAM,iBAAiB,oEAAkE,CAAC;AAEjG;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,OAAO,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,GAAG,SAAS,EAClC,MAAM,EAAE,gBAAgB,GACvB,IAAI,CAMN;AAkCD,kFAAkF;AAClF,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAGnD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,GAAG,SAAS,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASzB;AAuCD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,EACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE;IAAE,UAAU,EAAE,OAAO,CAAA;CAAE,GAC/B,IAAI,CAcN;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,EACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE;IAAE,SAAS,EAAE,KAAK,GAAG,UAAU,CAAA;CAAE,GACzC,IAAI,CASN;AAaD,4DAA4D;AAC5D,wBAAgB,cAAc,CAC5B,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,WAAW,GAAG,SAAS,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAOzB"}
@@ -1,8 +1,9 @@
1
- import { Type, OptionalKind } from '@sinclair/typebox';
1
+ import { Type, IsOptional } from 'typebox';
2
+ import { primaryAsString } from '../../types.js';
2
3
  import { readableFieldNames } from '../read-access.js';
3
- import { writableFields } from '../write-access.js';
4
+ import { writableFields, insertExcludedFields } from '../write-access.js';
4
5
  import { tenantOwnColumns } from '../tenant.js';
5
- import { findSecondaryTableConf } from '../engine/write-helpers.js';
6
+ import { resolveJoinTableConf, resolvedTenantScope } from '../table-helpers.js';
6
7
  /**
7
8
  * Build a PK-only schema for response shapes (single or composite PK).
8
9
  * Falls back to `fallback` field if tableConf is undefined.
@@ -16,14 +17,32 @@ export function pkSchema(tableConf, schema, fallback) {
16
17
  }
17
18
  return result;
18
19
  }
20
+ /** `action` of a write response: what the statement did to the main row (`WriteAction`). */
21
+ export const WriteActionSchema = Type.Union([Type.Literal('inserted'), Type.Literal('updated')]);
22
+ /**
23
+ * Path params of the single-record routes, `GET|DELETE /rest/:table/:id`.
24
+ *
25
+ * `:id` is validated against the primary key field's own type rather than as free text: a
26
+ * path parameter is a string on the wire, and Ajv's coercion turns `"7"` into the number an
27
+ * integer PK expects while `"abc"` becomes a structured 400 on `id` instead of a driver
28
+ * error the route can only report as a 500. The PK is Optional in a generated Schema when the
29
+ * database fills it, so the modifier is stripped here: the path always carries the value.
30
+ *
31
+ * The parameter is named `id` whatever the PK field is called; the response names the real
32
+ * field (see `pkSchema`).
33
+ */
34
+ export function pkParams(tableConf) {
35
+ const pkField = primaryAsString(tableConf.primary);
36
+ return Type.Object({ id: asRequired(tableConf.schema.fields[pkField] ?? Type.String()) });
37
+ }
19
38
  /**
20
39
  * Make the tenant column(s) of a scoped table Optional in a write body. The engine injects the
21
40
  * value for a single-tenant caller and validates the one a multi-tenant or `anyOf` caller sends,
22
41
  * so the body must offer the field without demanding it — the same reason a secondary's FK is
23
- * Optional. `tenantScope` names real DB columns; the schema field is found through `col()`.
42
+ * Optional. The scope is resolved to its columns, and each field matched through `col()`.
24
43
  */
25
44
  export function relaxTenantColumns(fields, tableConf, schema) {
26
- const columns = tenantOwnColumns(tableConf?.tenantScope);
45
+ const columns = tenantOwnColumns(resolvedTenantScope(tableConf));
27
46
  if (columns.length === 0)
28
47
  return;
29
48
  for (const field of Object.keys(fields)) {
@@ -34,7 +53,7 @@ export function relaxTenantColumns(fields, tableConf, schema) {
34
53
  /**
35
54
  * Build secondary table fields for body schemas:
36
55
  * - FK field becomes Optional (auto-filled from main)
37
- * - excludeFromCreation fields become Optional
56
+ * - insertExclude fields become Optional
38
57
  * - tenant column(s) become Optional (injected or validated by the engine)
39
58
  */
40
59
  function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
@@ -42,11 +61,9 @@ function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
42
61
  if (joinField in fields) {
43
62
  fields[joinField] = Type.Optional(fields[joinField]);
44
63
  }
45
- if (secondaryTableConf?.excludeFromCreation) {
46
- for (const field of secondaryTableConf.excludeFromCreation) {
47
- if (field in fields) {
48
- fields[field] = Type.Optional(fields[field]);
49
- }
64
+ for (const field of insertExcludedFields(secondaryTableConf)) {
65
+ if (field in fields) {
66
+ fields[field] = Type.Optional(fields[field]);
50
67
  }
51
68
  }
52
69
  relaxTenantColumns(fields, secondaryTableConf, joinSchema);
@@ -54,7 +71,7 @@ function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
54
71
  }
55
72
  /** The same schema without the Optional modifier, i.e. as a required property. */
56
73
  export function asRequired(schema) {
57
- if (!(OptionalKind in schema))
74
+ if (!IsOptional(schema))
58
75
  return schema;
59
76
  return Type.Required(Type.Object({ value: schema })).properties.value;
60
77
  }
@@ -94,40 +111,70 @@ export function applySchemaOverrides(fields, tableConf) {
94
111
  */
95
112
  function buildJoinAliasMap(tableConf, dbTables, build, options = {}) {
96
113
  const out = {};
97
- if (!tableConf.allowedWriteJoins?.length)
114
+ if (!tableConf.writeJoins?.length)
98
115
  return out;
99
116
  const objOpts = options.strictItems ? { additionalProperties: false } : {};
100
- for (const j of tableConf.allowedWriteJoins) {
101
- const sc = findSecondaryTableConf(dbTables, j.joinSchema.tableName);
117
+ for (const j of tableConf.writeJoins) {
118
+ if (options.include && !options.include(j))
119
+ continue;
120
+ const sc = resolveJoinTableConf(dbTables, j);
102
121
  const item = Type.Object(build(j, sc), objOpts);
103
- out[j.alias] = Type.Array(options.itemsPartial ? Type.Partial(item) : item);
122
+ // Partial rebuilds the object and drops the source's options: hand them over again.
123
+ out[j.alias] = Type.Array(options.itemsPartial ? Type.Partial(item, objOpts) : item);
104
124
  }
105
125
  return out;
106
126
  }
127
+ /** An alias map as an Optional, open object: see `attachRequestSections`. */
128
+ function aliasSection(map) {
129
+ return Type.Optional(Type.Partial(Type.Object(map)));
130
+ }
131
+ /** A relation `replace` can address: one with an `upsertKey` to tell its rows apart (ADR 0029). */
132
+ const replaceable = (join) => join.upsertKey !== undefined;
107
133
  /**
108
- * Attach `secondaries` (and optionally `deletions`) sections to the given target object,
109
- * iterating once over `tableConf.allowedWriteJoins`. Mutates `target` in-place.
110
- * Used by body and response schema builders for insert/update/bulk-upsert.
134
+ * Attach the child-row sections of a write request to a body schema, in place: `upsert` for
135
+ * every write join, `delete` (update and bulk upsert, `withDelete`) with every field Optional —
136
+ * the engine fills in the FK, the client names the key — and `replace` for the relations with
137
+ * an `upsertKey`, omitted when none has one.
111
138
  *
112
- * Deletions are emitted with every field Optional (Type.Partial) because the engine
113
- * auto-injects the FK to main and the consumer typically just provides the PK (or a small
114
- * subset of fields) to identify which child rows to delete.
139
+ * Every alias container stays open: an alias the table does not declare, or one `replace`
140
+ * cannot address, must reach the engine, which refuses it with a 400 at that alias
141
+ * (`assertWriteJoinAliases`). Closed, Fastify's default Ajv (`removeAdditional`) would strip
142
+ * it, and the write would answer success with the child rows unwritten.
115
143
  */
116
- export function attachWriteJoinSections(target, tableConf, dbTables, options) {
117
- if (!tableConf.allowedWriteJoins?.length)
144
+ export function attachRequestSections(target, tableConf, dbTables, options) {
145
+ if (!tableConf.writeJoins?.length)
118
146
  return;
119
- // additionalProperties:false closes the alias container itself, not just the items: an alias
120
- // the table does not declare must be a validation error, never a key that reaches the engine.
121
- const aliasMap = { additionalProperties: false };
122
- target.secondaries = Type.Optional(Type.Partial(Type.Object(buildJoinAliasMap(tableConf, dbTables, options.secondaryFields, { strictItems: options.strictItems }), aliasMap)));
123
- if (options.withDeletions) {
124
- target.deletions = Type.Optional(Type.Partial(Type.Object(buildJoinAliasMap(tableConf, dbTables, ({ joinSchema }) => joinSchema.fields, { itemsPartial: true }), aliasMap)));
147
+ const strict = { strictItems: true };
148
+ target.upsert = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinBodyFields, strict));
149
+ if (options.withDelete) {
150
+ target.delete = aliasSection(buildJoinAliasMap(tableConf, dbTables, ({ joinSchema }) => joinSchema.fields, { itemsPartial: true }));
151
+ }
152
+ if (tableConf.writeJoins.some(replaceable)) {
153
+ target.replace = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinBodyFields, { ...strict, include: replaceable }));
154
+ }
155
+ }
156
+ /**
157
+ * Attach the outcome sections of a write response, in place: `secondaries`, the key of every
158
+ * child row written, and `deletions`, the child rows removed. `deletions` lists every write
159
+ * join (`'all'`, update and bulk upsert) or only those a `replace` can address (`'replaced'`,
160
+ * insert — the only removals an insert makes), and is omitted when none qualifies.
161
+ */
162
+ export function attachOutcomeSections(target, tableConf, dbTables, options) {
163
+ if (!tableConf.writeJoins?.length)
164
+ return;
165
+ target.secondaries = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinResponseFields));
166
+ const include = options.deletions === 'all' ? undefined : replaceable;
167
+ if (!include || tableConf.writeJoins.some(include)) {
168
+ target.deletions = aliasSection(buildJoinAliasMap(tableConf, dbTables, ({ joinSchema }) => joinSchema.fields, { itemsPartial: true, include }));
125
169
  }
126
170
  }
127
171
  /** JoinFieldsBuilder for body schemas: includes all join fields with FK + excluded made Optional. */
128
- export const writeJoinBodyFields = (j, sc) => buildSecondaryFields(j.joinSchema, j.joinField, sc);
129
- /** JoinFieldsBuilder for response schemas: PK only. */
130
- export const writeJoinResponseFields = (j, sc) => pkSchema(sc, j.joinSchema, j.joinField);
172
+ const writeJoinBodyFields = (j, sc) => buildSecondaryFields(j.joinSchema, j.joinField, sc);
173
+ /**
174
+ * JoinFieldsBuilder for response schemas: the child's primary key — its table config's, else
175
+ * its Schema's, else the join field, as the engine reports it.
176
+ */
177
+ const writeJoinResponseFields = (j, sc) => pkSchema(sc, j.joinSchema, j.joinSchema.primaryKey ?? j.joinField);
131
178
  /** Schema fields minus the ones hidden by `readExclude`. */
132
179
  export function readableFields(schema, tableConf) {
133
180
  if (!tableConf?.readExclude?.length)
@@ -1,4 +1,4 @@
1
- import { type TObject } from '@sinclair/typebox';
1
+ import { type TObject } from 'typebox';
2
2
  import type { DbTables } from '../../types.js';
3
3
  export declare function InsertTableBody(dbTables: DbTables, tableName: string): TObject;
4
4
  export declare function InsertTableResponse(dbTables: DbTables, tableName: string): TObject;
@@ -1 +1 @@
1
- {"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/insert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,mBAAmB,CAAC;AAErE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAW/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAiC9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAalF"}
1
+ {"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/insert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAE3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAW/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA2B9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAWlF"}
@@ -1,19 +1,17 @@
1
- import { Type } from '@sinclair/typebox';
1
+ import { Type } from 'typebox';
2
2
  import { primaryAsString } from '../../types.js';
3
- import { writableFields } from '../write-access.js';
4
- import { pkSchema, applySchemaOverrides, attachWriteJoinSections, relaxTenantColumns, writeJoinBodyFields, writeJoinResponseFields, } from './helpers.js';
3
+ import { writableFields, insertExcludedFields } from '../write-access.js';
4
+ import { pkSchema, applySchemaOverrides, attachRequestSections, attachOutcomeSections, relaxTenantColumns, WriteActionSchema, } from './helpers.js';
5
5
  export function InsertTableBody(dbTables, tableName) {
6
6
  const tableConf = dbTables[tableName];
7
- const schema = tableConf.Schema;
8
- // Main: full validation, but excludeFromCreation fields become Optional. Fields the
7
+ const schema = tableConf.schema;
8
+ // Main: full validation, but insertExclude fields become Optional. Fields the
9
9
  // database computes are not offered at all — sending one is rejected here (400) rather
10
10
  // than by the driver (500).
11
11
  const mainFields = writableFields(applySchemaOverrides({ ...schema.fields }, tableConf), tableConf, schema);
12
- if (tableConf.excludeFromCreation) {
13
- for (const field of tableConf.excludeFromCreation) {
14
- if (field in mainFields)
15
- mainFields[field] = Type.Optional(mainFields[field]);
16
- }
12
+ for (const field of insertExcludedFields(tableConf)) {
13
+ if (field in mainFields)
14
+ mainFields[field] = Type.Optional(mainFields[field]);
17
15
  }
18
16
  // The tenant column is the engine's to fill or check, never the client's to be forced to send.
19
17
  relaxTenantColumns(mainFields, tableConf, schema);
@@ -22,21 +20,15 @@ export function InsertTableBody(dbTables, tableName) {
22
20
  // rejected (400) instead of silently reaching the INSERT as columns (mass assignment).
23
21
  main: Type.Object(mainFields, { additionalProperties: false }),
24
22
  };
25
- attachWriteJoinSections(bodyProperties, tableConf, dbTables, {
26
- withDeletions: false,
27
- secondaryFields: writeJoinBodyFields,
28
- strictItems: true,
29
- });
23
+ attachRequestSections(bodyProperties, tableConf, dbTables, { withDelete: false });
30
24
  return Type.Object(bodyProperties);
31
25
  }
32
26
  export function InsertTableResponse(dbTables, tableName) {
33
27
  const tableConf = dbTables[tableName];
34
28
  const responseProperties = {
35
- main: Type.Object(pkSchema(tableConf, tableConf.Schema, primaryAsString(tableConf.primary))),
29
+ main: Type.Object(pkSchema(tableConf, tableConf.schema, primaryAsString(tableConf.primary))),
30
+ action: WriteActionSchema,
36
31
  };
37
- attachWriteJoinSections(responseProperties, tableConf, dbTables, {
38
- withDeletions: false,
39
- secondaryFields: writeJoinResponseFields,
40
- });
32
+ attachOutcomeSections(responseProperties, tableConf, dbTables, { deletions: 'replaced' });
41
33
  return Type.Object(responseProperties);
42
34
  }
@@ -1,16 +1,5 @@
1
- import { type TObject } from '@sinclair/typebox';
1
+ import { type TObject } from 'typebox';
2
2
  import type { DbTables } from '../../types.js';
3
- export declare function SearchTableBodyPost(dbTables: DbTables, tableName: string): TObject;
4
- /**
5
- * Querystring schema, parameterized on the plugin's `maxItemsPerPage`.
6
- *
7
- * The `itemsPerPage` default MUST NOT exceed the cap: Ajv injects the default before any
8
- * runtime check runs, so a fixed default of 500 combined with `maxItemsPerPage: 100` would
9
- * make every request without an explicit `itemsPerPage` fail with 400. The default is
10
- * therefore `min(500, cap)` and the cap doubles as the schema `maximum` (structured 400).
11
- */
12
- export declare function SearchTableQuery(maxItemsPerPage?: number): TObject;
13
- /** Default-cap variant, kept for granular composition and backward compatibility. */
14
- export declare const SearchTableQueryString: TObject<import("@sinclair/typebox").TProperties>;
3
+ export declare function SearchTableBodyPost(dbTables: DbTables, tableName: string, maxItemsPerPage?: number): TObject;
15
4
  export declare function SearchTableResponse(dbTables: DbTables, tableName: string): TObject;
16
5
  //# sourceMappingURL=search.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,mBAAmB,CAAC;AAIrE,OAAO,KAAK,EAAE,QAAQ,EAAU,MAAM,gBAAgB,CAAC;AAkJvD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAyClF;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,eAAe,GAAE,MAAmC,GAAG,OAAO,CAe9F;AAED,qFAAqF;AACrF,eAAO,MAAM,sBAAsB,kDAAqB,CAAC;AAEzD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA2ClF"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAM3D,OAAO,KAAK,EAAE,QAAQ,EAAe,MAAM,gBAAgB,CAAC;AA4K5D,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,eAAe,GAAE,MAAmC,GACnD,OAAO,CAgDT;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA6ClF"}
@@ -1,7 +1,21 @@
1
- import { Type } from '@sinclair/typebox';
1
+ import { Type } from 'typebox';
2
2
  import { ALLOWED_METHODS, MAX_CONDITIONS, MAX_ORDER_BY_LENGTH } from '../condition-methods.js';
3
3
  import { readableFields } from './helpers.js';
4
- import { DEFAULT_MAX_ITEMS_PER_PAGE } from '../../types.js';
4
+ import { resolveJoinTableConf } from '../table-helpers.js';
5
+ import { extraFilterFields } from '../read-access.js';
6
+ import { DEFAULT_MAX_ITEMS_PER_PAGE, COMPUTE_FNS } from '../../types.js';
7
+ // ─── orderBy / paginator / compute ──
8
+ const orderByField = () => Type.Optional(Type.String({ maxLength: MAX_ORDER_BY_LENGTH }));
9
+ const pageField = () => Type.Optional(Type.Integer({ minimum: 1 }));
10
+ /**
11
+ * No schema default on purpose: Ajv injects defaults before the handler runs, and a request
12
+ * without a paginator must stay observable as such (LIMIT `maxItemsPerPage`, no COUNT, no
13
+ * `pagination` block). The 500-row default for `page` alone is applied by the route. The cap
14
+ * doubles as the schema `maximum`, so exceeding it is a structured 400.
15
+ */
16
+ const itemsPerPageField = (maxItemsPerPage) => Type.Optional(Type.Integer({ minimum: 1, maximum: maxItemsPerPage }));
17
+ /** `{ min?: string[], max?: string[], sum?: string[], avg?: string[] }`. */
18
+ const computeObject = () => Type.Object(Object.fromEntries(COMPUTE_FNS.map((fn) => [fn, Type.Optional(Type.Array(Type.String()))])));
5
19
  /**
6
20
  * A `filters` map.
7
21
  *
@@ -39,6 +53,8 @@ function computedFieldTypes(tableConf) {
39
53
  }
40
54
  return out;
41
55
  }
56
+ /** A projection: field names, validated by the engine (see the main `select`). */
57
+ const SelectSchema = Type.Array(Type.String());
42
58
  function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
43
59
  const props = {
44
60
  joinMustExist: {},
@@ -46,9 +62,9 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
46
62
  joinGroup: {},
47
63
  joinLeft: {},
48
64
  };
49
- for (const joinDef of tableConf.allowedReadJoins ?? []) {
65
+ for (const joinDef of tableConf.readJoins ?? []) {
50
66
  const { joinSchema, alias, unique } = joinDef;
51
- const joinTableConf = dbTables[joinSchema.tableName];
67
+ const joinTableConf = resolveJoinTableConf(dbTables, joinDef);
52
68
  const joinSchemaFields = joinTableConf
53
69
  ? { ...readableFields(joinSchema, joinTableConf), ...computedFieldTypes(joinTableConf) }
54
70
  : { ...joinSchema.fields };
@@ -56,7 +72,7 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
56
72
  // own `filters()` — buildJoinRefCondition, i.e. the unique:false paths. joinLeft
57
73
  // builds its condition inline (buildLeftJoinClauses) and handles schema and computed
58
74
  // fields only, so advertising extraFilters there would promise a silent no-op.
59
- const joinFilterFields = { ...joinSchemaFields, ...(joinTableConf?.extraFilters ?? {}) };
75
+ const joinFilterFields = { ...joinSchemaFields, ...extraFilterFields(joinTableConf) };
60
76
  const joinRefShape = {
61
77
  filters: Type.Optional(filtersObject(joinFilterFields)),
62
78
  conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
@@ -65,25 +81,23 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
65
81
  props.joinLeft[alias] = Type.Object({
66
82
  filters: Type.Optional(filtersObject(joinSchemaFields)),
67
83
  conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
68
- selection: Type.Optional(Type.String()),
84
+ select: Type.Optional(SelectSchema),
69
85
  });
70
86
  continue;
71
87
  }
72
88
  props.joinMustExist[alias] = Type.Object(joinRefShape);
73
89
  props.joinMultiple[alias] = Type.Object({
74
90
  ...joinRefShape,
75
- selection: Type.Optional(Type.String()),
91
+ select: Type.Optional(SelectSchema),
76
92
  });
77
93
  props.joinGroup[alias] = Type.Object({
78
- aggregations: Type.Object({
79
- by: Type.Optional(Type.String()),
80
- distinctCount: Type.Optional(Type.Array(Type.String())),
81
- min: Type.Optional(Type.Array(Type.String())),
82
- max: Type.Optional(Type.Array(Type.String())),
83
- sum: Type.Optional(Type.Array(Type.String())),
84
- avg: Type.Optional(Type.Array(Type.String())),
85
- count: Type.Optional(Type.Array(Type.String())),
86
- }),
94
+ by: Type.Optional(Type.String()),
95
+ distinctCount: Type.Optional(Type.Array(Type.String())),
96
+ min: Type.Optional(Type.Array(Type.String())),
97
+ max: Type.Optional(Type.Array(Type.String())),
98
+ sum: Type.Optional(Type.Array(Type.String())),
99
+ avg: Type.Optional(Type.Array(Type.String())),
100
+ count: Type.Optional(Type.Array(Type.String())),
87
101
  filters: Type.Optional(filtersObject(joinFilterFields)),
88
102
  conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
89
103
  });
@@ -95,9 +109,9 @@ function buildJoinResponseProps(dbTables, tableConf) {
95
109
  const joinMultipleProps = {};
96
110
  const joinLeftProps = {};
97
111
  const joinGroupProps = {};
98
- for (const joinDef of tableConf.allowedReadJoins ?? []) {
112
+ for (const joinDef of tableConf.readJoins ?? []) {
99
113
  const { joinSchema, alias, unique } = joinDef;
100
- const itemArray = Type.Array(Type.Partial(Type.Object(readableFields(joinSchema, dbTables[joinSchema.tableName]))));
114
+ const itemArray = Type.Array(Type.Partial(Type.Object(readableFields(joinSchema, resolveJoinTableConf(dbTables, joinDef)))));
101
115
  if (unique) {
102
116
  joinLeftProps[alias] = itemArray;
103
117
  }
@@ -108,18 +122,25 @@ function buildJoinResponseProps(dbTables, tableConf) {
108
122
  }
109
123
  return { joinMultipleProps, joinLeftProps, joinGroupProps };
110
124
  }
125
+ /**
126
+ * One joinGroup alias in the response: always `rows`, one per group (a single row without
127
+ * `by`), each carrying `by` when grouped and the aggregates nested by function, as in the
128
+ * dotted notation `alias.fn.field`.
129
+ */
111
130
  const JoinGroupResultItem = Type.Object({
112
- sum: Type.Optional(Type.Record(Type.String(), Type.Any())),
113
- min: Type.Optional(Type.Record(Type.String(), Type.Any())),
114
- max: Type.Optional(Type.Record(Type.String(), Type.Any())),
115
- avg: Type.Optional(Type.Record(Type.String(), Type.Any())),
116
- count: Type.Optional(Type.Record(Type.String(), Type.Any())),
117
- distinctCount: Type.Optional(Type.Record(Type.String(), Type.Any())),
118
- rows: Type.Optional(Type.Array(Type.Any())),
131
+ rows: Type.Array(Type.Object({
132
+ by: Type.Optional(Type.Any()),
133
+ sum: Type.Optional(Type.Record(Type.String(), Type.Any())),
134
+ min: Type.Optional(Type.Record(Type.String(), Type.Any())),
135
+ max: Type.Optional(Type.Record(Type.String(), Type.Any())),
136
+ avg: Type.Optional(Type.Record(Type.String(), Type.Any())),
137
+ count: Type.Optional(Type.Record(Type.String(), Type.Any())),
138
+ distinctCount: Type.Optional(Type.Record(Type.String(), Type.Any())),
139
+ })),
119
140
  });
120
- export function SearchTableBodyPost(dbTables, tableName) {
141
+ export function SearchTableBodyPost(dbTables, tableName, maxItemsPerPage = DEFAULT_MAX_ITEMS_PER_PAGE) {
121
142
  const tableConf = dbTables[tableName];
122
- const schema = tableConf.Schema;
143
+ const schema = tableConf.schema;
123
144
  const methodEnum = Type.Union(ALLOWED_METHODS.map((m) => Type.Literal(m)));
124
145
  const conditionItemSchema = Type.Object({
125
146
  field: Type.String(),
@@ -129,7 +150,7 @@ export function SearchTableBodyPost(dbTables, tableName) {
129
150
  const computedTypes = computedFieldTypes(tableConf);
130
151
  const filterFields = {
131
152
  ...readableFields(schema, tableConf),
132
- ...tableConf.extraFilters,
153
+ ...extraFilterFields(tableConf),
133
154
  ...computedTypes,
134
155
  };
135
156
  const filtersSchema = Type.Optional(filtersObject(filterFields));
@@ -137,10 +158,18 @@ export function SearchTableBodyPost(dbTables, tableName) {
137
158
  const bodyProperties = {
138
159
  filters: filtersSchema,
139
160
  conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
161
+ // Main projection, schema and computed fields alike, '*' for every readable field. Names
162
+ // are validated by the engine (readExclude included), not here: a closed enum would be
163
+ // stripped by `removeAdditional` the same way a closed filters map is.
164
+ select: Type.Optional(SelectSchema),
165
+ // Ordering, paging and aggregates belong with the rest of the request, in the body only.
166
+ orderBy: orderByField(),
167
+ paginator: Type.Optional(Type.Object({
168
+ page: pageField(),
169
+ itemsPerPage: itemsPerPageField(maxItemsPerPage),
170
+ })),
171
+ compute: Type.Optional(computeObject()),
140
172
  };
141
- if (Object.keys(computedTypes).length > 0) {
142
- bodyProperties.selectComputed = Type.Optional(Type.Array(Type.String()));
143
- }
144
173
  if (Object.keys(joins.joinMustExist).length > 0) {
145
174
  bodyProperties.joinMustExist = Type.Optional(Type.Partial(Type.Object(joins.joinMustExist)));
146
175
  bodyProperties.joinMultiple = Type.Optional(Type.Partial(Type.Object(joins.joinMultiple)));
@@ -151,40 +180,14 @@ export function SearchTableBodyPost(dbTables, tableName) {
151
180
  }
152
181
  return Type.Object(bodyProperties);
153
182
  }
154
- /**
155
- * Querystring schema, parameterized on the plugin's `maxItemsPerPage`.
156
- *
157
- * The `itemsPerPage` default MUST NOT exceed the cap: Ajv injects the default before any
158
- * runtime check runs, so a fixed default of 500 combined with `maxItemsPerPage: 100` would
159
- * make every request without an explicit `itemsPerPage` fail with 400. The default is
160
- * therefore `min(500, cap)` and the cap doubles as the schema `maximum` (structured 400).
161
- */
162
- export function SearchTableQuery(maxItemsPerPage = DEFAULT_MAX_ITEMS_PER_PAGE) {
163
- return Type.Object({
164
- orderBy: Type.Optional(Type.String({ maxLength: MAX_ORDER_BY_LENGTH })),
165
- page: Type.Optional(Type.Integer({ minimum: 1 })),
166
- itemsPerPage: Type.Optional(Type.Integer({
167
- minimum: 1,
168
- maximum: maxItemsPerPage,
169
- default: Math.min(500, maxItemsPerPage),
170
- })),
171
- computeMin: Type.Optional(Type.String()),
172
- computeMax: Type.Optional(Type.String()),
173
- computeSum: Type.Optional(Type.String()),
174
- computeAvg: Type.Optional(Type.String()),
175
- // selectComputed list goes in the body (POST), not querystring — see SearchTableBodyPost.
176
- });
177
- }
178
- /** Default-cap variant, kept for granular composition and backward compatibility. */
179
- export const SearchTableQueryString = SearchTableQuery();
180
183
  export function SearchTableResponse(dbTables, tableName) {
181
184
  const tableConf = dbTables[tableName];
182
- // Computed fields are present in main rows only when explicitly listed in
183
- // request body's selectComputed, so they're optional/Partial here.
185
+ // Computed fields are present in main rows only when listed in the request's `select`,
186
+ // so they're optional/Partial here.
184
187
  const computedTypes = computedFieldTypes(tableConf);
185
188
  // Not narrowed by schemaOverrides: an override rules what this API accepts from now on,
186
189
  // while the rows already stored predate it (see applySchemaOverrides).
187
- const mainReadable = readableFields(tableConf.Schema, tableConf);
190
+ const mainReadable = readableFields(tableConf.schema, tableConf);
188
191
  const mainItem = Object.keys(computedTypes).length > 0
189
192
  ? Type.Partial(Type.Object({ ...mainReadable, ...computedTypes }))
190
193
  : Type.Partial(Type.Object(mainReadable));
@@ -200,10 +203,10 @@ export function SearchTableResponse(dbTables, tableName) {
200
203
  if (Object.keys(joinLeftProps).length > 0) {
201
204
  responseProps.joinLeft = Type.Optional(Type.Partial(Type.Object(joinLeftProps)));
202
205
  }
206
+ responseProps.computed = Type.Optional(Type.Object(Object.fromEntries(COMPUTE_FNS.map((fn) => [fn, Type.Optional(Type.Record(Type.String(), Type.Any()))]))));
203
207
  responseProps.pagination = Type.Optional(Type.Object({
204
208
  total: Type.Integer(),
205
209
  pages: Type.Integer(),
206
- computed: Type.Optional(Type.Record(Type.String(), Type.Unknown())),
207
210
  paginator: Type.Object({
208
211
  page: Type.Integer(),
209
212
  itemsPerPage: Type.Integer(),
@@ -1,4 +1,4 @@
1
- import { type TObject } from '@sinclair/typebox';
1
+ import { type TObject } from 'typebox';
2
2
  import type { DbTables } from '../../types.js';
3
3
  export declare function UpdateTableBody(dbTables: DbTables, tableName: string): TObject;
4
4
  export declare function UpdateTableResponse(dbTables: DbTables, tableName: string): TObject;
@@ -1 +1 @@
1
- {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,mBAAmB,CAAC;AAErE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAW/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA+B9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAalF"}
1
+ {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAE3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAU/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA2B9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAUlF"}
@@ -1,10 +1,10 @@
1
- import { Type } from '@sinclair/typebox';
1
+ import { Type } from 'typebox';
2
2
  import { primaryAsString } from '../../types.js';
3
3
  import { writableFields } from '../write-access.js';
4
- import { pkSchema, applySchemaOverrides, asRequired, attachWriteJoinSections, writeJoinBodyFields, writeJoinResponseFields, } from './helpers.js';
4
+ import { pkSchema, applySchemaOverrides, asRequired, attachRequestSections, attachOutcomeSections, } from './helpers.js';
5
5
  export function UpdateTableBody(dbTables, tableName) {
6
6
  const tableConf = dbTables[tableName];
7
- const schema = tableConf.Schema;
7
+ const schema = tableConf.schema;
8
8
  // Main: PK required, all other fields optional (overrides applied before Optional wrap)
9
9
  const baseFields = writableFields(applySchemaOverrides({ ...schema.fields }, tableConf), tableConf, schema);
10
10
  const mainFields = {};
@@ -19,21 +19,14 @@ export function UpdateTableBody(dbTables, tableName) {
19
19
  // rejected (400) instead of silently reaching the UPDATE as columns (mass assignment).
20
20
  main: Type.Object(mainFields, { additionalProperties: false }),
21
21
  };
22
- attachWriteJoinSections(bodyProperties, tableConf, dbTables, {
23
- withDeletions: true,
24
- secondaryFields: writeJoinBodyFields,
25
- strictItems: true,
26
- });
22
+ attachRequestSections(bodyProperties, tableConf, dbTables, { withDelete: true });
27
23
  return Type.Object(bodyProperties);
28
24
  }
29
25
  export function UpdateTableResponse(dbTables, tableName) {
30
26
  const tableConf = dbTables[tableName];
31
27
  const responseProperties = {
32
- main: Type.Object(pkSchema(tableConf, tableConf.Schema, primaryAsString(tableConf.primary))),
28
+ main: Type.Object(pkSchema(tableConf, tableConf.schema, primaryAsString(tableConf.primary))),
33
29
  };
34
- attachWriteJoinSections(responseProperties, tableConf, dbTables, {
35
- withDeletions: true,
36
- secondaryFields: writeJoinResponseFields,
37
- });
30
+ attachOutcomeSections(responseProperties, tableConf, dbTables, { deletions: 'all' });
38
31
  return Type.Object(responseProperties);
39
32
  }