# sekejap.query.run

Run SQL on the project's built-in database (connection default-multimodel; no credential, no setup)

# sekejap.query.run

```
sekejap.query.run [--query TEXT] [--param KEY=EXPR…] [--write] [--limit N] [--statements EXPR] → query
```

Run SQL on the project's built-in database (connection `default-multimodel`; no credential, no setup). SQL goes in the body after `--` (or `--query`), values bind as `$1, $2, …` from `--param 1=… --param 2=…` — a literal is text, a whole `{{ expr }}` keeps its type. Reads only unless `--write` is set: a write without it is refused. Adds `query: { rows, columns, row_count, truncated }` with each row an object keyed by column (`input.query.rows[0].title`), plus `rows_affected` with `--write`; an `INSERT … RETURNING _key` answers the rows it wrote. `--limit` caps the rows (default 200, at most 5000). Every row has a `_key`: name it in the INSERT, or declare `_key TEXT PRIMARY KEY DEFAULT ulid()` and read the minted key back with `RETURNING _key`. A plain INSERT never overwrites (a taken key is 23505); upsert with `ON CONFLICT (_key) DO UPDATE SET c = EXCLUDED.c`. Sekejap's SQL is PostgreSQL's outside graphs: `LIKE`/`ILIKE`, `UNIQUE`, `REFERENCES`, `DEFAULT`, `NOT NULL`; relations between rows are graph walks, not JOINs — `GRAPH_TABLE (g MATCH (a)-[e:knows]->(b) RETURN b.name AS name)`; pages continue after the last row (`WHERE (name, _key) > ($1, $2) ORDER BY name, _key LIMIT 20`), there is no OFFSET. See help topic `db/sekejap`. An unknown table or column fails at run time, not at register. All or nothing: `--write --statements "{{ [ { sql: 'DELETE FROM order_items WHERE order_id = $1', params: [id] }, { sql: 'UPDATE orders SET status = $2 WHERE _key = $1', params: [id, 'cancelled'] } ] }}"` runs up to 100 statements in order in one transaction — every write lands or none does, a read sees the writes before it, the first failure rolls everything back and names its statement; schema changes run on their own. It answers the last statement's rows as `query.rows`, the total `rows_affected`, and each statement's answer under `query.statements`.

## Flags

- `--query`: The Sekejap SQL statement (or the body after `--`). A literal or {{ expr }}.
- `--param`: One bind value, repeated: `1=…` binds $1, `2=…` binds $2. A literal is text; a whole {{ expr }} keeps its JSON type, so a number stays a number.
- `--write`: Let the statement change data. Without it the query runs read-only and a write is refused. Literal; never {{ }}.
- `--limit`: Rows answered (default 200, at most 5000); `truncated` says more matched.
- `--statements`: All or nothing, with --write: a list of { sql, params } run in order in one transaction (at most 100); `params` is a list binding $1, $2, …. Instead of --query and --param.

## Examples

### Read with a bound value

```
| sekejap.query.run --param "1={{ $trigger.params.slug }}" -- "SELECT _key, title, body FROM posts WHERE slug = $1"
```

It answers:

```json
{
  "query": {
    "rows": [
      {
        "_key": "hello-world",
        "title": "Hello",
        "body": "…"
      }
    ],
    "columns": [
      "_key",
      "title",
      "body"
    ],
    "row_count": 1,
    "truncated": false
  }
}
```

### Insert from a form, learning the new key

```
| sekejap.query.run --write --param "1={{ $trigger.body.title }}" -- "INSERT INTO posts (title) VALUES ($1) RETURNING _key"
```

The table declares `_key TEXT PRIMARY KEY DEFAULT ulid()`, so the INSERT leaves the key out and `RETURNING` hands it back — `input.query.rows[0]._key` for a redirect.

It answers:

```json
{
  "query": {
    "rows": [
      {
        "_key": "01M3JSKZFJ4AZGVR0PG6ZXMDH5"
      }
    ],
    "columns": [
      "_key"
    ],
    "row_count": 1,
    "truncated": false,
    "rows_affected": 1
  }
}
```

### Upsert

```
| sekejap.query.run --write --param "1={{ $trigger.body.slug }}" --param "2={{ $trigger.body.title }}" -- "INSERT INTO posts (_key, title) VALUES ($1, $2) ON CONFLICT (_key) DO UPDATE SET title = EXCLUDED.title"
```

A plain INSERT of a key that exists is refused with 23505; ON CONFLICT is the upsert.

It answers:

```json
{
  "query": {
    "rows": [],
    "columns": [],
    "row_count": 0,
    "truncated": false,
    "rows_affected": 1
  }
}
```

### Create a table

```
| sekejap.query.run --write -- "CREATE TABLE posts (_key TEXT PRIMARY KEY DEFAULT ulid(), title TEXT NOT NULL, slug TEXT UNIQUE, status TEXT DEFAULT 'draft', created_at TIMESTAMPTZ DEFAULT now()) WITH (fulltext: [title])"
```

Run once from `pipeline_run` or a `jobs/migrate` function pipeline; keep the SQL in `db/001_posts.sql`. Scalar columns are indexed automatically; `WITH` declares full-text, spatial and vector indexes.

### Search by part of a name

```
| sekejap.query.run --param "1={{ '%' + $trigger.query.q + '%' }}" -- "SELECT _key, name FROM members WHERE name ILIKE $1 ORDER BY name LIMIT 20"
```

It answers:

```json
{
  "query": {
    "rows": [
      {
        "_key": "m1",
        "name": "Alex Doe"
      }
    ],
    "columns": [
      "_key",
      "name"
    ],
    "row_count": 1,
    "truncated": false
  }
}
```

### Newest first, one page at a time

```
| sekejap.query.run --param "1={{ $trigger.query.after || '~' }}" -- "SELECT _key, title FROM posts WHERE _key < $1 ORDER BY _key DESC LIMIT 20"
```

ULID keys sort by time, so `ORDER BY _key DESC` is newest first. The next page passes the last `_key` shown as `after`; there is no OFFSET.

It answers:

```json
{
  "query": {
    "rows": [
      {
        "_key": "01M3JSKZFM6MM3GJFF0WKN71DD",
        "title": "Latest"
      }
    ],
    "columns": [
      "_key",
      "title"
    ],
    "row_count": 1,
    "truncated": false
  }
}
```

### Add an edge

```
| sekejap.query.run --write --param "1={{ $trigger.body.member }}" --param "2={{ $trigger.body.institution }}" --param "3={{ $trigger.body.position }}" -- "INSERT INTO affiliated_with (member_id, institution_id, position) VALUES ($1, $2, $3)"
```

`affiliated_with` is an edge table: a table with two REFERENCES columns declared in `CREATE PROPERTY GRAPH … EDGE TABLES`. UPDATE and DELETE name an end: `DELETE FROM affiliated_with WHERE member_id = $1 AND institution_id = $2`.

It answers:

```json
{
  "query": {
    "rows": [],
    "columns": [],
    "row_count": 0,
    "truncated": false,
    "rows_affected": 1
  }
}
```

### Walk the graph

```
| sekejap.query.run --param "1={{ $trigger.params.id }}" -- "SELECT name, position FROM GRAPH_TABLE (network MATCH (m WHERE m._key = $1)-[a:affiliated_with]->(i) RETURN i.name AS name, a.position AS position)"
```

Name the edge (`[a:affiliated_with]`) to read its properties. Each matching path is one row; `RETURN DISTINCT` returns a node once.

It answers:

```json
{
  "query": {
    "rows": [
      {
        "name": "University One",
        "position": "Lecturer"
      }
    ],
    "columns": [
      "name",
      "position"
    ],
    "row_count": 1,
    "truncated": false
  }
}
```

## Used in

- [Agentic Scheduling (AI + Cron)](/examples/agentic-scheduling.md)
- [Blog with Admin](/examples/blog-with-admin.md)
- [Forum with Real-Time Chat](/examples/forum-with-chat.md)
- [Sign in to a published MCP server (OAuth)](/examples/mcp-oauth-login.md)
- [Real-Time Game (WebSocket State Sync)](/examples/realtime-game.md)
- [Web Scraping + Data Pipeline](/examples/scraping.md)
- [REST API + PostgreSQL](/examples/webhook-restapi-postgres.md)
