@rawsql-ts/ztd-cli 0.17.0 → 0.19.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 (242) hide show
  1. package/README.md +532 -366
  2. package/package.json +26 -15
  3. package/templates/.editorconfig +16 -16
  4. package/templates/.prettierignore +2 -2
  5. package/templates/.prettierrc +24 -24
  6. package/templates/AGENTS.md +30 -52
  7. package/templates/CONTEXT.md +11 -0
  8. package/templates/CONTEXT.webapi.md +11 -0
  9. package/templates/DESIGN.md +17 -0
  10. package/templates/DEV_NOTES.md +14 -0
  11. package/templates/PROMPT_DOGFOOD.webapi.md +49 -0
  12. package/templates/README.md +46 -14
  13. package/templates/README.webapi.md +38 -0
  14. package/templates/scripts/local-source-guard.mjs +189 -0
  15. package/templates/src/AGENTS.md +26 -26
  16. package/templates/src/application/AGENTS.md +15 -0
  17. package/templates/src/application/README.md +6 -0
  18. package/templates/src/catalog/AGENTS.md +28 -37
  19. package/templates/src/catalog/runtime/AGENTS.md +28 -75
  20. package/templates/src/catalog/runtime/_coercions.local-source.ts +30 -0
  21. package/templates/src/catalog/runtime/_coercions.ts +30 -1
  22. package/templates/src/catalog/runtime/_smoke.runtime.ts +21 -21
  23. package/templates/src/catalog/specs/AGENTS.md +41 -48
  24. package/templates/src/catalog/specs/_smoke.spec.arktype.ts +21 -21
  25. package/templates/src/catalog/specs/_smoke.spec.zod.ts +20 -20
  26. package/templates/src/db/sql-client-adapters.ts +32 -0
  27. package/templates/src/db/sql-client.ts +24 -24
  28. package/templates/src/domain/AGENTS.md +15 -0
  29. package/templates/src/domain/README.md +6 -0
  30. package/templates/src/infrastructure/AGENTS.md +14 -0
  31. package/templates/src/infrastructure/README.md +6 -0
  32. package/templates/src/infrastructure/db/AGENTS.md +14 -0
  33. package/templates/src/infrastructure/db/sql-client-adapters.ts +34 -0
  34. package/templates/{dist/ztd-cli/templates/src → src/infrastructure}/db/sql-client.ts +3 -3
  35. package/templates/src/infrastructure/persistence/AGENTS.md +18 -0
  36. package/templates/src/infrastructure/persistence/README.md +8 -0
  37. package/templates/src/infrastructure/persistence/repositories/AGENTS.md +17 -0
  38. package/templates/src/infrastructure/persistence/repositories/tables/AGENTS.md +20 -0
  39. package/templates/src/infrastructure/persistence/repositories/tables/README.md +5 -0
  40. package/templates/src/infrastructure/persistence/repositories/views/AGENTS.md +16 -0
  41. package/templates/src/infrastructure/persistence/repositories/views/README.md +5 -0
  42. package/templates/src/infrastructure/telemetry/AGENTS.md +14 -0
  43. package/templates/src/infrastructure/telemetry/consoleRepositoryTelemetry.ts +66 -0
  44. package/templates/src/infrastructure/telemetry/repositoryTelemetry.ts +26 -0
  45. package/templates/src/infrastructure/telemetry/types.ts +48 -0
  46. package/templates/src/jobs/AGENTS.md +25 -26
  47. package/templates/src/jobs/README.md +2 -2
  48. package/templates/src/local/sql-contract.ts +1 -0
  49. package/templates/src/presentation/AGENTS.md +15 -0
  50. package/templates/src/presentation/http/AGENTS.md +15 -0
  51. package/templates/src/presentation/http/README.md +6 -0
  52. package/templates/src/repositories/AGENTS.md +30 -118
  53. package/templates/src/repositories/tables/AGENTS.md +29 -94
  54. package/templates/src/repositories/tables/README.md +5 -3
  55. package/templates/src/repositories/views/AGENTS.md +25 -25
  56. package/templates/src/repositories/views/README.md +5 -3
  57. package/templates/src/sql/AGENTS.md +30 -77
  58. package/templates/src/sql/README.md +5 -5
  59. package/templates/tests/AGENTS.md +29 -62
  60. package/templates/tests/generated/AGENTS.md +23 -16
  61. package/templates/tests/smoke.test.ts +25 -5
  62. package/templates/tests/smoke.validation.test.ts +34 -34
  63. package/templates/tests/support/AGENTS.md +24 -26
  64. package/templates/tests/support/global-setup.ts +15 -15
  65. package/templates/tests/support/testkit-client.ts +14 -14
  66. package/templates/tests/support/testkit-client.webapi.ts +14 -0
  67. package/templates/tests/ztd-layout.generated.ts +6 -6
  68. package/templates/tsconfig.json +15 -15
  69. package/templates/vitest.config.ts +13 -13
  70. package/templates/ztd/AGENTS.md +23 -18
  71. package/templates/ztd/README.md +5 -5
  72. package/templates/ztd/ddl/AGENTS.md +26 -34
  73. package/templates/ztd/ddl/demo.sql +74 -74
  74. package/LICENSE +0 -21
  75. package/dist/commands/ddl.d.ts +0 -7
  76. package/dist/commands/ddl.js +0 -118
  77. package/dist/commands/ddl.js.map +0 -1
  78. package/dist/commands/diff.d.ts +0 -10
  79. package/dist/commands/diff.js +0 -38
  80. package/dist/commands/diff.js.map +0 -1
  81. package/dist/commands/genEntities.d.ts +0 -6
  82. package/dist/commands/genEntities.js +0 -50
  83. package/dist/commands/genEntities.js.map +0 -1
  84. package/dist/commands/init.d.ts +0 -79
  85. package/dist/commands/init.js +0 -1083
  86. package/dist/commands/init.js.map +0 -1
  87. package/dist/commands/lint.d.ts +0 -59
  88. package/dist/commands/lint.js +0 -358
  89. package/dist/commands/lint.js.map +0 -1
  90. package/dist/commands/options.d.ts +0 -9
  91. package/dist/commands/options.js +0 -48
  92. package/dist/commands/options.js.map +0 -1
  93. package/dist/commands/pull.d.ts +0 -10
  94. package/dist/commands/pull.js +0 -105
  95. package/dist/commands/pull.js.map +0 -1
  96. package/dist/commands/ztdConfig.d.ts +0 -23
  97. package/dist/commands/ztdConfig.js +0 -216
  98. package/dist/commands/ztdConfig.js.map +0 -1
  99. package/dist/commands/ztdConfigCommand.d.ts +0 -5
  100. package/dist/commands/ztdConfigCommand.js +0 -157
  101. package/dist/commands/ztdConfigCommand.js.map +0 -1
  102. package/dist/index.d.ts +0 -2
  103. package/dist/index.js +0 -22
  104. package/dist/index.js.map +0 -1
  105. package/dist/utils/agents.d.ts +0 -1
  106. package/dist/utils/agents.js +0 -48
  107. package/dist/utils/agents.js.map +0 -1
  108. package/dist/utils/collectSqlFiles.d.ts +0 -9
  109. package/dist/utils/collectSqlFiles.js +0 -58
  110. package/dist/utils/collectSqlFiles.js.map +0 -1
  111. package/dist/utils/connectionSummary.d.ts +0 -3
  112. package/dist/utils/connectionSummary.js +0 -29
  113. package/dist/utils/connectionSummary.js.map +0 -1
  114. package/dist/utils/dbConnection.d.ts +0 -29
  115. package/dist/utils/dbConnection.js +0 -210
  116. package/dist/utils/dbConnection.js.map +0 -1
  117. package/dist/utils/fs.d.ts +0 -1
  118. package/dist/utils/fs.js +0 -12
  119. package/dist/utils/fs.js.map +0 -1
  120. package/dist/utils/normalizePulledSchema.d.ts +0 -12
  121. package/dist/utils/normalizePulledSchema.js +0 -213
  122. package/dist/utils/normalizePulledSchema.js.map +0 -1
  123. package/dist/utils/optionalDependencies.d.ts +0 -35
  124. package/dist/utils/optionalDependencies.js +0 -96
  125. package/dist/utils/optionalDependencies.js.map +0 -1
  126. package/dist/utils/pgDump.d.ts +0 -11
  127. package/dist/utils/pgDump.js +0 -55
  128. package/dist/utils/pgDump.js.map +0 -1
  129. package/dist/utils/sqlLintHelpers.d.ts +0 -18
  130. package/dist/utils/sqlLintHelpers.js +0 -270
  131. package/dist/utils/sqlLintHelpers.js.map +0 -1
  132. package/dist/utils/typeMapper.d.ts +0 -4
  133. package/dist/utils/typeMapper.js +0 -76
  134. package/dist/utils/typeMapper.js.map +0 -1
  135. package/dist/utils/ztdProjectConfig.d.ts +0 -40
  136. package/dist/utils/ztdProjectConfig.js +0 -167
  137. package/dist/utils/ztdProjectConfig.js.map +0 -1
  138. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.d.ts +0 -38
  139. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.js +0 -117
  140. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.js.map +0 -1
  141. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.d.ts +0 -4
  142. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.js +0 -71
  143. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.js.map +0 -1
  144. package/templates/dist/drivers/pg-testkit/src/index.d.ts +0 -5
  145. package/templates/dist/drivers/pg-testkit/src/index.js +0 -11
  146. package/templates/dist/drivers/pg-testkit/src/index.js.map +0 -1
  147. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.d.ts +0 -3
  148. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.js +0 -79
  149. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.js.map +0 -1
  150. package/templates/dist/drivers/pg-testkit/src/types.d.ts +0 -69
  151. package/templates/dist/drivers/pg-testkit/src/types.js +0 -3
  152. package/templates/dist/drivers/pg-testkit/src/types.js.map +0 -1
  153. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.d.ts +0 -15
  154. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.js +0 -34
  155. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.js.map +0 -1
  156. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.d.ts +0 -12
  157. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.js +0 -53
  158. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.js.map +0 -1
  159. package/templates/dist/mapper-core/src/index.d.ts +0 -160
  160. package/templates/dist/mapper-core/src/index.js +0 -637
  161. package/templates/dist/mapper-core/src/index.js.map +0 -1
  162. package/templates/dist/testkit-core/src/errors/index.d.ts +0 -49
  163. package/templates/dist/testkit-core/src/errors/index.js +0 -111
  164. package/templates/dist/testkit-core/src/errors/index.js.map +0 -1
  165. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.d.ts +0 -5
  166. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.js +0 -29
  167. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.js.map +0 -1
  168. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.d.ts +0 -37
  169. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.js +0 -182
  170. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.js.map +0 -1
  171. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.d.ts +0 -20
  172. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.js +0 -121
  173. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.js.map +0 -1
  174. package/templates/dist/testkit-core/src/fixtures/FixtureStore.d.ts +0 -51
  175. package/templates/dist/testkit-core/src/fixtures/FixtureStore.js +0 -199
  176. package/templates/dist/testkit-core/src/fixtures/FixtureStore.js.map +0 -1
  177. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.d.ts +0 -10
  178. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.js +0 -28
  179. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.js.map +0 -1
  180. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.d.ts +0 -18
  181. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.js +0 -80
  182. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.js.map +0 -1
  183. package/templates/dist/testkit-core/src/fixtures/ddlLint.d.ts +0 -59
  184. package/templates/dist/testkit-core/src/fixtures/ddlLint.js +0 -489
  185. package/templates/dist/testkit-core/src/fixtures/ddlLint.js.map +0 -1
  186. package/templates/dist/testkit-core/src/fixtures/naming.d.ts +0 -1
  187. package/templates/dist/testkit-core/src/fixtures/naming.js +0 -6
  188. package/templates/dist/testkit-core/src/fixtures/naming.js.map +0 -1
  189. package/templates/dist/testkit-core/src/index.d.ts +0 -17
  190. package/templates/dist/testkit-core/src/index.js +0 -47
  191. package/templates/dist/testkit-core/src/index.js.map +0 -1
  192. package/templates/dist/testkit-core/src/logger/NoopLogger.d.ts +0 -8
  193. package/templates/dist/testkit-core/src/logger/NoopLogger.js +0 -16
  194. package/templates/dist/testkit-core/src/logger/NoopLogger.js.map +0 -1
  195. package/templates/dist/testkit-core/src/provider/TestkitProvider.d.ts +0 -57
  196. package/templates/dist/testkit-core/src/provider/TestkitProvider.js +0 -149
  197. package/templates/dist/testkit-core/src/provider/TestkitProvider.js.map +0 -1
  198. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.d.ts +0 -43
  199. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.js +0 -473
  200. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.js.map +0 -1
  201. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.d.ts +0 -9
  202. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.js +0 -38
  203. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.js.map +0 -1
  204. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.d.ts +0 -42
  205. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.js +0 -298
  206. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.js.map +0 -1
  207. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.d.ts +0 -12
  208. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.js +0 -63
  209. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.js.map +0 -1
  210. package/templates/dist/testkit-core/src/types/index.d.ts +0 -69
  211. package/templates/dist/testkit-core/src/types/index.js +0 -3
  212. package/templates/dist/testkit-core/src/types/index.js.map +0 -1
  213. package/templates/dist/testkit-core/src/utils/queryHelpers.d.ts +0 -28
  214. package/templates/dist/testkit-core/src/utils/queryHelpers.js +0 -81
  215. package/templates/dist/testkit-core/src/utils/queryHelpers.js.map +0 -1
  216. package/templates/dist/writer-core/src/index.d.ts +0 -34
  217. package/templates/dist/writer-core/src/index.js +0 -115
  218. package/templates/dist/writer-core/src/index.js.map +0 -1
  219. package/templates/dist/ztd-cli/templates/src/db/sql-client.d.ts +0 -20
  220. package/templates/dist/ztd-cli/templates/src/db/sql-client.js +0 -3
  221. package/templates/dist/ztd-cli/templates/src/db/sql-client.js.map +0 -1
  222. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.d.ts +0 -36
  223. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.js +0 -85
  224. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.js.map +0 -1
  225. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.d.ts +0 -20
  226. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.js +0 -33
  227. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.js.map +0 -1
  228. package/templates/dist/ztd-cli/templates/tests/support/global-setup.d.ts +0 -10
  229. package/templates/dist/ztd-cli/templates/tests/support/global-setup.js +0 -29
  230. package/templates/dist/ztd-cli/templates/tests/support/global-setup.js.map +0 -1
  231. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.d.ts +0 -66
  232. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.js +0 -552
  233. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.js.map +0 -1
  234. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.d.ts +0 -1
  235. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.js +0 -82
  236. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.js.map +0 -1
  237. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.d.ts +0 -1
  238. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.js +0 -29
  239. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.js.map +0 -1
  240. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.d.ts +0 -7
  241. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.js +0 -10
  242. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.js.map +0 -1
@@ -1,26 +1,26 @@
1
- # src AGENTS
2
-
3
- This directory is runtime application code.
4
-
5
- ## Boundaries
6
-
7
- - Code under "src/" MUST NOT import from:
8
- - "tests/"
9
- - "tests/generated/"
10
- - any test-only helpers
11
- - Runtime code MUST NOT depend on ZTD internals.
12
- - Generated artifacts are test-only signals, not runtime dependencies.
13
-
14
- ## Implementation principles
15
-
16
- - Keep modules small and explicit.
17
- - Prefer explicit contracts over inference.
18
- - Favor deterministic behavior and clear error surfaces.
19
-
20
- ## Verification (required)
21
-
22
- After changes:
23
- - Run the project typecheck command (example: "pnpm typecheck").
24
- - Run relevant tests (example: "pnpm test" or a filtered command).
25
-
26
- If you touched SQL contracts or catalog specs, run tests that exercise them.
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src`.
3
+ - Governs runtime application code emitted by template generation across both generic and WebAPI-oriented layouts.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Runtime code under `src/` MUST remain independent from `tests/` and `tests/generated/` imports.
8
+ - Runtime code MUST remain independent from ZTD internals.
9
+ - Runtime modules MUST use explicit contracts and deterministic failure surfaces.
10
+ - Domain, application, and presentation layers MUST stay free from direct SQL/DDL ownership when those directories exist.
11
+
12
+ ## ALLOWED
13
+ - Runtime modules MAY use project typecheck and filtered test commands for validation.
14
+
15
+ ## PROHIBITED
16
+ - Importing test-only helpers into runtime code.
17
+ - Treating generated artifacts as runtime dependencies.
18
+
19
+ # Mandatory Workflow
20
+ - Before committing changes under `src/`, run project typecheck and relevant tests.
21
+
22
+ # Hygiene
23
+ - Keep runtime modules explicit and small enough to preserve contract boundaries.
24
+
25
+ # References
26
+ - Parent policy: [../AGENTS.md](../AGENTS.md)
@@ -0,0 +1,15 @@
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src/application`.
3
+ - Defines application-layer orchestration for WebAPI-oriented scaffolds.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Application modules MUST orchestrate domain-facing ports and policies without embedding transport or SQL details.
8
+ - Validation and command handling MAY live here when it stays independent from persistence mechanics.
9
+
10
+ ## PROHIBITED
11
+ - Direct SQL file access.
12
+ - Direct dependence on DDL or QuerySpec implementation details.
13
+
14
+ # Mandatory Workflow
15
+ - Application changes MUST run the relevant typecheck and behavioral tests.
@@ -0,0 +1,6 @@
1
+ # Application
2
+
3
+ Use cases and orchestration live here.
4
+
5
+ - Coordinate domain behavior and repository ports here.
6
+ - Avoid direct ownership of SQL, DDL, and ZTD configuration in this layer.
@@ -1,37 +1,28 @@
1
- # src/catalog AGENTS
2
-
3
- This directory is runtime code.
4
-
5
- ## Purpose
6
-
7
- Catalog defines named query entry points by binding:
8
- - SQL assets ("src/sql/*.sql")
9
- - input parameter contracts
10
- - output DTO contracts
11
- - validation and mapping behavior
12
- - observability hooks (if supported)
13
-
14
- ## Directory roles (important)
15
-
16
- - "src/catalog/specs": human-owned contracts (params + DTO + semantics)
17
- - "src/catalog/runtime": AI-assisted runtime wiring (executors, helpers, sinks)
18
-
19
- ## Non-negotiable ownership
20
-
21
- - Specs are contracts. Do not infer, guess, widen, or narrow them.
22
- - Do not change params / DTO shapes in "specs" without explicit instruction.
23
-
24
- ## Boundaries
25
-
26
- - Code under "src/catalog/" MUST NOT import from:
27
- - "tests/"
28
- - "tests/generated/"
29
- - "ztd/"
30
- - Do not depend on ZTD internals at runtime.
31
-
32
- ## Testing rule (required)
33
-
34
- Every spec MUST have tests that verify:
35
- - SQL executes under ZTD rewriting
36
- - mapping/validation behavior is correct (success and failure)
37
- - output DTO shape matches expectations
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src/catalog`.
3
+ - Defines runtime catalog contract boundaries between human-owned specs and runtime wiring.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Catalog entrypoints MUST bind SQL assets, parameter contracts, output contracts, and runtime validation/mapping.
8
+ - `src/catalog/specs` MUST be treated as human-owned contracts.
9
+ - `src/catalog/runtime` MUST implement runtime wiring only.
10
+ - Code in `src/catalog` MUST remain independent from `tests`, `tests/generated`, and `ztd` imports.
11
+ - Each spec MUST be covered by tests for rewrite execution, mapping/validation outcomes, and output shape.
12
+
13
+ ## ALLOWED
14
+ - Runtime catalog code MAY add observability hooks where contract behavior is unchanged.
15
+
16
+ ## PROHIBITED
17
+ - Changing spec params/DTO contracts without explicit instruction.
18
+ - Runtime dependency on ZTD internals.
19
+
20
+ # Mandatory Workflow
21
+ - Catalog spec or runtime changes MUST run tests that exercise affected specs.
22
+
23
+ # Hygiene
24
+ - Preserve clear separation between `specs` and `runtime` responsibilities.
25
+
26
+ # References
27
+ - Specs contract: [./specs/AGENTS.md](./specs/AGENTS.md)
28
+ - Runtime contract: [./runtime/AGENTS.md](./runtime/AGENTS.md)
@@ -1,75 +1,28 @@
1
- # src/catalog/runtime AGENTS
2
-
3
- This directory contains runtime wiring for catalog specs:
4
- - parameter validation entrypoints
5
- - row-to-DTO mapping
6
- - output validation
7
-
8
- ## Runtime classification
9
-
10
- - This is a runtime directory.
11
- - Code here is executed/loaded by the application.
12
-
13
- ## Responsibilities (critical)
14
-
15
- - Runtime MUST be the only place that:
16
- - validates unknown inputs into typed params
17
- - maps SQL rows (snake_case) into DTO objects
18
- - validates DTO outputs before returning to repositories
19
- - Runtime output validation MUST follow the output contract type (DTO or scalar).
20
- - Command outputs MAY be scalar identifiers or `void`; DTO mapping is required only when the output contract is DTO.
21
-
22
- Repositories MUST call runtime helpers (ensure*/map*) and MUST NOT bypass them.
23
-
24
- ## Validator requirement (required)
25
-
26
- - A validator library (zod or arktype) is always available.
27
- - Runtime MUST apply validators for:
28
- - input params (unknown -> typed)
29
- - outputs (mapped DTO -> validated DTO)
30
-
31
- Do not rely on TypeScript types alone.
32
-
33
- ## SQL row normalization (important)
34
-
35
- SQL drivers may return different runtime representations for the same column types.
36
- Runtime MUST normalize driver-dependent values before validating DTOs.
37
-
38
- Required normalization rules:
39
- - Timestamp columns (e.g. timestamptz) may arrive as Date or string.
40
- - Runtime MUST use `timestampFromDriver` from `@rawsql-ts/sql-contract`.
41
- - Do NOT re-implement `normalizeTimestamp` locally.
42
- - Do NOT call `new Date(...)` directly for driver-dependent timestamp normalization.
43
- - Numeric columns may arrive as number, string, or bigint depending on driver.
44
- - Normalization rules MUST be explicit per contract and MUST NOT be silent.
45
-
46
- Never force SQL assets to encode driver-specific behavior.
47
-
48
- ## Mapping rules (required)
49
-
50
- - Keep SQL assets snake_case and DTO-independent.
51
- - Mapping occurs in runtime:
52
- - snake_case row -> DTO-shaped object
53
- - apply normalization
54
- - validate via spec-owned validator
55
- - DTO camelCase aliases in SQL are forbidden.
56
-
57
- ## Entry points (recommended)
58
-
59
- Prefer these patterns:
60
- - `ensureXxxParams(value: unknown): XxxParams` (validate inputs)
61
- - `mapXxxRowToDto(row: XxxSqlRow): XxxDto` (normalize + validate outputs)
62
-
63
- The `map*` functions MUST always validate before returning.
64
-
65
- ## Error behavior (required)
66
-
67
- - Validation errors must fail fast with clear messages.
68
- - Do not swallow validator errors.
69
- - Do not silently coerce invalid values unless explicitly defined by the contract.
70
-
71
- ## Boundaries
72
-
73
- - Do not perform database I/O here.
74
- - Do not import from "tests/" or "tests/generated/".
75
- - Do not define human-owned contracts here; they live in "src/catalog/specs".
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src/catalog/runtime`.
3
+ - Defines runtime validation, normalization, and row-to-DTO mapping behavior.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Runtime MUST be the only layer that validates unknown input into typed params and validates output contracts.
8
+ - Runtime MUST map snake_case SQL rows into DTO objects using explicit normalization rules.
9
+ - Timestamp normalization MUST use `timestampFromDriver`.
10
+ - Numeric normalization rules MUST be explicit per contract.
11
+ - Runtime helpers (`ensure*`, `map*`) MUST validate before returning values.
12
+
13
+ ## ALLOWED
14
+ - Command outputs MAY be scalar identifiers or `void` when the contract output is non-DTO.
15
+
16
+ ## PROHIBITED
17
+ - Database I/O in runtime mapping modules.
18
+ - Importing from `tests/` or `tests/generated/`.
19
+ - Defining or editing human-owned contracts in runtime modules.
20
+
21
+ # Mandatory Workflow
22
+ - Runtime changes MUST run tests that cover normalization and validator failure paths.
23
+
24
+ # Hygiene
25
+ - Do not swallow validator errors or silently coerce unsupported values.
26
+
27
+ # References
28
+ - Parent catalog policy: [../AGENTS.md](../AGENTS.md)
@@ -0,0 +1,30 @@
1
+ // Runtime coercions run BEFORE validator schemas.
2
+ // See docs/recipes/mapping-vs-validation.md for pipeline details.
3
+ export function normalizeTimestamp(value: unknown, fieldName?: string): Date {
4
+ const fieldLabel = fieldName?.trim() ? ` for "${fieldName}"` : '';
5
+
6
+ // Preserve valid Date instances while rejecting invalid dates eagerly.
7
+ if (value instanceof Date) {
8
+ if (Number.isNaN(value.getTime())) {
9
+ throw new Error(`Invalid Date value${fieldLabel}.`);
10
+ }
11
+ return value;
12
+ }
13
+
14
+ // Parse driver-returned timestamp strings after trimming transport whitespace.
15
+ if (typeof value === 'string') {
16
+ const trimmed = value.trim();
17
+ if (!trimmed) {
18
+ throw new Error(`Expected a non-empty timestamp string${fieldLabel}.`);
19
+ }
20
+
21
+ const timestamp = Date.parse(trimmed);
22
+ if (Number.isNaN(timestamp)) {
23
+ throw new Error(`Invalid timestamp string${fieldLabel}: "${value}".`);
24
+ }
25
+ return new Date(timestamp);
26
+ }
27
+
28
+ const actualType = value === null ? 'null' : typeof value;
29
+ throw new Error(`Expected Date or timestamp string${fieldLabel}, received ${actualType}.`);
30
+ }
@@ -1 +1,30 @@
1
- export { timestampFromDriver as normalizeTimestamp } from '@rawsql-ts/sql-contract';
1
+ // Runtime coercions run BEFORE validator schemas.
2
+ // See docs/recipes/mapping-vs-validation.md for pipeline details.
3
+ export function normalizeTimestamp(value: unknown, fieldName?: string): Date {
4
+ const fieldLabel = fieldName?.trim() ? ` for "${fieldName}"` : '';
5
+
6
+ // Preserve valid Date instances while rejecting invalid dates eagerly.
7
+ if (value instanceof Date) {
8
+ if (Number.isNaN(value.getTime())) {
9
+ throw new Error(`Invalid Date value${fieldLabel}.`);
10
+ }
11
+ return value;
12
+ }
13
+
14
+ // Parse driver-returned timestamp strings after trimming transport whitespace.
15
+ if (typeof value === 'string') {
16
+ const trimmed = value.trim();
17
+ if (!trimmed) {
18
+ throw new Error(`Expected a non-empty timestamp string${fieldLabel}.`);
19
+ }
20
+
21
+ const timestamp = Date.parse(trimmed);
22
+ if (Number.isNaN(timestamp)) {
23
+ throw new Error(`Invalid timestamp string${fieldLabel}: "${value}".`);
24
+ }
25
+ return new Date(timestamp);
26
+ }
27
+
28
+ const actualType = value === null ? 'null' : typeof value;
29
+ throw new Error(`Expected Date or timestamp string${fieldLabel}, received ${actualType}.`);
30
+ }
@@ -1,21 +1,21 @@
1
- import { parseSmokeOutput, type SmokeOutput } from '../specs/_smoke.spec';
2
- import { normalizeTimestamp } from './_coercions';
3
-
4
- /**
5
- * Validate runtime output against the catalog smoke invariant.
6
- */
7
- export function ensureSmokeOutput(value: unknown): SmokeOutput {
8
- // Normalize driver-dependent timestamp representations before contract validation.
9
- if (isRecord(value) && 'createdAt' in value) {
10
- return parseSmokeOutput({
11
- ...value,
12
- createdAt: normalizeTimestamp(value.createdAt, 'createdAt')
13
- });
14
- }
15
-
16
- return parseSmokeOutput(value);
17
- }
18
-
19
- function isRecord(value: unknown): value is Record<string, unknown> {
20
- return typeof value === 'object' && value !== null;
21
- }
1
+ import { parseSmokeOutput, type SmokeOutput } from '../specs/_smoke.spec';
2
+ import { normalizeTimestamp } from './_coercions';
3
+
4
+ /**
5
+ * Validate runtime output against the catalog smoke invariant.
6
+ */
7
+ export function ensureSmokeOutput(value: unknown): SmokeOutput {
8
+ // Normalize driver-dependent timestamp representations before contract validation.
9
+ if (isRecord(value) && 'createdAt' in value) {
10
+ return parseSmokeOutput({
11
+ ...value,
12
+ createdAt: normalizeTimestamp(value.createdAt, 'createdAt')
13
+ });
14
+ }
15
+
16
+ return parseSmokeOutput(value);
17
+ }
18
+
19
+ function isRecord(value: unknown): value is Record<string, unknown> {
20
+ return typeof value === 'object' && value !== null;
21
+ }
@@ -1,48 +1,41 @@
1
- # src/catalog/specs AGENTS
2
-
3
- This directory defines query and command specifications (contracts).
4
-
5
- ## Runtime classification
6
-
7
- - This is a runtime directory.
8
- - Contents are human-owned contracts, not implementation details.
9
-
10
- ## Ownership (critical)
11
-
12
- - Specs are human-owned.
13
- - Input parameters are part of the domain contract.
14
- - Output DTO shapes are part of the domain contract.
15
- - Cardinality and semantics are part of the domain contract.
16
-
17
- AI MUST NOT:
18
- - infer missing parameters
19
- - widen or narrow DTO shapes
20
- - change nullability or optionality
21
- - change CUD return shape implicitly
22
-
23
- Changes require explicit human instruction.
24
-
25
- ## Validator requirement (required)
26
-
27
- - A validator library (zod or arktype) is always available.
28
- - Every spec MUST define validators for its public inputs/outputs.
29
- - Validators are part of the contract and are human-owned.
30
-
31
- ## Driver variability policy (important)
32
-
33
- Specs MUST NOT require SQL assets to encode driver-specific workarounds.
34
-
35
- - Specs may acknowledge that SQL rows can vary in representation by driver
36
- (e.g. timestamps as Date or string).
37
- - Normalization for driver-dependent values is implemented in catalog runtime,
38
- then validated against the spec.
39
-
40
- Contracts define the final DTO shape.
41
- Runtime defines normalization steps to reach that shape.
42
-
43
- ## What does NOT belong here
44
-
45
- - No executors
46
- - No database connections
47
- - No ZTD internals
48
- - No SQL camelCase aliasing rules (SQL rules live under src/sql)
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src/catalog/specs`.
3
+ - Defines human-owned query/command contracts for params, DTOs, and semantics.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Specs MUST define public input and output validators.
8
+ - Specs MUST preserve declared parameter shape, DTO shape, nullability, cardinality, and semantics.
9
+ - Driver-dependent normalization behavior MUST be specified for runtime handling, not SQL workaround encoding.
10
+
11
+ ## ALLOWED
12
+ - Specs MAY acknowledge driver representation variance when runtime normalization is defined.
13
+
14
+ ## PROHIBITED
15
+ - Inferring or modifying contract shape without explicit instruction.
16
+ - Adding executors, DB connections, or ZTD internal dependencies.
17
+
18
+ # Mandatory Workflow
19
+ - Contract changes MUST run tests that validate success and failure paths.
20
+
21
+ ## Generating initial output types
22
+
23
+ Use `npx ztd model-gen <sql-file> --probe-mode ztd --out src/catalog/specs/<spec-file>.ts` to derive the first QuerySpec scaffold from the local DDL snapshot.
24
+
25
+ - SQL assets are expected to use named parameters (`:name`).
26
+ - `ztd model-gen` is names-first and derives `sqlFile` / `spec id` from the SQL path under `src/sql` by default.
27
+ - `ztd model-gen --probe-mode ztd` is the recommended fast-loop path when the referenced schema already exists in `ztd/ddl/*.sql`.
28
+ - In `--probe-mode ztd`, unqualified table references follow the configured `defaultSchema` / `searchPath` priority from `ztd.config.json`.
29
+ - The CLI default remains `--probe-mode live` for backward compatibility, but project guidance should treat `--probe-mode ztd` as the preferred inner-loop mode.
30
+ - `ztd model-gen --probe-mode ztd` uses the ZTD-owned test database from `ZTD_TEST_DATABASE_URL`.
31
+ - `ztd model-gen --probe-mode live` is an explicit target inspection path and MUST be used with `--url` or a complete `--db-*` flag set.
32
+ - `DATABASE_URL` is a runtime concern and is not read implicitly by `ztd-cli`.
33
+ - Positional placeholders are legacy and should be rewritten whenever possible.
34
+ - Use `--allow-positional` only when a legacy SQL asset cannot be rewritten immediately.
35
+ - Review the generated file before commit: confirm imports, nullability, cardinality, rowMapping key, runtime normalization, and example values.
36
+
37
+ # Hygiene
38
+ - Keep contract definitions isolated from runtime wiring and SQL implementation logic.
39
+
40
+ # References
41
+ - Parent catalog policy: [../AGENTS.md](../AGENTS.md)
@@ -1,21 +1,21 @@
1
- import { type } from 'arktype';
2
-
3
- /**
4
- * Validator invariant contract used to prove runtime validation wiring.
5
- *
6
- * This file is intentionally minimal and domain-agnostic.
7
- */
8
- export const smokeOutputSchema = type({
9
- id: 'number.integer',
10
- createdAt: 'Date'
11
- });
12
-
13
- export type SmokeOutput = ReturnType<typeof smokeOutputSchema>;
14
-
15
- /**
16
- * Parse and validate an unknown runtime payload.
17
- */
18
- export function parseSmokeOutput(value: unknown): SmokeOutput {
19
- smokeOutputSchema.assert(value);
20
- return value as SmokeOutput;
21
- }
1
+ import { type } from 'arktype';
2
+
3
+ /**
4
+ * Validator invariant contract used to prove runtime validation wiring.
5
+ *
6
+ * This file is intentionally minimal and domain-agnostic.
7
+ */
8
+ export const smokeOutputSchema = type({
9
+ id: 'number.integer',
10
+ createdAt: 'Date'
11
+ });
12
+
13
+ export type SmokeOutput = ReturnType<typeof smokeOutputSchema>;
14
+
15
+ /**
16
+ * Parse and validate an unknown runtime payload.
17
+ */
18
+ export function parseSmokeOutput(value: unknown): SmokeOutput {
19
+ smokeOutputSchema.assert(value);
20
+ return value as SmokeOutput;
21
+ }
@@ -1,20 +1,20 @@
1
- import { z } from 'zod';
2
-
3
- /**
4
- * Validator invariant contract used to prove runtime validation wiring.
5
- *
6
- * This file is intentionally minimal and domain-agnostic.
7
- */
8
- export const smokeOutputSchema = z.object({
9
- id: z.number().int(),
10
- createdAt: z.date()
11
- });
12
-
13
- export type SmokeOutput = z.infer<typeof smokeOutputSchema>;
14
-
15
- /**
16
- * Parse and validate an unknown runtime payload.
17
- */
18
- export function parseSmokeOutput(value: unknown): SmokeOutput {
19
- return smokeOutputSchema.parse(value);
20
- }
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Validator invariant contract used to prove runtime validation wiring.
5
+ *
6
+ * This file is intentionally minimal and domain-agnostic.
7
+ */
8
+ export const smokeOutputSchema = z.object({
9
+ id: z.number().int(),
10
+ createdAt: z.date()
11
+ });
12
+
13
+ export type SmokeOutput = z.infer<typeof smokeOutputSchema>;
14
+
15
+ /**
16
+ * Parse and validate an unknown runtime payload.
17
+ */
18
+ export function parseSmokeOutput(value: unknown): SmokeOutput {
19
+ return smokeOutputSchema.parse(value);
20
+ }
@@ -0,0 +1,32 @@
1
+ import type { SqlClient } from './sql-client';
2
+
3
+ /**
4
+ * Adapt a `pg`-style queryable (Client or Pool) into a SqlClient.
5
+ *
6
+ * pg's `query()` returns `QueryResult<T>` with a `.rows` property.
7
+ * This helper unwraps the result so it satisfies the `SqlClient` contract.
8
+ *
9
+ * Usage:
10
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
11
+ * const client = fromPg(pool);
12
+ * const users = await client.query<{ id: number }>('SELECT id ...', []);
13
+ */
14
+ export function fromPg(
15
+ queryable: {
16
+ query(text: string, values?: readonly unknown[]): Promise<{ rows: Record<string, unknown>[] }>;
17
+ }
18
+ ): SqlClient {
19
+ return {
20
+ query<T extends Record<string, unknown> = Record<string, unknown>>(
21
+ text: string,
22
+ values?: readonly unknown[] | Record<string, unknown>
23
+ ): Promise<T[]> {
24
+ if (values != null && !Array.isArray(values)) {
25
+ throw new Error('fromPg adapter does not support named parameter objects; use positional parameter arrays');
26
+ }
27
+ return queryable
28
+ .query(text, values as readonly unknown[])
29
+ .then((result) => result.rows as T[]);
30
+ }
31
+ };
32
+ }
@@ -1,24 +1,24 @@
1
- /**
2
- * Promise that resolves to the array of rows produced by an SQL query.
3
- * @template T Shape of each row yielded by the SQL client.
4
- * @example
5
- * const rows: SqlQueryRows<{ id: number; name: string }> = client.query('SELECT id, name FROM users');
6
- */
7
- export type SqlQueryRows<T> = Promise<T[]>;
8
-
9
- /**
10
- * Minimal SQL client interface required by the repository layer.
11
- *
12
- * - Production: adapt this interface to your preferred driver (pg, mysql2, etc.) and normalize the results to `T[]`.
13
- * - Tests: replace the implementation with a mock, a fixture helper, or an adapter that follows this contract.
14
- *
15
- * Connection strategy note:
16
- * - Prefer a shared client per worker process for better performance.
17
- * - Do not share a live client across parallel workers without proper synchronization.
18
- */
19
- export type SqlClient = {
20
- query<T extends Record<string, unknown> = Record<string, unknown>>(
21
- text: string,
22
- values?: readonly unknown[] | Record<string, unknown>
23
- ): SqlQueryRows<T>;
24
- };
1
+ /**
2
+ * Promise that resolves to the array of rows produced by an SQL query.
3
+ * @template T Shape of each row yielded by the SQL client.
4
+ * @example
5
+ * const rows: SqlQueryRows<{ id: number; name: string }> = client.query('SELECT id, name FROM users');
6
+ */
7
+ export type SqlQueryRows<T> = Promise<T[]>;
8
+
9
+ /**
10
+ * Minimal SQL client interface required by the repository layer.
11
+ *
12
+ * - Production: adapt this interface to your preferred driver (pg, mysql2, etc.) and normalize the results to `T[]`.
13
+ * - Tests: replace the implementation with a mock, a fixture helper, or an adapter that follows this contract.
14
+ *
15
+ * Connection strategy note:
16
+ * - Prefer a shared client per worker process for better performance.
17
+ * - Do not share a live client across parallel workers without proper synchronization.
18
+ */
19
+ export type SqlClient = {
20
+ query<T extends Record<string, unknown> = Record<string, unknown>>(
21
+ text: string,
22
+ values?: readonly unknown[] | Record<string, unknown>
23
+ ): SqlQueryRows<T>;
24
+ };
@@ -0,0 +1,15 @@
1
+ # Package Scope
2
+ - Applies to `packages/ztd-cli/templates/src/domain`.
3
+ - Defines the domain layer for WebAPI-oriented scaffolds.
4
+
5
+ # Policy
6
+ ## REQUIRED
7
+ - Domain code MUST remain independent from SQL, DDL, QuerySpecs, and generated test artifacts.
8
+ - Domain modules MUST express business rules in transport-agnostic types.
9
+
10
+ ## PROHIBITED
11
+ - Importing `src/sql`, `src/catalog`, `ztd`, or telemetry scaffolds into domain code.
12
+ - Encoding HTTP or persistence concerns in domain rules.
13
+
14
+ # Mandatory Workflow
15
+ - Domain changes MUST run the relevant typecheck and unit tests.
@@ -0,0 +1,6 @@
1
+ # Domain
2
+
3
+ Domain types, invariants, and business rules live here.
4
+
5
+ - Do not import SQL files, QuerySpecs, or ZTD-generated artifacts into this layer.
6
+ - Prefer ports or plain interfaces for dependencies on application or infrastructure code.