# User types

> agdb example: using your own structs and enums in queries via derive macros

## What it shows

The example shows the derive macros that let you use your own Rust types directly in queries:

- `DbValue` + `DbSerialize` store an enum or a struct as a single value.
- `DbTypeMarker` allows a type to be used in a `Vec` of values.
- `DbType` maps a struct to the key-value pairs of a database element, with the `rename`, `skip` and `flatten` field attributes and optional fields.
- `DbElement` additionally stores the type of the struct so that two types with the same fields can be told apart.

## Run it

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

The derive macros are part of the default `derive` feature of the `agdb` crate.

## Defining the types

```rs
#[derive(Debug, Clone, DbSerialize, DbValue)]
enum UserStatus {
    Active,
    Inactive,
    Banned,
}

// The empty trait `DbTypeMarker` (deriveable via
// `agdb::DbTypeMarker`) is used to allow the user
// types (structs & enums) to be used in vectorized
// variants of the user types.
#[derive(Debug, Clone, DbTypeMarker, DbSerialize, DbValue)]
struct Property {
    name: String,
    value: String,
}

#[derive(Debug, DbType)]
struct SubProperty {
    id: i32,
}

#[derive(Debug, DbType)]
struct User {
    username: String,
    password: String,
    #[agdb(rename = "name")]
    display_name: Option<String>,
    status: UserStatus,
    extra: Vec<Property>,
    #[agdb(skip)]
    _skipped: (),
    #[agdb(flatten)]
    sub: SubProperty,
}
```

## Telling overlapping types apart

Types derived with `DbElement` store their type with each element, so a search can return only the elements of one type even when several types with the same fields are reachable from the same node - no extra conditions needed:

```rs
let type1: T1 = db
    .exec(
        QueryBuilder::select()
            .element::<T1>()
            .search()
            .from("types")
            .query(),
    )?
    .try_into()?;
let type2: T2 = db
    .exec(
        QueryBuilder::select()
            .element::<T2>()
            .search()
            .from("types")
            .query(),
    )?
    .try_into()?;
```

## Learn more

- [DbType](/docs/references/queries#dbtype) in the queries reference.
- [Data types](/docs/guides/concepts#data-types) - which Rust types map to which database values.
- [Quickstart](/docs/guides/quickstart) - a step-by-step guide using user types.

<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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}

</style>

---

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