Surreal Derive
Description
- Generates query statements
- Provides easy access to query results via paths
- Supports serialization to
surrealdb::sql::Valueand deserialization fromsurrealdb::sql::Valueinstead of using serde - Support enum
- Easy to add custom type
- Supports IDs and nested structs
- Supports relations
Installation
1. Install surreal-devl: https://crates.io/crates/surreal_devl
cargo add surreal_devl
2. Install surreal-derive:
cargo add surreal_derive_plus
Usage:
Generate query statement
use ;
use Value;
// Example 1: Generate query statement
let user = User ;
// Generates: CREATE user:john SET name = 'john', age = 30
let query = surreal_quote!;
Easy access to query result from path
let result: SurrealQR = db.query.await?.take;
// Access nested fields
let name: = result.get?.deserialize?;
Serialize and Deserialize
// Serialize
let user = User ;
let value: Value = user.serialize;
// Deserialize
let user: User = deserialize?;
Support id and nested struct
let address = Address ;
let user_address = Address ;
let user = User ;
let company = Company ;
// Create user with nested struct address
let query = db.query.await?;
// Create company with link to user eg: user = user:john
let query = surreal_quote!;
let result: = db.query.await?.take;
Support relation
let edge = Employment .relate;
// Creates relation: RELATE employee:john->employment:developer->company:acme
db.query.await?;
Foreign key
Support enum
// Example usage:
let admin = User ;
let power_user = User ;
let mod_user = User ;
// Serialize to SurrealDB
let query = surreal_quote!;
// Will create: CREATE user:alice SET name = 'alice', role = { type: 'Admin' }
let query = surreal_quote!;
// Will create: CREATE user:bob SET name = 'bob', role = { user: { level: 5 }
let query = surreal_quote!;
// Will create: CREATE user:charlie SET name = 'charlie', role = { moderator: [ 'forums' ] }
// Deserialize from query results
let result: User = db.query.await?.take?;
assert!;
Support custom type
To support custom types, implement both SurrealSerializer and SurrealDeserializer traits:
use ;
use Value;
// Example: Custom DateTime wrapper
;
// Use in structs
// Example usage:
let event = Event ;
// Serialize to SurrealDB
let query = surreal_quote!;
// Deserialize from query results
let result: Event = db.query.await?.take?;
Variables
Normal variable
let age = 2;
let query_statement = surreal_quote!;
Array
let arr = vec!;
let query_statement = surreal_quote!;
Struct Array
let friends = vec!;
let query_statement = surreal_quote!;
DateTime
let birthday: = Utc.with_ymd_and_hms.unwrap;
let query_statement = surreal_quote!;
Duration
let party_duration = from_millis;
let party_started_at: = Utc.with_ymd_and_hms.unwrap;
let query_statement = surreal_quote!;
Surreal ID
Convert a struct into it's id if it has implement SurrealId trait
let user = User ;
let query_statement = surreal_quote!;
Custom Settings
You can customize settings inside Cargo.toml
You might need to call cargo clean for changes to take effect
[]
# 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"
License
This project is licensed under the MIT License - see below for details:
MIT License
Copyright (c) 2024 surreal-derive contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.