# App db

> agdb example: schemaless key-value nodes linked by edges and found with a graph search

## What it shows

The example creates an in-memory database (`DbMemory`) with a root node aliased `users`, inserts users as plain key-value nodes without any schema and links them to the root with edges. It then finds a user with a depth-first search filtered by `username` - the graph equivalent of `SELECT * FROM users WHERE username = "user1"` - and prints the id and the key-value pairs of the found element from the raw `QueryResult`.

## Run it

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

## Inserting schemaless data

Each node gets its own list of key-value pairs. There is no schema, so the second user can simply omit the `age`. The result of the insert is passed directly to the next query to connect the new nodes to the `users` root:

```rs
// Inserts the raw data by providing list of keys and values
// for each node. Notice we can easily create sparse data
// omitting certain fields as there is no schema to enforce.
let users = db.exec_mut(
    QueryBuilder::insert()
        .nodes()
        .values([
            vec![
                ("username", "user1").into(),
                ("password", "password123").into(),
                ("age", 20).into(),
            ],
            vec![
                ("username", "user2").into(),
                ("password", "password456").into(),
            ],
        ])
        .query(),
)?;

// Link the users with the "users" node. Note that we are referring to
// the users node with an alias "users" we have given it and to
// the new user nodes by supplying the result of the previous query directly.
// One could also use the `QueryResult::ids()` method to extract the ids.
db.exec_mut(
    QueryBuilder::insert()
        .edges()
        .from("users")
        .to(users)
        .query(),
)?;
```

## Searching and reading the result

The search starts at `users` and returns only elements whose `username` is `user1`. The result always has the same shape: a numeric `result` and a list of `elements`, each with an `id` and its key-value pairs. Nodes have positive ids and edges negative ids:

```rs
let user_result = db.exec(
    QueryBuilder::select()
        .search()
        .depth_first()
        .from("users")
        .where_()
        .key("username")
        .value("user1")
        .query(),
)?;

if user_result.result == 1 {
    println!("User: {:?}", user_result.elements[0].id);

    for key_value in user_result.elements[0].values.iter() {
        println!("  {}: {}", key_value.key, key_value.value);
    }
}
```

## Learn more

- [Concepts](/docs/guides/concepts) - graph, ids and data types.
- [Queries reference](/docs/references/queries) - [insert](/docs/references/queries#insert), [search](/docs/references/queries#search) and [conditions](/docs/references/queries#conditions).
- [User types](/docs/examples/user-types) - the same data with Rust structs instead of raw key-values.

<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 .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 .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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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/app_db.rs)
