Expand description
Express complex, composable queries through Rust structs and attributes.
§edgedb_composable_query::composable
Beware: it’s very much a work-in-progress. Pre-0.1. There’re todo!()’s in the code, etc. I’m still figuring out the semantics. If you’re interested in this working for your use-cases, please try it and file the issues at: https://github.com/valyagolev/edgedb-composable-query/issues. But don’t use it seriously yet; it might panic, and will change the API.
§EdgedbComposableQuery Examples
If you have this schema:
module default {
type Inner {
required req: str;
opt: str;
}
type Outer {
inner: Inner;
some_field: str;
required other_field: str;
}
Here’re some of the ways to use this module:
use edgedb_protocol::model::Uuid;
use edgedb_composable_query::{query, EdgedbObject, Ref};
use edgedb_composable_query::composable::{EdgedbComposableQuery, EdgedbComposableSelector, run_query};
let conn = edgedb_tokio::create_client().await?;
// You can define specific queries directly:
#[derive(Debug, PartialEq, Eq, EdgedbObject,
EdgedbComposableSelector, EdgedbComposableQuery)]
#[select("select Inner limit 1")]
struct OneInnerQuery {
req: String,
opt: Option<String>,
}
assert!(
run_query::<OneInnerQuery>(&conn, ()).await.is_ok()
);
// If you want to compose them, typically it's better to extract the selector:
#[derive(Debug, PartialEq, Eq, EdgedbObject, EdgedbComposableSelector)]
struct InnerSelector {
req: String,
opt: Option<String>,
}
#[derive(Debug, PartialEq, Eq, EdgedbComposableQuery)]
#[select("select Inner limit 1")]
struct OneInnerBySelector(InnerSelector);
assert!(
run_query::<OneInnerBySelector>(&conn, ()).await.is_ok()
);
#[derive(Debug, PartialEq, Eq, EdgedbComposableQuery)]
#[params(id: Uuid)]
#[select("select Inner filter .id = id")]
struct OneInnerBySelectorById(Option<InnerSelector>);
assert!(
run_query::<OneInnerBySelectorById>(&conn, (
Uuid::parse_str("9be70fb0-8240-11ee-9175-cff95b46d325").unwrap(),
)).await.unwrap().is_some()
);
#[derive(Debug, PartialEq, Eq, EdgedbComposableQuery)]
#[select("select Inner limit 10")]
struct ManyInnersBySelector(Vec<InnerSelector>);
// Queries can have parameters:
// (And remember to use Ref<T>)
#[derive(Debug, PartialEq, Eq, EdgedbObject,
EdgedbComposableSelector, EdgedbComposableQuery)]
#[params(id: Uuid)]
#[select("select Outer filter .id = id limit 1")]
struct OuterByIdQuery {
inner: Option<Ref<InnerSelector>>,
some_field: Option<String>,
other_field: String,
}
Enums§
Traits§
- Edgedb
Composable Query - Derivable trait. Can have parameters. Either an object with named fields, or can be a wrapper around a selector, or
Option<selector>
, orVec<selector>
, or `NonEmpty``. - Edgedb
Composable Selector - Derivable trait. Must have named fields, each is either another selector, or a primitive, or a
Vec/Option/NonEmpty
of those.
Functions§
- run_
query - use this to run an
EdgedbComposableQuery