# Reylo

> Reylo is an engine that infers type information from SQL queries, read from your live database. TypeScript uses its types as you write, and its CLI writes and runs your migrations.

An engine that infers the types of your SQL queries

Reylo is a type inference engine for SQL. It lets you import SQL queries into your TypeScript files with full type information.

**Postgres**:

```sql
create extension if not exists pg_jsonschema;

create type user_role as enum ('admin', 'member');

create table users (
  id serial primary key,                     -- at most one row per id
  name text not null,                        -- name: string
  email text unique,                         -- email: string | null
  role user_role not null default 'member',  -- role: "admin" | "member"
  tags text[] not null default '{}',         -- tags: (string | null)[]
  created_at timestamptz                     -- created_at: string
    not null default now()
);

create table posts (
  id serial primary key,
  user_id integer not null references users (id),
  title text not null,
  body text,
  published_at timestamptz
);

create table events (
  id serial primary key,
  kind text not null
    check (kind in ('click', 'key')),        -- kind: "click" | "key"
  payload jsonb not null                     -- payload: what the schema admits
    check (jsonb_matches_schema('{
      "type": "object",
      "properties": {
        "x": { "type": "integer" },
        "y": { "type": "integer" },
        "code": { "type": "string" }
      }
    }', payload))
);
```

**MySQL**:

```sql
create table users (
  id int auto_increment primary key,          -- at most one row per id
  name text not null,                         -- name: string
  email varchar(255) unique,                  -- email: string | null
  role enum('admin', 'member')                -- role: "admin" | "member"
    not null default 'member',
  tags json not null default (json_array()),  -- tags: Json
  created_at datetime                         -- created_at: string, UTC
    not null default (utc_timestamp())
);

create table posts (
  id int auto_increment primary key,
  user_id int not null,
  title text not null,
  body text,
  published_at datetime,
  foreign key (user_id) references users (id)
);

create table events (
  id int auto_increment primary key,
  kind text not null,
  payload json not null                       -- payload: what the schema admits
    check (json_schema_valid('{
      "type": "object",
      "properties": {
        "x": { "type": "integer" },
        "y": { "type": "integer" },
        "code": { "type": "string" }
      }
    }', payload))
);
```

**SQLite**:

```sql
create table users (
  id integer primary key,               -- at most one row per id
  name text not null,                   -- name: string
  email text unique,                    -- email: string | null
  role text not null default 'member'   -- role: string
    check (role in ('admin', 'member')),
  tags text not null default '[]',      -- tags: string, JSON text
  created_at text                       -- created_at: string, UTC
    not null default (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
) strict;

create table posts (
  id integer primary key,
  user_id integer not null references users (id),
  title text not null,
  body text,
  published_at text
) strict;

create table events (
  id integer primary key,
  kind text not null,
  payload text not null
) strict;
```

**ClickHouse**:

```sql
CREATE TABLE users (
  id UInt32,                             -- no unique key: say LIMIT 1
  name String,                           -- name: string
  email Nullable(String),                -- email: string | null
  role Enum8('admin' = 1, 'member' = 2)  -- role: "admin" | "member"
    DEFAULT 'member',
  tags Array(String),                    -- tags: string[]
  created_at DateTime64(3, 'UTC')        -- created_at: string
    DEFAULT now64(3)
) ENGINE = MergeTree ORDER BY id;

CREATE TABLE posts (
  id UInt32,
  user_id UInt32,
  title String,
  body Nullable(String),
  published_at Nullable(DateTime64(3, 'UTC'))
) ENGINE = MergeTree ORDER BY id;

CREATE TABLE events (
  id UInt32,
  kind Enum8('click' = 1, 'key' = 2),    -- kind: "click" | "key"
  payload String                         -- payload: string, JSON text
) ENGINE = MergeTree ORDER BY id;
```

If you have a query like this:

`get_author.sql`:

```sql
-- hover a table to see its declaration
select u.id, u.name, count(p.id)::int as post_count
from users u
left join posts p on p.user_id = u.id
where u.id = :id::int
group by u.id
```

You can import it like this. Hover over `author` to see the inferred type!

`main.ts`:

```ts
import postgres from "postgres";
import getAuthor from "./get_author.sql";

const db = postgres(process.env.DATABASE_URL);
const author = await getAuthor.run(db, { id: 1 });
```

> **Content mappers.** Reylo relies on the Content Mapper API (introduced in TypeScript 7.1) to let you directly import `.sql` files into TypeScript.

Reylo infers type information about both the parameters and the return types, including [nullable and optional parameters](https://reylo.dev/docs/inference#nullable-parameters). Throughout the docs, you will see "inference cards" like this where you can quickly see both a query and its inferred type information.

Postgres, MySQL and SQLite:

```sql
select id, name, email from users where id = :id::int
```

```ts
// Params
{ id: number }
// Returns
{ id: number; name: string; email: string | null } | null
```

ClickHouse:

```sql
SELECT id, name, email FROM users WHERE id = {id:UInt32} LIMIT 1
```

```ts
// Params
{ id: number }
// Returns
{ id: number; name: string; email: string | null } | null
```

Reylo gets type information about a query by sending it to a *live dev database* and getting back the database's own type descriptors. It then maps these to TypeScript types.

Reylo is the perfect option for the agent era:

- 💪 **The full power of SQL** — any syntax, function or operator your database supports
- 🌵 **DRY** — no redundant hand-written types
- 🎯 **Checked against your database** — a misspelled column is a compile error
- 🤖 **Agent-friendly** — DRY code is more maintainable for agents
- 🧩 **Postgres, MySQL, SQLite and ClickHouse** — Neon, PlanetScale, Cloudflare D1, Turso and more
- 🔌 **Your own driver** — `pg`, postgres.js, `mysql2`, `better-sqlite3`, `@clickhouse/client` and [more](https://reylo.dev/docs/typescript#run) in TypeScript

---

> **Looking for a schema migration tool?.** Try Reylo Migrate. Model your schema in `schema.sql`, then generate migrations or write your own.
> 
> [Read the Reylo Migrate docs](https://reylo.dev/docs/migrate)
