Surreal derive
Description
Simple library for writing SurrealQL , this is just a verythin layer on top of SurrealDb Rust SDK.
An interesting solution for working with RecordId and Relate statement
Take a look on my other crate call surrealdb-id .
Installation
1. Install surreal-devl: https://crates.io/crates/surreal_devl
Contains the core logic of the whole library, the main purpose is to act as a bridge between SurrealDb SDK and the your defined Struct, also support working with Array, ID or DateTime
cargo add sureal_devl
2. Install surreal-derive:
cargo add surreal_derive_plus
Note:
Current restriction that will be resolved in future: If your variable names coincide with any of the following supported statements: ["id", "val", "date", "duration", "record", "set", "content", "multi", "array"], kindly consider renaming them.
Usage
Mark your struct as surreal_derive.
This will will automatically generate code that could convert your struct into surrealdb idioms
use ;
use SurrealDerive;
Implement the Into/surrealdb::value::RecordId trait
use RecordId;
use crateUser;
Write query by using surreal_derive_plus::surreal_quote! macro
Struct
use surreal_quote;
.... connect to your surreal db ...
let new_user = User ;
let created_user: = DB.query.await.unwrap.take.unwrap; => CREATE user:surreal SET name='surreal', password='000000'
Variable
let age = 2;
let query_statement = surreal_quote!;
assert_eq!;
Array
let arr = vec!;
let query_statement = surreal_quote!;
assert_eq!;
Struct Array
let friends = vec!;
let query_statement = surreal_quote!;
assert_eq!;
DateTime
let birthday: = Utc.with_ymd_and_hms.unwrap;
let query_statement = surreal_quote!;
assert_eq!;
Duration
let party_duration = from_millis;
let party_started_at: = Utc.with_ymd_and_hms.unwrap;
let query_statement = surreal_quote!;
assert_eq!;
Surreal ID
let user = User ;
let query_statement = surreal_quote!;
assert_eq!;
Surreal Value
This will wrap the variable inside surrealdb::sql::Value::from()
let str = Stringfrom;
let statement = surreal_quote!;
assert_eq!;
Relation
Using with surrealdb-id crate we can simplify the process of working with relate statement, then you can generate entire relate statement
link here: https://crates.io/crates/surrealdb_id
Example:
let user: RecordId = from;
let blogPost: RecordId = from;
let discussion = Discussion ;
let relation = discussion.relate
assert_eq!;
Surreal Resource
let new_user = User ;
db.create.content;
Customize setting
You can customize the setting inside cargo.toml it is neccessary for call cargo clean to apply all of these configuration
[]
# Will log the query command at runtime
= false
# Will log the generated code at build time
= false
# Change the naming convention of generated statement into camel case
= false
# The log namespace, apply for both build time log and runtime log
= "surrealql-derive"
# The macro name that use for info log, for example
= "println"
# The macro name that use for warning log, for example
= "println"