2 releases
0.2.1 | May 7, 2024 |
---|---|
0.2.0 | May 7, 2024 |
#144 in Database implementations
12KB
72 lines
surql-definition
surql-definition
is a Rust crate that provides a unified interface for generating SurrealDB table and field definitions. It re-exports functionality from surql-definition-macros
and surql-definition-core
.
Table of Contents
Features
- Re-exports the
SurQLDefinition
derive macro fromsurql-definition-macros
. - Re-exports the
SurQLSchemaProducer
trait fromsurql-definition-core
. - Simplifies the process of defining and validating SurrealDB schemas.
Installation
Add surql-definition
to your Cargo.toml
:
[dependencies]
surql-definition = "0.2.1"
Usage
To use surql-definition
, import the relevant items as needed:
use surql_definition::{SurQLDefinition, SurQLSchemaProducer};
Examples
Defining a SurrealDB Table
The following example demonstrates how to use the SurQLDefinition
macro to define a SurrealDB table:
use surql_definition::SurQLDefinition;
#[derive(SurQLDefinition)]
struct User {
id: u64,
name: String,
email: String,
}
assert_eq!(
User::schema_query(),
"DEFINE TABLE user; DEFINE FIELD id ON user TYPE int; DEFINE FIELD name ON user TYPE string; DEFINE FIELD email ON user TYPE string;"
);
Using SurQLSchemaProducer
The SurQLSchemaProducer
trait allows you to manually implement a schema query for a struct:
use surql_definition::SurQLSchemaProducer;
struct Product;
impl SurQLSchemaProducer for Product {
fn schema_query() -> &'static str {
"DEFINE TABLE product;"
}
}
assert_eq!(Product::schema_query(), "DEFINE TABLE product;");
Validation
surql-definition
supports runtime and compile-time validation of generated queries through the features provided by surql-definition-macros
.
To enable validation, update your Cargo.toml
:
[dependencies]
surql-definition = { version = "0.2.1", features = ["runtime_query_validation"] }
License
This project is licensed under the MIT License. See the LICENSE file for details.
Links
Dependencies
~0.2–17MB
~238K SLoC