#edge-db #macro #client

macro edgedb-derive

Derive macros for EdgeDB database client

7 releases (4 breaking)

0.5.2 May 9, 2024
0.5.1 Jun 22, 2023
0.5.0 May 15, 2023
0.4.0 Jul 26, 2022
0.1.0 Oct 23, 2020

#847 in Procedural macros

Download history 96/week @ 2024-08-18 208/week @ 2024-08-25 95/week @ 2024-09-01 59/week @ 2024-09-08 76/week @ 2024-09-15 151/week @ 2024-09-22 136/week @ 2024-09-29 155/week @ 2024-10-06 153/week @ 2024-10-13 69/week @ 2024-10-20 60/week @ 2024-10-27 144/week @ 2024-11-03 127/week @ 2024-11-10 114/week @ 2024-11-17 107/week @ 2024-11-24 65/week @ 2024-12-01

422 downloads per month
Used in 6 crates

MIT/Apache

23KB
490 lines

EdgeDB Rust Binding: Derive Crate

This crate contains derive macros for the EdgeDB client.

License

Licensed under either of

at your option.


lib.rs:

Derive macro that allows structs and enums to be populated by database queries.

This derive can be used on structures with named fields (which correspond to "shapes" in EdgeDB). Note that field order matters, so the struct below corresponds to an EdgeDB User query with first_name followed by age. A DescriptorMismatch will be returned if the fields in the Rust struct are not in the same order as those in the query shape.

#[derive(Queryable)]
struct User {
first_name: String,
age: i32,
}

This allows a query to directly unpack into the type instead of working with the Value enum.

let query = "select User { first_name, age };";
// With Queryable:
let query_res: Vec<User> = client.query(query, &()).await?;
// Without Queryable:
let query_res: Vec<Value> = client.query(query, &()).await?;

Field attributes

JSON

The #[edgedb(json)] attribute decodes a field using serde_json instead of the EdgeDB binary protocol. This is useful if some data is stored in the database as JSON, but you need to process it. The underlying type must implement serde::Deserialize.


#[derive(Queryable)]
struct User {
#[edgedb(json)]
user_notes: HashMap<String, String>,
}

Container attributes

JSON

The #[edgedb(json)] attribute can be used to unpack the structure from the returned JSON. The underlying type must implement serde::Deserialize.

#[derive(Queryable, serde::Deserialize)]
#[edgedb(json)]
struct JsonData {
field1: String,
field2: u32,
}

This allows a query to directly unpack into the type without an intermediate step using serde_json::from_str:

let query = "select <json>JsonData { field1, field2 };";
let query_res: Vec<JsonData> = client.query(query, &()).await?;

Dependencies

~1.1–8MB
~77K SLoC