# CLI and Markdown contract

`mddb --help` lists the installed commands. All file arguments refer to one UTF-8 Markdown file. JSON output uses real booleans and nulls.

| Command | Purpose |
| --- | --- |
| `init FILE` | Create a starter; refuse an existing file |
| `tables FILE` | Discover table names, columns and inferred types |
| `query FILE SQL --format json` | Read-only SQLite projection; also table, csv and md output |
| `exec FILE SQL --dry-run` | Diff for one-table INSERT/UPSERT, UPDATE, DELETE or ADD COLUMN |
| `inspect FILE --format json` | Discover namespace paths |
| `get FILE PATH --format json` | Read metadata, section, object, table or checklist projection |
| `set FILE PATH VALUE --json --dry-run` | Preview a JSON value at a writable metadata/section/object path |
| `list check FILE NAME TEXT` | Complete an exact named checklist item; also uncheck and add |
| `dump FILE` | Complete namespace projection as JSON |
| `render FILE --template TEMPLATE -o OUTPUT` | Embed a static data snapshot in HTML |
| `serve FILE --template TEMPLATE --port PORT` | Read-only live view, loopback by default |
| `workspace --directory DIRECTORY` | Local block UI and revision-bound write API |
| `skill path` / `skill install --target DIRECTORY` | Locate or install the bundled Skill |

Remove `--dry-run` to save. `set` defaults to a string; `--json` parses JSON. `list add` accepts `--done`. Exit codes are 0 success, 1 runtime/validation failure, 2 invalid arguments. The CLI takes a fresh snapshot for each invocation; a previous dry-run does not pin a later invocation's revision. Use the resource API when the write must be bound to a specific preview snapshot.

## Tables

Ordinary pipe tables work without markers. The nearest heading supplies a name; the CLI reports disambiguated names. A single table also has alias `t` and an alias based on the file stem. Quote identifiers with spaces. Explicit names, primary keys and types use an adjacent comment:

```markdown
<!-- mddb:table name=tasks pk=id schema="id:int,title:text,status:text" -->
| id | title | status |
| --- | --- | --- |
| 1 | Review | TODO |
```

Types: int/integer, real/float, text, bool/boolean. Otherwise infer from non-empty cells. Empty cells become SQL NULL; non-finite numbers, BLOB and NUL cannot be written. Escaped pipes/backslashes and line breaks round-trip through supported encodings. Updates preserve all bytes outside the target table, but reformat that table. SQL constraints added only in the temporary SQLite schema do not automatically persist to future reads.

## Other namespaces

- YAML Frontmatter is addressed as `meta.status` or `meta.release.version`.
- A section between `<!-- mddb:section name=summary -->` and `<!-- /mddb:section -->` is addressed as `sections.summary`.
- A fenced JSON/YAML block preceded by `<!-- mddb:object name=goals format=json -->` is addressed as `objects.goals`, including nested keys and numeric array indices such as `objects.goals.items.0.name`.
- A checklist preceded by `<!-- mddb:list name=signoff -->` is a named CLI checklist. Ordinary lists without that marker are managed through the workspace block API instead.

YAML is a documented subset for configuration data, not a full YAML implementation: mappings, sequences, scalar strings/numbers/booleans/null. Unsupported forms fail explicitly. A malformed optional namespace does not make the older table-only query interface parse that namespace.

## HTML snapshots

Templates must contain exactly one closed `<script id="mddb-data" type="application/json"></script>`. MDDB embeds the namespace as escaped JSON. Template code controls display; template scripts are trusted local code. Export refuses to overwrite the source/template, including hard-link aliases. Generated reports have snapshot data; live preview rereads the file.
