redis-om
A Rust/Redis ORM-style library that simplify the development process and reduce the amount of boilerplate code needed to build programs that leverage redis powerful capabilities and use cases.
Status: WIP, fully testsed, possible breaking changes, stay tuned
Features
- ORM-style API to define/manipulate redis data structures (e.g. hashes, json, streams) using derive macros.
- Automatic serialization/desalinization between Redis data and rust objects.
- Interoperability with serde, e.g. using
rename
,rename_all
orserde
. - Nested hash datatype support (e.g.
list.1
or nested modelsaccount.balance
as keys).
Usage
- Getting Started
- Using Redis's Hash Datatype
- Using Redis's Json Datatype
- Using Redis's Stream Datatype
Roadmap
- 0.1.0
- Enable users to define and derive Hash Model with most common methods
- Enable users to define and derive JSON Model with most common methods
- Enable users to define and derive streams with managers to publish-to/read-from them.
- Support users to choose between asynchronous and synchronous runtime.
- 0.2.0
- Enable Multi-Stream Manager Support to enable users to combine multiple
RedisModels
. - Support Serializing/deserializing
HashModel
complex fields using serde. - Support
RedisSearch
and provide query-building API. - .....
- Enable Multi-Stream Manager Support to enable users to combine multiple
- 0.3.0
- Support validation of struct fields and enum values (most likely using validator library).
- .....
Getting Started
= { = "*" }
# TLS support with async-std
= { = "*", = ["tls"] }
# async support with tokio
= { = "*", = ["tokio-comp"] }
# async support with async-std
= { = "*", = ["async-std-comp"] }
# TLS and async support with tokio
= { = "*", = ["tokio-native-tls-comp"] }
# TLS support with async-std
= { = "*", = ["async-std-tls-comp"] }
Hash
use HashModel;
// Now that we have a `Customer` model, let's use it to save customer data to Redis.
// First, we create a new `Customer` object:
let mut jane = Customer ;
// Get client
let client = open.unwrap;
// Get connection
let mut conn = client.get_connection.unwrap;
// We can save the model to Redis by calling `save()`:
jane.save.unwrap;
// Expire the model after 1 min (60 seconds)
jane.expire.unwrap;
// Retrieve this customer with its primary key
let jane_db = get.unwrap;
// Delete customer
delete.unwrap;
assert_eq!;
Json
redis-om support json data type through redis_om::JsonModel
. It requires that the type
derives serde::Deserialize
as well as serde::Serialize
.
use JsonModel;
use ;
// Now that we have a `Account` model, let's use it to save account data to Redis.
// First, we create a new `Account` object:
let mut john = Account ;
// Get client
let client = open.unwrap;
// Get connection
let mut conn = client.get_connection.unwrap;
// We can save the model to Redis by calling `save()`:
john.save.unwrap;
// Expire the model after 1 min (60 seconds)
john.expire.unwrap;
// Retrieve this account with its primary key
let john_db = get.unwrap;
// Delete customer
delete.unwrap;
assert_eq!;
Stream
redis-om support json data type through redis_om::StreamModel
. It requires that any nested type to derives redis_om::RedisTransportValue
.
use ;
/// An enum of room service kind
/// An enum of room service kind
// rename stream key in redis
// Get client
let client = open.unwrap;
// Get connection
let mut conn = client.get_connection.unwrap;
// Create a new instance of Room service Event Manager with consumer group.
// Note: consumer name is auto generated,
// use RoomServiceEventManager::new_with_consumer_name, // for a custom name
let manager = new;
// Ensure the consumer group
manager.ensure_group_stream.unwrap;
// Create new event
let event = RoomServiceEvent ;
// Publish the event to the RoomServiceEvent redis stream
publish.unwrap;
// Read with optional read_count: Option<usize>, block_interval: Option<usize>
let read = manager.read.unwrap;
// Get first incoming event
let incoming_event = read.first.unwrap;
// Get first incoming event data
let incoming_event_data = incoming_event..unwrap;
// Acknowledge that you received the event, so other in the consumers don't get it
ack.unwrap;
assert_eq!;