| Crates.io | surrealdb-types |
| lib.rs | surrealdb-types |
| version | 3.0.0-alpha.10 |
| created_at | 2025-09-16 13:28:18.606594+00 |
| updated_at | 2025-09-23 16:37:56.938457+00 |
| description | A scalable, distributed, collaborative, document-graph database, for the realtime web |
| homepage | https://github.com/surrealdb/surrealdb |
| repository | https://github.com/surrealdb/surrealdb |
| max_upload_size | |
| id | 1841665 |
| size | 216,739 |
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.
The surrealdb-types crate acts as a shared public value type system that:
The main Value enum represents all possible data types in SurrealDB:
use surrealdb_types::{Array, Datetime, Number, Object, Value};
use std::collections::BTreeMap;
// Basic types
let bool_val = Value::Bool(true);
let string_val = Value::String("hello".to_string());
let number_val = Value::Number(Number::Int(42));
// Complex types
let array_val = Value::Array(Array::from(vec![Value::String("item".to_string())]));
let object_val = Value::Object(Object::new());
let datetime_val = Value::Datetime(Datetime::now());
The Kind enum represents the type system used for schema validation and type checking:
use surrealdb_types::Kind;
// Basic kinds
let string_kind = Kind::String;
let number_kind = Kind::Number;
let array_kind = Kind::Array(Box::new(Kind::String), Some(10)); // Array of strings, max 10 items
The SurrealValue trait provides type-safe conversion between Rust types and SurrealDB values:
use surrealdb_types::{SurrealValue, Value};
// 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.is::<String>() {
println!("Value is a string");
}
// Convert from SurrealDB value to Rust type
let string = value.into::<String>().unwrap();
println!("Extracted string: {}", string);
Support for spatial data types using the geo crate:
use surrealdb_types::{Geometry, Value};
use geo::Point;
let point = Point::new(1.0, 2.0);
let geometry_val = Value::Geometry(Geometry::Point(point));
Type-safe representation of SurrealDB record identifiers:
use surrealdb_types::{RecordId, RecordIdKey, Value};
let record_id = RecordId {
table: "person".to_string(),
key: RecordIdKey::String("john".to_string()),
};
let record_val = Value::RecordId(record_id);
use surrealdb_types::{Value, Number, Array, Object};
use std::collections::BTreeMap;
// Create values
let values = vec![
Value::Bool(true),
Value::Number(Number::Int(42)),
Value::String("hello".to_string()),
Value::Array(Array::from(vec![Value::String("item".to_string())])),
];
// Work with objects
let mut map = BTreeMap::new();
map.insert("key".to_string(), Value::String("value".to_string()));
let object = Value::Object(Object::from(map));
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 surrealdb_types::{object, array};
let values = array![
true,
42,
"hello".to_string(),
array!["item1".to_string()],
];
let map = object! {
key: "value".to_string(),
};
SurrealValue traitSurrealValue traitanyhow in your dependencies, as the SurrealValue trait uses it for error handling.use surrealdb_types::SurrealValue;
#[derive(SurrealValue)]
struct Person {
name: String,
age: i64,
}
let person = Person {
name: "John".to_string(),
age: 30,
};
let value = person.into_value();
let person2 = Person::from_value(value).unwrap();
assert_eq!(person2.name, "John");
assert_eq!(person2.age, 30);
use surrealdb_types::{Value, SurrealValue};
fn process_value(value: &Value) {
match value {
Value::String(s) => println!("String: {}", s),
Value::Number(n) => println!("Number: {:?}", n),
Value::Array(arr) => println!("Array with {} items", arr.len()),
_ => println!("Other type"),
}
}
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 the SurrealValue traitsurrealdb-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 encodingThis crate is part of SurrealDB and follows the same licensing terms.