# Server client - Rust

> agdb example: managing users and databases and running queries on agdb_server from Rust

## What it shows

The example uses the async Rust client `agdb_api` to log in to an `agdb_server` as the admin, create a user, log in as that user, create a database and run two batches of queries: the first inserts a `users` root and user nodes, the second links them and searches for one user.

## Run it

The example needs a running server on `localhost:3000` with the default `admin` user. In one terminal start the server:

```bash
cargo run -p agdb_server
```

and in another one run the example:

```bash
cargo run -p agdb_api --example server_client
```

The example creates the user `client`, so a second run against the same server fails because the user already exists. Run it against a fresh server (delete its `data_dir`, `agdb_server_data` by default) or change the user name.

## Users and databases

```rs
use agdb::DbType;
use agdb::QueryBuilder;
use agdb_api::DbKind as ApiDbType;
use agdb_api::ReqwestClient;

let mut client = agdb_api::AgdbApi::new(ReqwestClient::new(), "localhost:3000");

// Log in as admin and create a new user.
client.user_login("admin", "admin").await?;
client.admin_user_add("client", "password111").await?;

// Switch to the new user and create a database.
client.user_login("client", "password111").await?;
client.db_add("client", "db", ApiDbType::Memory).await?;
```

## Running queries

Queries are built with the same `QueryBuilder` as in the embedded database and sent in batches. The user data is a plain struct deriving `DbType`:

```rs
#[derive(Debug, DbType)]
struct User {
    username: String,
    password: String,
}
```

A batch runs as a single transaction and returns one result per query, so the second batch can use the ids returned by the first one:

```rs
let users = vec![
    User {
        username: "user1".to_string(),
        password: "password123".to_string(),
    },
    User {
        username: "user2".to_string(),
        password: "password456".to_string(),
    },
];

// Insert a root "users" alias and the user nodes.
let queries = vec![
    QueryBuilder::insert()
        .nodes()
        .aliases(["users"])
        .query()
        .into(),
    QueryBuilder::insert().nodes().values(&users).query().into(),
];
let results = client.db_exec_mut("client", "db", &queries).await?.1;

// Link users to root and search for one by username.
let queries = vec![
    QueryBuilder::insert()
        .edges()
        .from("users")
        .to(results[1].ids())
        .query()
        .into(),
    QueryBuilder::select()
        .search()
        .depth_first()
        .from("users")
        .where_()
        .key("username")
        .value("user1")
        .query()
        .into(),
];

let results = client.db_exec_mut("client", "db", &queries).await?.1;

println!("User: {:?}", results[1].elements[0].id);
for key_value in results[1].elements[0].values.iter() {
    println!("  {}: {}", key_value.key, key_value.value);
}
```

## Learn more

- [Rust client](/api-docs/rust) - installing and using `agdb_api`.
- [Server reference](/docs/references/server) - configuration, users, databases and the REST API.
- [How to run a server?](/docs/guides/how-to-run-server)

<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 .sw1J6, html code.shiki .sw1J6{--shiki-light:#F76D47;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .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 .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 .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 .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}

</style>

---

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