# Athena CLI

TypeScript CLI for generating SQL and CQL seed files alongside optional constraint artifacts directly from Supabase schema metadata.

## Setup

```bash
cd cli
npm install
```

## Build

```bash
npm run build
```

## Usage

```bash
npm start -- --command=seed [options]
```

## Commands

- `seed` — default command that fetches Supabase schema metadata and writes `seed.sql`, `seed.cql`, `views.sql`, and `extensions.sql`.
  - `--sql=PATH` — destination for PostgreSQL `CREATE TABLE` statements (default: `seed.sql`).
  - `--cql=PATH` — destination for Cassandra CQL statements (default: `seed.cql`).
  - `--include-constraints-sql` — also emit `constraints.sql`, `constraints_nofk.sql`, and `constraints_fk.sql`.
  - `--if-not-exist-table` — wrap SQL tables in `CREATE TABLE IF NOT EXISTS`.
  - `--supabase-url=URL` / `--supabase-key=KEY` — override Supabase credentials for RPC calls.
  - `--supabase-config=PATH` — load a JSON file containing credentials and optional `database` block.
  - `--db-config-output=PATH` — dump the parsed `database` object when using `--supabase-config`.

- `constraints` — generate `constraints.sql`, `constraints_nofk.sql`, and `constraints_fk.sql` using the Supabase schema RPC.
  - accepts the same Supabase credential flags as `seed`.

- `table-registry` — regenerate `lib/db/table-registry.ts` from your Drizzle schema.
  - `--registry-schema-path=PATH` — override the schema source (default: `lib/db/schema.ts`).
  - `--registry-path=PATH` — destination for the registry file (default: `lib/db/table-registry.ts`).

- `help` — render this help text automatically (also triggered via `--help` or `-h`).

## Environment variables

- `ATHENA_SUPABASE_URL` / `SUPABASE_URL`
- `ATHENA_SUPABASE_ANON_KEY` / `SUPABASE_ANON_KEY`

## Guide & Command Breakdown

1. **Provision credentials** – set the Supabase RPC credentials either via env vars, `--supabase-url`/`--supabase-key`, or `--supabase-config`. When a config file contains a `database` block, the CLI can emit it through `--db-config-output` for reuse.
2. **Execute the default `seed` command** – `npm start -- --command=seed [options]` fetches schema JSON, writes `seed.sql`, `seed.cql`, `views.sql`, `extensions.sql`, and (when `--include-constraints-sql` is provided) the constraint scripts.
3. **Run `constraints` only** – `npm start -- --command=constraints` produces the constraint SQL artifacts using the same Supabase metadata without re-emitting the table or view definitions.
4. **Generate the Drizzle table registry** – `npm start -- --command=table-registry` parses `lib/db/schema.ts`, catalogs every exported `pgTable`, and emits `lib/db/table-registry.ts`. Override the source or target path with `--registry-schema-path` / `--registry-path` as needed.
5. **Discover commands** – `npm start -- --command=help`, `npm start -- --help`, or `npm start -- -h` prints the live manifest of commands/flags read from `cli/commands/index.ts`.

Each command lives under `cli/commands/<name>/index.ts`, reuses shared helpers from `cli/utils/*`, and is registered through `cli/commands/index.ts`. The entrypoint in `cli/main.ts` simply dispatches to the requested handler based on `cli/utils/args.ts`, keeping the project modular and easy to extend.

## Supabase JSON schema expectations

The CLI calls the `get_full_schema_json` RPC and expects a response with `schema` and `extensions` arrays. Tables must include `columns` and may optionally include `constraints`.

## Notes

- Database configs supplied via `--supabase-config` are emitted only when the file contains a `database` object.
- Output files are overwritten in place.
