edgedb_codegen
Generate fully typed rust code from your EdgeDB schema and inline queries.
Installation
To install the edgedb_codegen crate you can use the following command.
Or directly add the following to your Cargo.toml file.
= "0.2"
Follow the Quickstart Guide to make sure your edgedb instance is running. The macro relies on the running edgedb instance to parse the output of the provided query string.
Usage
When working with edgedb you often need to write queries and also provide the typed for both the input and output. Your code is only checked at runtime which increases the risk of bugs and errors.
Fortunately, edgedb has a query language that is typed and can be converted into types and queried for correctness at compile time.
Inline Queries
use edgedb_query;
use Error;
use create_client;
// Creates a module called `simple` with a function called `query` and structs
// for the `Input` and `Output`.
edgedb_query!;
async
The macro above generates the following code:
Query Files
Define a query file in the queries directory of your crate called select_user.edgeql.
# queries/select_user.edgeql
select User {
name,
bio,
slug,
} filter .slug = <str>$slug;
Then use the edgedb_query macro to import the query.
use edgedb_query;
use Error;
use create_client;
// Creates a module called `select_user` with public functions `transaction` and
// `query` as well as structs for the `Input` and `Output`.
edgedb_query!;
async
Future Work
This crate is still in early development and there are several features that are not yet implemented.
Missing Types
Currently the following types are not supported:
enum- currently all enums are represented as strings.MultiRange- The macro will panic if a multirange is used.
enum
Currently all enums are represented as strings.
In order to support full enum generation the edgedb-protocol crate needs to be updated to use the binary protocol 2.0. In the current 1.0 version the enum descriptors are returned without the name property.
Once this is implemented the macro will be able to generate the correct code.
However end users probably don't want multiple enums for each generated query module as this would break sharing. To get around this, there should be a macro for generating the shared types used by all other.
// lib.rs
use generate_shared_types;
generate_shared_types!; // exports the shared types to the `edb` module.
MultiRange
These are not currently exported by the edgedb-protocol so should be added in a PR to the edgedb-protocol crate, if they are still supported in the new protocol.
Configuration
Currently everything is hardcoded and the macro is not configurable.
The following configuration options should be added:
- Name of input struct (optional) -
Inputby default. - Name of output struct (optional) -
Outputby default. - Name of query function (optional) -
queryby default. - Name of transaction function (optional) -
transactionby default. - Default location of queries (optional) -
queriesby default. - Default crate export name for shared types (optional) -
edbby default. - Default
edgedbinstance (optional) -$EDGEDB_INSTANCEby default. - Default
edgedbbranch (optional) -$EDGEDB_BRANCHby default.
Probably these should be read from the Cargo.toml file and parsed manually to prevent slowdowns from parsing the file.
LSP parsing
Currently the macro depends on having a running edgedb instance to parse the query string.
Once an LSP is created for edgedb it would make sense to switch from using string to using inline edgedb queries.
use edgedb_query;
edgedb_query!;
CLI
Create a edgedb_codegen_cli crate which supports generating the typed code into rust files rather than inline queries. This is useful for larger projects to prevent constantly compiling the queries on every change / build.
Features
default— The default feature iswith_all.with_bigint— Include thenum-bigintdependency.with_bigdecimal— Use thebigdecimalcrate.with_chrono— Use thechronocrate for all dates.with_all(enabled by default) — Include all additional types. This is included by default. Usedefault-features = falseto disable.builder— Use thetyped-buildercrate to generate the builders for the generatedInputstructs.query— Turn on thequeryandtransactionmethods and anything that relies onedgedb-tokio. The reason to separate this feature is to enable usage of this macro in browser environments whereedgedb-tokiois not feasible.serde— Enable serde for the generated code.