# CLI

> Commands for analyzing queries, generating code, running ad-hoc SQL, and starting the local playground, plus the programmatic analyze API.

The commands for analyzing, generating and running SQL

Run these commands with `npx reylo`, or install the [`reylo` CLI](https://reylo.dev/docs/cli) globally. Each command reads the database your project names, as the type check does.

## `reylo analyze`

Prints what Reylo [infers](https://reylo.dev/docs/inference) for each statement. Given this query:

`queries/get_user.sql`:

```sql
-- One user, by id.
select id, email, bio from users where id = :id
```

Analyze it:

```terminal
$ reylo analyze queries/get_user.sql
{
  "$schema": "https://reylo.dev/typedefs/preview.json",
  "dialect": { "name": "postgres", "version": "18.6" },
  "sql": "-- One user, by id.\nselect id, email, bio from users where id = $1\n",
  "kind": "select",
  "comment": "One user, by id.",
  "params": [
    { "name": "id", "type": { "kind": "integer", "bits": 64, "label": "int8" } }
  ],
  "result": {
    "kind": "table",
    "cardinality": "at_most_one",
    "columns": [
      {
        "name": "id",
        "type": { "kind": "integer", "bits": 64, "label": "int8" }
      },
      {
        "name": "email",
        "type": {
          "kind": "text",
          "code_points": [[1, 1114111]],
          "size": { "max": 255 },
          "label": "varchar"
        }
      },
      {
        "name": "bio",
        "type": {
          "kind": "text",
          "code_points": [[1, 1114111]],
          "label": "text"
        },
        "nullable": true,
        "comment": "Shown on the profile page"
      }
    ]
  },
  "capabilities": { "tables": { "public.users": "read" } },
  "diagnostics": []
}
```

Each field of the result:

| Field | What it holds |
| --- | --- |
| `file` | The file the statement came from, when the command reads several |
| `$schema` | The format's version, as the URL of its JSON Schema |
| `dialect` | The database, and its server's version |
| `sql` | The statement as the database runs it, with the database's own placeholders |
| `kind` | What the statement does: `select`, `insert`, `update`, `delete`, `merge` or `other` |
| `comment` | The comment before the statement |
| `params` | Each parameter's name and [type](https://reylo.dev/docs/typedefs) |
| `result` | Each column's name and type, and the cardinality: how many rows the statement returns |
| `capabilities` | The tables the statement reads or writes |
| `diagnostics` | What the database reports where it refuses the statement |

All type information follows the [Reylo Type Defs](https://reylo.dev/docs/typedefs) format.

> **Preview.** [Reylo Type Defs](https://reylo.dev/docs/typedefs) is in preview until 1.0.0, so fields may still change. It's designed for building custom code generators and tooling on top of Reylo.

Pass files, directories or globs. Use `-` for standard input, or `--sql` for one statement:

```sh
reylo analyze queries/get_user.sql
reylo analyze queries
reylo analyze 'queries/**/*.sql'
reylo analyze - < queries/get_user.sql
reylo analyze --sql 'select count(*) as n from users'
```

Piped, each statement prints as one line of JSON:

```terminal
$ reylo analyze --sql 'select count(*) as n from users' | cat
{"$schema":"https://reylo.dev/typedefs/preview.json","dialect":{"name":"postgres","version":"18.6"},"sql":"select count(*) as n from users","kind":"select","params":[],"result":{"kind":"table","cardinality":"one","columns":[{"name":"n","type":{"kind":"integer","bits":64,"label":"int8"}}]},"capabilities":{"tables":{"public.users":"read"}},"diagnostics":[]}
```

With `--format typescript`, it prints the parameters and what `.run()` resolves to:

```terminal
$ reylo analyze queries/get_user.sql --format typescript
type Params = { id: number | bigint | string };
type Returns = {
  id: bigint;
  email: string;
  /** Shown on the profile page */
  bio: string | null;
} | null;
```

The `--format yaml` flag prints a YAML document per statement.

A statement the database refuses prints its error on stderr, and the command exits with 1:

```terminal
$ reylo analyze queries/broken.sql > /dev/null
queries/broken.sql:4:7: column u.emial does not exist (42703)
  where u.emial = :email
        ^^^^^^^
```

If Reylo cannot reach the database or read the input, it exits with 2.

## Custom codegen

Build your own code generator or tooling in any language on top of Reylo's type descriptors.

### From the CLI

Pipe `reylo analyze` into your generator script. When reading several files, each line is one JSON descriptor with `file` set:

```sh
reylo analyze 'queries/**/*.sql' | python3 scripts/codegen.py
```

### From JavaScript

Call `analyze()` from `reylo` in Node 24+ or Bun to inspect statements inside a script or build step:

```ts
import { analyze, type TypeDefs } from "reylo";

const desc: TypeDefs.StatementDescriptor = await analyze({
  sql: "select id, email, bio from users where id = :id",
});

if (desc.diagnostics.length > 0) {
  throw new Error(desc.diagnostics[0].message);
}

console.log(desc.sql);
console.log(desc.params);
console.log(desc.result.cardinality, desc.result.columns);
```

How `analyze()` behaves:

- **Configuration.** Reads `direct_url` from `.reylo.yaml` or `DATABASE_URL`, along with `allow_untyped_params`. Pass `url` to override it.
- **Live connections.** Calls with the same URL share one connection, and each call reads the current schema afresh.
- **Diagnostics.** A refused statement returns empty `result.columns` and puts `message`, `start`, `end` and optional `code` in `diagnostics`. The promise rejects only when Reylo cannot connect or analyze at all.
- **Bundle size.** Apps that never import `analyze()` bundle none of it.

## `reylo generate`

Writes every query into one TypeScript file ([Codegen](https://reylo.dev/docs/typescript#codegen)):

```terminal
$ npx reylo generate typescript 'src/queries/**/*.sql' --out src/queries.ts
✓ Wrote src/queries.ts
```

## `reylo run`

Runs SQL on the database as written, outside any migration's history.

It runs the statements you pass as text, then each `--file`, in order:

```terminal
$ reylo run 'alter table users add column bio text'
✓ alter table users add column bio text
```

Pass `--url` to target another database. A statement that returns rows prints them, up to 50:

```terminal
$ reylo run 'select id, email, bio from users'
✓ select id, email, bio from users
  id  email              bio
  ──  ─────────────────  ───────────────────────
   1  ada@example.com    Wrote the first program
   2  grace@example.com  null
  Returned 2 rows
```

On a SQLite file that isn't there, it refuses rather than make an empty database.

## `reylo play`

Serves the playground on this machine.

The page is a query editor. It shows Reylo's analysis of the query against your database at every keystroke, without running it. Pass `--port` to change the default port 4983. Use `--no-open` to leave the browser closed for a script or an agent. Either reads the same analysis as JSON:

```text
http://127.0.0.1:4983/?q=<sql>         the page, with a query in it
GET  /api/analyze?q=<sql>               a query's Params, Returns and diagnostics
POST /api/analyze  {"sql": "<sql>"}     the same
GET  /api/schema                        the tables, their columns, functions and types
```
