Skip to main content
Glama
README.md
# dbml.db

DBML as a service for AI agents. Build, validate, review and convert database
schemas written in [DBML](https://dbml.dbdiagram.io/docs) over an HTTP API, an
MCP server and a CLI.

- Site: https://dbml.db.pit.world (Moshpit name `dbml.db`)
- MCP (streamable HTTP): https://dbml.db.pit.world/mcp
- Docs: https://dbml.db.pit.world/docs · [llms.txt](https://dbml.db.pit.world/llms.txt)
- CLI: `npm i -g @profullstack/dbml`

Free, no account, rate limited per caller. No AI model is called: every answer is
deterministic.

## What it does

| | API | MCP tool | CLI |
| --- | --- | --- | --- |
| Validate (errors with line/column) | `POST /api/v1/validate` | `validate_dbml` | `dbml validate` |
| Parse to a JSON model | `POST /api/v1/parse` | `parse_dbml` | `dbml parse` |
| Summary (counts, outline) | `POST /api/v1/summary` | `summarize_dbml` | `dbml summary` |
| ER diagram (SVG) | `POST /api/v1/diagram` | `diagram_dbml` | `dbml diagram` |
| Review (lint, score 0-100) | `POST /api/v1/review` | `review_dbml` | `dbml review`, `dbml watch` |
| Canonical format | `POST /api/v1/format` | `format_dbml` | `dbml format` |
| DBML to SQL (postgres, mysql, mssql, oracle) | `POST /api/v1/dbml-to-sql` | `dbml_to_sql` | `dbml to-sql` |
| SQL to DBML (postgres, mysql, mssql, oracle, snowflake, schemarb, json) | `POST /api/v1/sql-to-dbml` | `sql_to_dbml` | `dbml from-sql` |
| Review rules | `GET /api/v1/rules` | `list_review_rules` | `dbml rules` |
| DBML cheat sheet | `GET /api/v1/syntax` | `dbml_syntax` | `dbml syntax` |

```sh
curl -s https://dbml.db.pit.world/api/v1/review -H 'content-type: application/json' \
  -d '{"dbml":"Table users { id int [pk]\n email varchar }"}'

curl -s 'https://dbml.db.pit.world/api/v1/dbml-to-sql?dialect=mysql' \
  -H 'content-type: text/plain' --data-binary @schema.dbml

claude mcp add --transport http dbml https://dbml.db.pit.world/mcp
```

Invalid DBML answers `422` with `errors: [{message, line, column, end_line, end_column, code}]`
(`/validate` answers `200` with `valid: false`).

### Review rules

Missing primary keys, refs to non-unique columns, foreign key type mismatches,
unindexed and nullable foreign keys, many-to-many refs, duplicate and redundant
indexes, SQL reserved words, float money columns, varchar without length on
MySQL/SQL Server, mixed naming styles and singular/plural table names, columns
that look like keys with no ref, isolated tables, missing timestamps, unused
enums and wide tables. `GET /api/v1/rules` is the list.

## The DBA agent (Ask, pay as you go)

A DBA as a service: a database question in plain words gets an answer
grounded in the engine. The schema's summary, every review finding and exact
SQL go into the prompt, and the checker's exact output is added to convert and
review answers. 5 cents per answered question from a prepaid balance; failures
are free. Top up $5/$10/$20 through CoinPay's hosted checkout (no
subscriptions).

- **People:** the "Ask the DBA agent" button on the home page asks about the
  playground's schema in place; `/ask` is the full page with every past answer.
  Anyone gets a personal link there (`POST /api/signup`, 5 per IP per hour,
  `SIGNUP_CREDIT_CENTS` free credit, default 0).
- **Sign in with CoinPay:** `/signin` (OAuth 2.1 + PKCE; the first sign-in
  creates the account, so it is also signup; passwords and resets live at
  CoinPay), `/signout`. Accounts bind to the CoinPay `sub`, never the email.
  Signing in while on a personal link links CoinPay to that account; each
  device gets its own session, so the personal link keeps working. Needs
  `COINPAY_OAUTH_CLIENT_ID` / `COINPAY_OAUTH_CLIENT_SECRET` (vault
  `dbml-db--prod`, redirect `https://dbml.db.pit.world/auth/coinpay/callback`).
- **SQL import is forgiving:** DBML settings in SQL (`int [pk]`, `[not null]`,
  `[default: now()]`), MySQL's inline `INDEX name (cols)` in another dialect,
  and `ALTER TABLE ... ADD COLUMN` are rewritten before parsing; the response
  lists each rewrite in `warnings`. Valid SQL is parsed untouched.
- **Agents:** `POST /api/ask` + `GET /api/ask/<id>` with
  `Authorization: Bearer <token>`, or the MCP tools `ask_dba` / `ask_dba_result`
  with the same header on the `/mcp` connection.
- **Models:** c0mpute first through `@profullstack/ai-providers`. OpenAI or
  Claude is offered for a question only when its price is at least
  `ASK_MIN_MARGIN` (2) x the worst-case cost of the paid call (prompt at 3
  chars/token, all 2,500 output tokens, plus any paid attempt that already
  failed). Rates per million tokens: `AI_COST_OPENAI_IN/OUT` (0.25/2,
  gpt-5-mini) and `AI_COST_ANTHROPIC_IN/OUT` (0.10/0.50, claude-haiku-5-5);
  set them when changing `AI_OPENAI_MODEL` / `AI_ANTHROPIC_MODEL`.

Operator (`ADMIN_KEY` from the vault):

```sh
DBML_ADMIN_KEY=... node bin/admin.js create "Name" --credit 1000   # prints the personal link
node bin/admin.js list | credit <id> <cents> | link <id> | revoke <id> | costs | backup <file>
```

State is one SQLite file in `/data` (the c0mpute app state directory).

## Run it

```sh
pnpm install
pnpm start          # http://localhost:8080
pnpm test
```

Env: `PORT` (8080), `HOST`, `SITE_URL` (https://dbml.db.pit.world),
`RATE_LIMIT_PER_MIN` (60). Paid tier (optional): `COINPAY_X402_KEY` (scoped
CoinPay key with `payments:create`) and `CRAWL_PAY_TO` (EVM address); with both
set, a caller over the free limit is offered a $1 day pass over x402 / CoinPay
checkout instead of a plain 429. Keys live in the logicsrc vault
`profullstack/dbml-db--prod`, never in the repo.

## Deploy

c0mpute Apps (stateless, 2 copies, primary on vienna2, dev2 allowed):

```sh
sh deploy/deploy.sh                       # prints the app id
moshcode name c0mpute dbml.db <app_id>    # only when the app id changes
```

The Moshpit name `dbml.db` (ending `.db`) serves the app; the web2 address is
https://dbml.db.pit.world.

## Databases on c0mpute (`/databases`)

Sign in with c0mpute (OAuth 2.1 + PKCE, public client `c0c_AyiyxjD37Pp8ZXYkm4v4Ng`,
scope `db`) and dbml.db manages the person's c0mpute databases: Postgres 17 with
pgvector and PostGIS, copies on several c0mpute nodes (DIP 0047), metered on
their c0mpute account (no dbml.db fee).

- c0mpute.com holds the databases (`/api/v1/databases`, created by the c0mpute
  cloud node); dbml.db holds only the person's `db`-scoped key, sealed with
  `DBML_KEK` (or a key derived from `ADMIN_KEY`). Accounts bind by the c0mpute
  user id, never an email.
- Deploy: `src/migrate.js` introspects the live schema (one read-only
  statement), diffs it against the DBML and plans the SQL; apply runs it in one
  transaction through the database's SQL door and records `_dbml_migrations`.
  Destructive steps only with `allow_destructive`.
- Surfaces: `/databases` page, the editor's **Live DB** tab, `/api/v1/databases/*`,
  MCP tools (`list_databases` ... `destroy_database`), CLI `dbml db ...`.
- Roadmap (MariaDB, the full suite): [docs/roadmap.md](docs/roadmap.md).

## TODO

- Human review marketplace: let an agent request a paid review of its schema by
  a human DBA through c0mpute.com/humans. Not built.

## DBML-X: SQL, document and vector databases

DBML-X models document collections and vector indexes next to SQL tables, in
plain DBML that any DBML tool still parses. A table's kind comes from its
`Metadata` block, else from `Project.database_type` (MongoDB-like ->
collection, Qdrant/Pinecone-like -> vector):

```dbml
Table orders {
  _id objectId [pk]
  "shipping.city" string      // nested field
  items "object[]"            // array of objects
  "items[].sku" string
}
Metadata Table orders { kind: 'collection' }

Table chunks {
  id uuid [pk]
  embedding vector(1536) [not null]
  lang varchar(8)             // payload, filterable
  indexes { lang }
}
Metadata Table chunks { kind: 'vector', metric: 'cosine', index: 'hnsw' }
```

Review has rules for each kind (dimensions, metric, payload indexes, `_id`,
orphan nested paths) and skips SQL-only rules for collections and vectors.
`POST /api/v1/export {dbml, target}` (MCP `export_schema`, CLI
`dbml export --to`) writes SQL for postgres/mysql/mssql/oracle, a mongosh script
for MongoDB (`$jsonSchema` validators, indexes, Atlas Vector Search), pgvector
DDL with an HNSW/IVFFlat index, and Qdrant and Pinecone configs. Starter
schemas: `GET /api/v1/examples`. Code: `src/kinds.js`.

## Editor (`/playground/`)

`playground/` is a fork of
[DBML Playground](https://github.com/holistics/dbml/tree/master/dbml-playground)
by Holistics (Apache-2.0, see `playground/LICENSE` and `playground/NOTICE`;
forked at upstream `b2ae4c1`). It keeps the multi-file Monaco editor, live
diagnostics and parser views, and adds three tabs backed by this server:
**Diagram** (`/api/v1/diagram`), **Ask AI** (the DBA agent, with one-click
apply of a revised schema) and **MCP** (connect config and the live tool list).

```sh
cd playground && npm ci && VITE_DBML_API=https://dbml.db.pit.world npx vite  # dev on :3001
cd playground && npx vite build                                              # served by src/app.js
```

The Dockerfile builds it in its own stage. To take upstream changes, diff a
fresh upstream `dbml-playground/` against this directory; our changes are
listed in `playground/NOTICE`.

## Credits and license

Parsing and SQL import/export use [@dbml/core](https://github.com/holistics/dbml)
by Holistics, licensed Apache-2.0 (see NOTICE). DBML is their language; this is
an independent service. The code in this repository is MIT, Copyright (c) 2026
Profullstack, Inc.