surreal_derive_plus 1.0.1

Query builder for surrealdb
Documentation
surreal_derive_plus-1.0.1 has been yanked.

Surreal derive

Simple library for writing SurrealQL , this is just a verythin layer on top of SurrealDb Rust SDK. You will never pay any runtime performance for what you don't do

Installation

1. Install surreal-devl: https://github.com/dev-logs/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

Usage

Mark your struct as surreal_derive

This will will automatically generate code that could convert your struct into surrealdb idioms

use serde::{Deserialize, Serialize};
use surreal_derive_plus::SurrealDerive;

#[derive(Debug, Serialize, Deserialize, SurrealDerive, Clone)]
pub struct User {
    pub name: String,
    pub password: String,
}

Implement the Intosurrealdb::value::RecordId trait

use surrealdb::opt::RecordId;
use crate::entities::user::User;

impl Into<RecordId> for User {
    fn into(self) -> RecordId {
        return RecordId::from(("user", self.name.as_str()));
    }
}

Write query by using surreal_derive_plus::surreal_quote! macro

Struct

    use surreal_derive_plus::surreal_quote;
    .... connect to your surreal db ...
    
    let new_user = User {
        name: "surreal".to_string(),
        password: "000000".to_string(),
    };

    let created_user: Option<entities::user::User> = DB.query(surreal_quote!("CREATE #record(&user)")).await.unwrap().take(0).unwrap(); => CREATE user:surreal SET name='surreal', password='000000'

Variable

        let age = 2;
        let query_statement = surreal_derive_plus::surreal_quote!("CREATE user SET age = #age");

        assert_eq!(query_statement, "CREATE user SET age = 2")

Array

        let arr = vec![1,2,3,1];
        let query_statement = surreal_derive_plus::surreal_quote!("CREATE user SET arr = #array(&arr)");

        assert_eq!(query_statement, "CREATE user SET arr = [1, 2, 3, 1]")

Struct Arra

        let friends = vec![
            User {
                name: "clay".to_string(),
                full_name: "clay clay".to_string(),
                password: "123123".to_string(),
            },
            User {
                name: "joih".to_string(),
                full_name: "joih joih".to_string(),
                password: "123123".to_string(),
            }
        ];
        let query_statement = surreal_derive_plus::surreal_quote!("CREATE user SET friends= #array(&friends)");
        assert_eq!(query_statement, "CREATE user SET friends= [user:clay, user:joih]");

DateTime

        let dateBirth: DateTime<Utc> = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
        let query_statement = surreal_derive_plus::surreal_quote!("CREATE user SET brithday = #date(&dateBirth)");
        assert_eq!(query_statement, "CREATE user SET brithday = '2020-01-01T00:00:00Z");

Surreal ID

        let user =  User {
            name: "clay".to_string(),
            full_name: "clay clay".to_string(),
            password: "123123".to_string(),
        };

        let query_statement = surreal_derive_plus::surreal_quote!("UPDATE #id(&user) SET age = 10");
        assert_eq!(query_statement, "UPDATE user:clay SET age = 10");

Customize setting

You can customize the setting inside cargo.toml it is neccessary for call cargo clean to apply all of these configuration

[package.metadata]
# Will log the query command at runtime
surreal_enable_log = false
# Will log the generated code at build time
surreal_enable_compile_log = false
# Change the naming convention of generated statement into camel case
surreal_use_camel_case = false
# The log namespace, apply for both build time log and runtime log
surreal_namespace = "surrealql-derive"
# The macro name that use for info log, for example
surreal_info_log_macro = "println"
# The macro name that use for warning log, for example
surreal_warn_log_macro = "println"