Crate ohmers [] [src]

Object-hash mapping library for Redis.

Ohmers is a library for storing objects in Redis, a persistent key-value database. It is based on the Ruby library Ohm, and it uses the same key names, so it can be used in the same system.

Prerequisites

Have a redis server running and a redis-rs connection.

Getting started

Ohmers maps Rust structs to hash maps in Redis. First define the structs using the model! macro, and then use their methods to created, read, update, delete.


model!(Event {
    indices {
        name:String = "My Event".to_string();
    };
    venue:Reference<Venue> = Reference::new();
    participants:Set<Person> = Set::new();
    votes:Counter = Counter;
});

model!(Venue {
    name:String = "My Venue".to_string();
    events:Set<Event> = Set::new();
});

model!(Person {
    name:String = "A Person".to_string();
});
let p1 = create!(Person { name: "Alice".to_string(), }, &client).unwrap();
let p2 = create!(Person { name: "Bob".to_string(), }, &client).unwrap();
let p3 = create!(Person { name: "Charlie".to_string(), }, &client).unwrap();

let v1 = create!(Venue { name: "Home".to_string(), }, &client).unwrap();
let v2 = create!(Venue { name: "Work".to_string(), }, &client).unwrap();

let mut e1 = create!(Event { name: "Birthday Party".to_string(), }, &client).unwrap();
insert!(e1.participants, p1, &client).unwrap();
insert!(e1.participants, p2, &client).unwrap();
insert!(e1.participants, p3, &client).unwrap();
e1.venue.set(&v1);
e1.save(&client).unwrap();

let mut e2 = create!(Event { name: "Work Meeting".to_string(), }, &client).unwrap();
insert!(e2.participants, p1, &client).unwrap();
insert!(e2.participants, p2, &client).unwrap();
e2.venue.set(&v2);
e2.save(&client).unwrap();

Macros

collection!

Properties declared as Collection can use the collection macro to get a Query to iterate over all of its elements. A Collection is an accessor to objects that have a Reference to the object.

contains!

Checks if an element is in a List or a Set.

counter!
create!

Creates a new instance of $class using the default properties, overriding specified collection of $key with $value, and saving it in the database

decr!
find!

Returns a Query with all the $class objects where $key is $value. All the $key must be declared as indices in the model! declaration.

first!

Retrieves an element from the beginning of $obj.$prop. The property must be a List.

incr!
insert!

Insert $el in $obj.$prop. The property must be a Set.

last!

Retrieves an element from the end of $obj.$prop. The property must be a List.

len!

Number of elements in a List or Set property.

model!

Declares a struct. Fields may be declared as a part of uniques, indices, or regular fields. Every field must have a default value. The struct will derive RustcEncodable, RustcDecodable, and Default. More derives can be specified.

new!

Creates a new instance of $class using the default properties, overriding specified collection of $key with $value.

pop_back!

Retrieves and remove an element from the end of $obj.$prop. The property must be a List.

pop_front!

Retrieves and remove an element from the beginning of $obj.$prop. The property must be a List.

push_back!

Adds $el at the end of $obj.$prop. The property must be a List.

push_front!

Adds $el at the beginning of $obj.$prop. The property must be a List.

remove!

Removes occurences of an element in a List or a Set.

try_iter!

Creates an iterable of all elements in $obj.$prop. The property must be a List.

try_range!

Creates an iterable of $obj.$prop between $start and $end. The property must be a List.

Structs

Collection

A wrapper for classes that are referenced from another classes property.

Counter

Atomic counter

Iter

Iterator for query results

List

A list of elements.

Query

A query of a set, or a result of set operations.

Reference

A Reference to another Ohmer object.

Set

An unordered collection of items.

Enums

OhmerError
StalSet

A set of values. It can be generated from a Redis key or from a set operation based on other sets.

Traits

Ohmer

Structs that can be stored in and retrieved from Redis. You can use the model! macro as a helper.

Functions

all

Gets an iterator for all elements.

all_query

Gets a query for all elements.

get

Gets an element by id.

with

Find an element by a unique index.