# Joins

> agdb example: combining data from nodes and edges without a join operator

## What it shows

`agdb` has no join operator because the relations are stored directly as edges. The example models a user who has access to several databases, with the database `name` stored on the nodes and the user's `role` stored on the edges, and shows two ways to read both into one list of `UserDb { name, role }`.

## Run it

```bash
cargo run -p agdb --example joins
```

## Single search

A depth-first search from the user returns edges and nodes in order: each edge (with `role`) is followed by the node it points to (with `name`). The sign of the id tells them apart - edges have negative ids:

```rs
let mut user_dbs: Vec<UserDb> = vec![];
db.exec(
    QueryBuilder::select()
        .search()
        .depth_first()
        .from("user")
        .where_()
        .keys("role")
        .or()
        .keys("name")
        .query(),
)?
.elements
.into_iter()
.for_each(|e| {
    if e.id.0 < 0 {
        // handle the edge
        user_dbs.push(UserDb {
            role: e.values[0].value.to_string(),
            name: String::new(),
        });
    } else {
        // handle the node
        user_dbs.last_mut().unwrap().name = e.values[0].value.to_string();
    }
});
```

## Multiple queries in a transaction

When the data is sparse, or you need more control, run several queries inside one read transaction. All of them see the same consistent state of the database:

```rs
let user_dbs = db.transaction(|t| -> Result<Vec<UserDb>, DbError> {
    let db_names = t.exec(
        QueryBuilder::select()
            .search()
            .from("user")
            .where_()
            .neighbor()
            .and()
            .keys("name")
            .query(),
    )?;

    let mut dbs = vec![];

    for db_name in db_names.elements {
        let role = t.exec(
            QueryBuilder::select()
                .search()
                .to(db_name.id)
                .limit(1)
                .where_()
                .keys("role")
                .query(),
        )?;

        dbs.push(UserDb {
            name: db_name.values[0].value.to_string(),
            role: role.elements[0].values[0].value.to_string(),
        });
    }

    Ok(dbs)
})?;
```

## Learn more

- [Transactions](/docs/references/queries#transactions) in the queries reference.
- [Conditions](/docs/references/queries#conditions) - `keys`, `neighbor`, `and`/`or` and more.
- [Search](/docs/references/queries#search) - search directions, `limit` and ordering.

<style>

html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sw1J6, html code.shiki .sw1J6{--shiki-light:#F76D47;--shiki-default:#D73A49;--shiki-dark:#F97583}

</style>

---

- [View source](https://github.com/agnesoft/agdb/blob/main/agdb/examples/joins.rs)
