1 unstable release
new 0.5.2 | Jan 14, 2025 |
---|
#423 in Procedural macros
Used in gel-tokio
23KB
492 lines
Gel Rust Binding: Derive Crate
This crate contains derive macros for the Gel client.
License
Licensed under either of
- Apache License, Version 2.0, (./LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (./LICENSE-MIT or http://opensource.org/licenses/MIT)
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 Gel). Note that field order matters, so the struct below
corresponds to an Gel 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 #[gel(json)]
attribute decodes a field using serde_json
instead
of the Gel 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 {
#[gel(json)]
user_notes: HashMap<String, String>,
}
Container attributes
JSON
The #[gel(json)]
attribute can be used to unpack the structure from
the returned JSON. The underlying type must implement
serde::Deserialize
.
#[derive(Queryable, serde::Deserialize)]
#[gel(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–8.5MB
~75K SLoC