Queries attribute macros
Edgedb-query-derive crate provide 5 macro attributes that represent a edgeDB query:
Queries attributes (except #[file_query] ) take three arguments 👇
Argument | Optional | Description |
---|---|---|
module | yes | The name of the edgeDB module on which the query is executed. By default: 'default' |
table | no | The name of the edgeDB table on which the query is executed. |
result | yes | The query result type. By default: BasicResult |
When a struct is decorated with one of those queries macro attributes, several trait implementations are created for this one:
The following example shows how to get a parameterized query from a struct decorated with a query macro attribute ( #[insert_query] for example) :
Consider the following struct 👇:
#[insert_query(module="humans", table="Person")]
pub struct InsertPerson {
pub name: String,
pub age: i8
}
To get a parameterized query from this struct, we can write the following code:
#[tokio::main]
fn main() -> Result<()>{
let insert_person = InsertPerson {
name: "John".to_string(),
age: 20
};
let edge_query: EdgeQuery = insert_person.to_edge_query();
}
to_edge_query()
( from ToEdgeQuery trait ) method returns a EdgeQuery struct that contains the query string and the query parameters.
The query's cardinality (Cardinality::Many
by default) can also be set while getting the query by calling to_edge_query_with_cardinality()
method instead of to_edge_query()
.
let edge_query: EdgeQuery = insert_person.to_edge_query_with_cardinality(Cardinality::One);
With the got query, we can now execute request using edgedb-client like this:
if let EdgeQuery{ query, args: Some(params), .. } = edge_query {
let result: BasicResult = client.query_required_single(query.as_str(), params).await?;
}