@rawsql-ts/ztd-cli
v0.24.3
Published
DB-agnostic scaffolding and DDL helpers for Zero Table Dependency projects
Readme
@rawsql-ts/ztd-cli
ztd-cli is a SQL-first CLI for feature-first application development.
Highlights
- DDL is the source of truth, and
pg_dumpoutput can be used to bootstrap it. - SQL lives as files, co-located with each feature.
- Development starts from SQL changes, then moves through tests and repair loops.
- ZTD-format SQL tests are the standard, and SQL tuning has a dedicated path.
- Migration artifacts are generated for review, not applied automatically.
- No extra DSL is required.
- VSA-style feature-local SQL layouts are supported.
Quickstart
Run these in order.
npm install -D @rawsql-ts/ztd-cli vitest typescript
npx ztd init --starter
# starter scaffold generates compose.yaml, starter DDL, config, and test stubs
npx ztd agents init
cp .env.example .env
# edit ZTD_DB_PORT=5433 if needed
npx ztd ztd-config
docker compose up -d
npx vitest runPowerShell:
npm install -D @rawsql-ts/ztd-cli vitest typescript
npx ztd init --starter
# starter scaffold generates compose.yaml, starter DDL, config, and test stubs
npx ztd agents init
Copy-Item .env.example .env
# edit ZTD_DB_PORT=5433 if needed
npx ztd ztd-config
docker compose up -d
npx vitest runFeature Test Debugging
Port Already In Use
If port 5432 is already in use, change ZTD_DB_PORT in .env and then verify recovery with:
docker compose up -d
npx vitest runZTD Runtime Debugging
- If an AI-authored ZTD test fails, do not assume the prompt or case file is the only problem; check whether
ztd-cliorrawsql-tschanged the manifest or rewrite path. - If you see
user_id: null, compare the direct databaseINSERT ... RETURNING ...result with the ZTD result and inspect.ztd/generated/ztd-fixture-manifest.generated.tsfirst. - If a dogfood workspace is meant to reflect a source change, verify that it resolves
rawsql-tsfrom the local source tree rather than a registry copy. - If
afterDblooks wrong, remember that the verifier uses subset-based per-row matching, ignores row order, treats rows as an unordered multiset, and may omit volatile columns from the persistent case.
Create the Users Insert Feature
Use this after Quickstart.
The DDL is in db/ddl/public.sql.
Run this first:
npx ztd feature scaffold --table users --action insertScaffold the users-insert feature with co-located SQL, specs, and a thin tests entrypoint.
The starter smoke sample now mirrors the same feature-first layout: src/features/smoke/spec.ts, src/features/smoke/tests/smoke.entryspec.test.ts, and src/features/smoke/queries/smoke/tests/smoke.queryspec.ztd.test.ts.
After you finish the SQL and DTO edits, run npx ztd feature tests scaffold --feature <feature-name> to refresh src/features/<feature-name>/queries/<query-name>/tests/generated/TEST_PLAN.md and analysis.json. That command also creates the thin Vitest entrypoint src/features/<feature-name>/queries/<query-name>/tests/<query-name>.queryspec.ztd.test.ts, which stays checked in as a small adapter around the fixed app-level harness. generated/* is CLI-owned and refreshable, cases/* is human/AI-owned and kept, and the thin entrypoint is kept. ZTD here means query-local cases that execute through the fixed app-level harness against the real database engine, not a mocked executor. Persistent case files belong in src/features/<feature-name>/queries/<query-name>/tests/cases/. If ztd-config has already run, use .ztd/generated/ztd-fixture-manifest.generated.ts as the source for tableDefinitions and any fixture-shape hints the case needs. beforeDb and afterDb are schema-qualified pure fixture skeletons. Use validation-only cases for boundary checks and DB-backed cases for the success path. Keep the feature-root src/features/<feature-name>/tests/<feature-name>.entryspec.test.ts for mock-based boundary tests. afterDb is subset-based per row, rows are treated as an unordered multiset, and row order itself is ignored. The verifier truncates tables named in beforeDb with restart identity cascade before seeding. After the cases are filled, run npx vitest run src/features/<feature-name>/queries/<query-name>/tests/<query-name>.queryspec.ztd.test.ts to execute the ZTD query test.
Troubleshooting
- If a DB-backed ZTD case returns
user_id: null, check the fixture manifest and rewrite path before weakening the case. - Compare the direct database
INSERT ... RETURNING ...result with the ZTD result so you can separate a DB issue from a manifest or rewrite issue. - If the workspace is meant to reflect a source change, verify it resolves
rawsql-tsfrom the local source tree instead of a registry copy. - When
afterDbfails, remember that the comparison is subset-based, row order is ignored, and volatile columns such as timestamps may be intentionally omitted from the fixture.
Write ZTD-format cases for the spec.
Keep the persistent case files in `src/features/<feature>/queries/<query>/tests/cases/`.
Use `src/features/<feature>/queries/<query>/tests/generated/TEST_PLAN.md` and `analysis.json` as the source of truth.
Do not put returned columns into the input fixture; only assert them after the DB-backed case returns.
The validation cases may stay at the entry boundary, but the success case must run through the fixed app-level ZTD runner and verify the returned result.
If the returned result is `null`, stop and fix the scaffold or DDL instead of weakening the case.
Before writing the success-path assertion, inspect the current SQL and QuerySpec. If the scaffold does not actually return the expected result shape, report that mismatch instead of inventing fixture data or schema overrides.
Do not apply migrations automatically.Finish by running:
npx vitest runIf you want a deeper walkthrough, keep that in the linked guides instead of expanding this README.
Commands
| Command | Purpose |
|---|---|
| ztd init --starter | Scaffold the starter project with smoke, DDL, compose, and local Postgres wiring. |
| ztd feature scaffold --table <table> --action <insert/update/delete/get-by-id/list> | Scaffold a feature-local CRUD/SELECT slice with SQL, spec.ts, README, and a thin tests entrypoint. |
| ztd feature tests scaffold --feature <feature-name> | Refresh tests/generated/TEST_PLAN.md and analysis.json, create the thin <query-name>.queryspec.ztd.test.ts Vitest entrypoint when missing, and keep tests/cases/ as human/AI-owned persistent cases. |
| ztd agents init | Add the optional Codex bootstrap files. |
| ztd ztd-config | Regenerate TestRowMap and runtime fixture metadata from DDL without Docker. |
| ztd lint | Lint SQL against a temporary Postgres. |
| ztd model-gen | Generate spec scaffolding from SQL assets. |
| ztd query uses | Find impacted SQL before changing a table or column. |
| ztd query match-observed | Rank likely source SQL assets from observed SELECT text. |
| ztd query sssql scaffold / ztd query sssql refresh | Author and refresh SQL-first optional filter branches. |
| ztd ddl pull / ztd ddl diff | Inspect a target and prepare migration SQL. |
| ztd perf init / ztd perf run | Run the tuning loop for index or pipeline investigation. |
| ztd describe | Inspect commands in machine-readable form. |
Glossary
| Term | Meaning | |---|---| | ZTD | Zero Table Dependency - test against a real database engine without creating or mutating application tables. | | DDL | SQL schema files that act as the source of truth for type generation. | | TestRowMap | Generated TypeScript types that describe row shape from local DDL. | | Query spec | Contract object that ties a SQL asset file to parameter and output types. | | SSSQL | SQL-first optional-filter authoring style that keeps the query truthful and lets the runtime prune only what it must. |
Further Reading
User Guides
- SQL-first End-to-End Tutorial - starter flow, repair loops, and scenario-specific CLI guidance
- SQL Tool Happy Paths - choose between query plan, perf, query uses, and telemetry
- Dynamic Filter Routing - decide between DynamicQueryBuilder filters and SSSQL branches
- ztd.config.json Top-Level Settings - where schema resolution lives and how to read the generated config
- Perf Tuning Decision Guide - index tuning vs pipeline tuning
- JOIN Direction Lint Specification - readable FK-aware JOIN guidance
- Repository Telemetry Setup - how to edit the scaffold, emit logs, and investigate with
queryId - Observed SQL Investigation - how to use
ztd query match-observedwhenqueryIdis missing - ztd-cli Agent Interface - machine-readable command surface
Advanced User Guides
- Published-Package Verification Before Release - pack and smoke-test the published-package path
- What Is SSSQL? - the shortest intro to truthful optional-filter SQL
- SSSQL for Humans - why SSSQL exists and where it fits in the toolchain
- ztd-cli Telemetry Philosophy - when to enable telemetry and why it stays opt-in
- ztd-cli Telemetry Policy - which event fields are allowed and how redaction works
- ztd-cli Telemetry Export Modes - how to send telemetry to console, debug, file, or OTLP
- Observed SQL Matching - reverse lookup for missing
queryId, with best-effort ranking and skip/warning reporting - Multiple DB Clients in One Workflow - separate DB contexts, one workflow, and side-by-side
SqlClientbindings
Developer Guides
- Local-Source Dogfooding - unpublished local checkout workflow
- Generated-Project Verification Before Merge - fresh-project scaffold and layout smoke check for
ztd initandfeature scaffold - Codex Bootstrap Verification - reviewer-checkable fresh-project verification for
ztd agents init - ztd-cli spawn EPERM Investigation - reviewer-checkable root-cause investigation for the local Vitest startup blocker
- ztd Onboarding Dogfooding - reviewer-checkable README Quickstart and tutorial verification for the customer-facing onboarding path
License
MIT
