Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Document-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation


Add to your gemfile:

gem 'redis_rejson_models', '~> 0.9.5'

Or without bundler, run in your shell:

gem i redis_rejson_models


  • Redis (v4+)
  • Re-JSON module installed

Instance API:

Here's a brief walktrough of how the Instance API works

In the code that folows, we defined a Document model, we did that by including the ShallowAttributes and the redis_rejson_models mixins and by defining some properties/attributes.

Here's an example code that you should look at first, RedisRejsonModel's usage.

In this first code sample we have an object instantiated via the Document class that has 2 attribute accessors: .name and `.content

We call this object doc, an object which not only has the properties name and contents, it has also a .save method.

.save() saves the object as a json object in redis using the rejson indexing, here's the json object saved in redis:

{ id: 234, name: "Foo", contents: "Bar123" }

in JSON format:

{ "id": 234, "name": "Foo", "contents": "Bar123" }

Here's the first sample code showing the instance api in all his glory:

doc = name: "Foo", contents: "Bar123"
puts      #=> "foo"
puts doc.antani    #=> "..." # saves the record, gives an auto increment id (example: 1)

doc = Document.get
puts      #=> "foo"
puts doc.antani    #=> "..."

Note: We use the shallow_attributes gem (already required by redis_rejson_models that provides the main ORM Model-like familiar ruby API that most ruby ORMs have / used to have)

Query API:

RedisRejsonModels not only has the name has also an instance api

Document.get 123 #=> returns a single redis rejson object as a whole { id: 123, name: "Foo", contents: "..." }

Document.get_attr 123, "name" #=> "name" # this is the fastest method to read an attribute, even if your json gets big, by using `Model.get_attr` you can be sure that you've got a very quick access and retrieval of that data

Document.all #=> returns a list of hashes querying all the entries from redis [{ id: 123, name: ... }, { id: 234, name: ... }, ...]

Create / Update

If you don't like setting properties and invoking .save() you should check out how RedisRejsonModels handles with a straightforward class-method-based api the creation and update of json records in redis.

# Document.create attrs
doc = Document.create name: "Foo", contents: "..." #=> has the new auto increment id (e.g. `123`)

# Document.update id, attrs
Document.update 123, name: "Foo2" 

doc = Document.get 123 #=> "Foo2"

Model Definition:

Finally by looking at the definition we can clearly see that it's very sensible, we use ShallowAttributes attributes and RedisRejsonModels gives us all the extra method for persisting the model data.

class Document
  extend  RedisRejsonModelLib
  include RedisRejsonModelMixin
  include ShallowAttributes

  attribute :id,        Integer
  attribute :name,      String
  attribute :contents,  String

  def antani
    "hi #{name}, what do you think about #{contents} ?"

treat your models as a normal ruby object (not even a PORO, just a normal object :D) and you'll go long way, maximum testability, crazy redis speeds and it's just json data, treated as a document-based database.

Run local Redis (+ Re-JSON module) locally via Docker:

in this repo run:

docker-compose up

Which use the docker-compose.yml to start your redis.

Set up your Redis and RJSON

# sample environment setup
require 'bundler'
Bundler.require :default

# setup redis
R =

# load and configure redis with rejson models
include RedisRejsonModels
RJ.configure redis: R

class Document
  # ...

Check a sample project contained in the example dir of this repo:

Run specs

(you need to run a local redis + rejson service - explained above)


rake spec




Document-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation







No releases published


No packages published