# Databases

Single-writer SQLite (sql) and JSON document (documents) databases with durable S3 commit logs and 30 days of point-in-time history.

Skopik Platform **Databases** run on the Shared Objects engine. Each database is named within an organization (one shared namespace) and has a fixed **type** chosen at creation:

- **`sql`** — A dedicated SQLite database with a single writer, foreign keys enabled, parameterized queries, and transactional batches.
- **`documents`** — JSON documents with IDs, queryable by any field using JSON filters — no SQL required.

Every committed write is shipped to **S3 Express One Zone** before it is acknowledged and copied to **S3 Standard** within a second. Idle databases unload automatically and reload on any host on the next call.

## Creating a Database

:::tabs
::tab skopik
```bash
skopik create_database --name notes --type sql
skopik create_database --name tasks --type documents
```
::tab curl
```bash
curl -X POST https://api.platform.skopik.com/v1/databases \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"name": "notes", "type": "sql"}'
```
::tab MCP
```json
{
  "tool": "create_database",
  "arguments": { "name": "notes", "type": "sql" }
}
```
:::

## Working with `sql` Databases

Three operations are supported: `execute` (one statement in a transaction), `batch` (multiple statements in one atomic transaction), and `query` (read-only statement).

:::tabs
::tab skopik
```bash
skopik execute_sql --name notes \
  --sql "CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, title TEXT NOT NULL)"

skopik execute_sql --name notes \
  --sql "INSERT INTO items (title) VALUES (?)" \
  --params '["Ship docs"]'

skopik query_sql --name notes --sql "SELECT id, title FROM items"
```
::tab curl
```bash
curl -X POST https://api.platform.skopik.com/v1/databases/notes/query \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"sql": "SELECT id, title FROM items WHERE id = ?", "params": [1]}'
```
::tab MCP
```json
{
  "tool": "query_sql",
  "arguments": {
    "name": "notes",
    "sql": "SELECT id, title FROM items WHERE id = ?",
    "params": [1]
  }
}
```
:::

**Limits:** 30 seconds per call, 16 MiB max request/response body, **1 GiB** max database size. `ATTACH`, `VACUUM`, temp tables, and manual `BEGIN`/`COMMIT` are disallowed (use `batch` for multi-statement transactions).

## Working with `documents` Databases

Store JSON objects by ID or let the platform generate one, bulk-insert, and filter by any field:

- `POST /v1/databases/{name}/documents` — Insert a document (MCP `put_document`).
- `PUT / GET / DELETE /v1/databases/{name}/documents/{id}` — Replace, fetch, or delete by ID.
- `POST /v1/databases/{name}/documents/bulk` — Put multiple documents in one call (MCP `put_documents`).
- `POST /v1/databases/{name}/documents/query` — Filter, sort, and paginate documents (MCP `query_documents`).

## 30-Day History, Rewind & Time Travel

Because the S3 page log is the source of truth, both `sql` and `documents` databases support full history operations for the last **30 days**:

| Capability | Route | MCP Tool |
| --- | --- | --- |
| **Inspect a moment** | `POST /v1/databases/{name}/moment` | `get_database_moment` |
| **Query a past moment** | `POST /v1/databases/{name}/moment/query` (or `.../moment/documents/query`) | `query_sql_moment`, `query_documents_moment` |
| **Rewind in place** | `POST /v1/databases/{name}/rewind` | `rewind_database` |
| **Undo last rewind** | `POST /v1/databases/{name}/undo-rewind` | `undo_database_rewind` |
| **Copy a moment to a new DB** | `POST /v1/databases/{name}/copy` | `copy_database` |
| **Compare two moments** | `POST /v1/databases/{name}/compare` | `compare_database` |
| **Commit log & activity** | `GET /v1/databases/{name}/log`, `.../activity` | `list_database_log`, `get_database_activity` |
| **Download SQLite file** | `GET /v1/databases/{name}/download[?at=\|seq=]` | *(HTTP only)* |

> [!TIP]
> Calling a `sql` operation on a `documents` database (or vice versa) fails safely with `409 wrong_database_type`. You can also save reusable SQL queries per database with `save_database_query`.
