SurrealDB Types
This crate provides the shared public value type system for SurrealDB. It serves as a foundational layer that defines all the data types that can be stored and manipulated in SurrealDB.
Purpose
The surrealdb-types crate acts as a shared public value type system that:
- Provides type safety: Offers strongly-typed representations of all SurrealDB data types
- Enables serialization: Supports serialization/deserialization of values
- Facilitates type conversion: Provides traits and methods for converting between Rust types and SurrealDB values
Core Types
Value Types
The main Value enum represents all possible data types in SurrealDB:
use ;
use BTreeMap;
// Basic types
let bool_val = Bool;
let string_val = String;
let number_val = Number;
// Complex types
let array_val = Array;
let object_val = Object;
let datetime_val = Datetime;
Type System
The Kind enum represents the type system used for schema validation and type checking:
use Kind;
// Basic kinds
let string_kind = String;
let number_kind = Number;
let array_kind = Array; // Array of strings, max 10 items
Key Features
Type Conversion
The SurrealValue trait provides type-safe conversion between Rust types and SurrealDB values:
use ;
// Convert from Rust type to SurrealDB value
let value: Value = "hello".to_string.into_value;
// Check if a value is of a specific type
if value.
// Convert from SurrealDB value to Rust type
let string = value.into_t::.unwrap;
println!;
Geometric Types
Support for spatial data types using the geo crate:
use ;
use Point;
let point = new;
let geometry_val = Geometry;
Record Identifiers
Type-safe representation of SurrealDB record identifiers:
use ;
let record_id = RecordId ;
let record_val = RecordId;
Usage
Basic Usage
use ;
use BTreeMap;
// Create values
let values = vec!;
// Work with objects
let mut map = new;
map.insert;
let object = Object;
Macros for object & array values
This library provides two macros to easily create object and array values. All values you pass to the macro must implement the SurrealValue trait.
use ;
let values = array!;
let map = object! ;
Deriving the SurrealValue trait
Requirements
- All fields must implement the
SurrealValuetrait
use SurrealValue;
let person = Person ;
let value = person.into_value;
let person2 = from_value.unwrap;
assert_eq!;
assert_eq!;
Type Checking
use ;
Dependencies
This crate has minimal external dependencies:
serde: For serialization/deserializationchrono: For datetime handlinguuid: For UUID supportrust_decimal: For decimal number supportregex: For regular expression supportgeo: For geometric typessurrealdb-types-derive: For deriving theSurrealValuetraitsurrealdb-protocol: For the SurrealDB protocolflatbuffers: For the FlatBuffers protocolbytes: For the Bytes typeanyhow: For error handlinggeo: For geometric typesregex: For regular expression supportrust_decimal: For decimal number supportuuid: For UUID supportrstest: For testinghex: For hex encoding
License
This crate is part of SurrealDB and follows the same licensing terms.