> For the complete documentation index, see [llms.txt](https://roam-tui.avelino.run/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://roam-tui.avelino.run/sdk/queries.md).

# Queries

Helper functions for building Roam API queries. Available at `roam_sdk::queries`.

## Daily notes

### `daily_note_uid_for_date`

Generate the UID for a daily note from a date.

```rust
pub fn daily_note_uid_for_date(month: u32, day: u32, year: i32) -> String
```

```rust
let uid = queries::daily_note_uid_for_date(2, 21, 2026);
assert_eq!(uid, "02-21-2026");
```

### `pull_daily_note`

Build a pull request for a daily note by its UID.

```rust
pub fn pull_daily_note(uid: &str) -> (serde_json::Value, String)
```

Returns `(eid, selector)` ready to pass to `client.pull()`.

```rust
let uid = queries::daily_note_uid_for_date(2, 21, 2026);
let (eid, selector) = queries::pull_daily_note(&uid);
let resp = client.pull(eid, &selector).await?;
```

The selector includes: `:block/uid`, `:node/title`, `:block/string`, `:block/children` (recursive), `:block/order`, `:block/open`, `:block/refs`.

## Pages

### `pull_page_by_title`

Build a pull request for any page by title.

```rust
pub fn pull_page_by_title(title: &str) -> (serde_json::Value, String)
```

Uses `[:node/title "..."]` as the entity lookup. Same selector as daily notes.

```rust
let (eid, selector) = queries::pull_page_by_title("Projects");
let resp = client.pull(eid, &selector).await?;
```

### `all_page_titles_query`

Build a Datalog query to fetch all page titles and UIDs.

```rust
pub fn all_page_titles_query() -> String
```

Returns rows of `[title, uid]`.

### `search_blocks_query`

Build a Datalog query to fetch all blocks with their text and parent page title.

```rust
pub fn search_blocks_query() -> String
```

Returns rows of `[uid, block_string, page_title]`. Useful for full-text search when combined with client-side filtering.

## Graph statistics

### `graph_page_count_query`

Count total pages in the graph.

```rust
pub fn graph_page_count_query() -> String
```

Returns `[[count]]`.

### `graph_block_count_query`

Count total blocks in the graph.

```rust
pub fn graph_block_count_query() -> String
```

Returns `[[count]]`.

## Linked references

### `linked_refs_query`

Build a Datalog query that finds all blocks referencing a page.

```rust
pub fn linked_refs_query(page_title: &str) -> String
```

Returns a query string for `client.query()`. Double quotes in the title are escaped.

```rust
let query = queries::linked_refs_query("My Project");
let resp = client.query(query, vec![]).await?;
```

The query returns rows of `[uid, block_string, source_page_title]`. Parse the results with `types::parse_linked_refs()`:

```rust
let groups = types::parse_linked_refs(&resp.result, "My Project");
```

## Writing your own queries

You can pass any Datalog query string directly to `client.query()`:

```rust
// Find all blocks containing "TODO"
let query = r#"[:find ?uid ?s
                :where [?b :block/string ?s]
                       [?b :block/uid ?uid]
                       [(clojure.string/includes? ?s "TODO")]]"#;

let resp = client.query(query.into(), vec![]).await?;
```

Query format notes:

* Use `:find` with simple variable bindings (not pull expressions)
* `:where` clauses use Datomic-style pattern matching
* `args` must always be provided (use `vec![]` for no arguments)
* Results are `Vec<Vec<serde_json::Value>>` with values in `:find` variable order


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://roam-tui.avelino.run/sdk/queries.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
