Skip to content
driver for rethinkdb / rebirthdb
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This is a RethinkDB Driver for the Crystal Language.

Build Status Crystal Version

WARNING: This is only a basic driver a lot of functions are not implemented.


This driver is mostly a copy of this project: cubos/ (quickly) updated to work for Crystal 26.1 (and now 0.28.0). It is designed to work with the rethinkdb V1_0 release and has the user authentication mechanism implemented which was taken from this project: rethinkdb-lite

Thanks to these great projects it was not too hard to create this one. Unfortunately those other 2 projects are not being maintained and the project has more of the api implemented but the code is not as well structured as the newer rethinkdb-lite project. However rethinkdb-lite has a lot of missing functionality so I made the decision to fix up the original project and add in the authentication from the newer one.

I will try to do more work on this library over time. Thanks to Guilherme Bernal for his hard work on which this project is based.

Thanks also to Caspian Baska for all the awesome contributions.


Add this to your application's shard.yml:

    github: kingsleyh/crystal-rethinkdb


This library is meant to be compatible with RethinkDB's Ruby API. Thus, all official documentation should be valid here. If you find something that behaves differently, please open an issue.

require "crystal-rethinkdb"
include RethinkDB::Shortcuts

# Let’s connect and create a table:

conn = r.connect(host: "localhost")

# Now, let’s insert some JSON documents into the table:

  {name: "Star Trek TNG", episodes: 178},
  {name: "Battlestar Galactica", episodes: 75}

# We’ve just inserted two rows into the tv_shows table. Let’s verify the number of rows inserted:

pp r.table("tv_shows").count().run(conn)

# Finally, let’s do a slightly more sophisticated query. Let’s find all shows with more than 100 episodes.

p r.table("tv_shows").filter {|show| show["episodes"] > 100 }.run(conn).to_a

# As a result, we of course get the best science fiction show in existence.

Connecting as a user

If you made a user called bob with password secret in the rethinkdb system table users e.g.:

r.db('rethinkdb').table('users').insert({id: 'bob', password: 'secret'})
require "crystal-rethinkdb"
include RethinkDB::Shortcuts

conn = r.connect(host: "localhost", db: "my_database", user: "bob", password: "secret")

Read more about users and permissions here:

Useful Queries

Here are some more complex queries - mostly as a reminder to myself on how to do various more complicated things:

Something to note is that depending on the query you write you could get back one of these 3 things:

  • RethinkDB::QueryResult
  • RethinkDB:Cursor
  • RethinkDB::Array(RethinkDB::QueryResult)
                      name: name, email: email, password: password,
                      activeChannel: {} of String => String,
                      channels: [] of String, groups: [] of String,
                      isOnline: false
r.table("messages").insert({channelId: channelId, userId: userId, content: content, date:}).run(@connection)
Finding All
         {id: u["id"], name: u["name"], isOnline: u["isOnline"]}
      }.run(@connection) }
r.table("users").filter{|u| r.expr(u["groups"]).contains(groupId) }.map{|u|
         {id: u["id"], name: u["name"], isOnline: u["isOnline"]}
         {id: g["id"], name: g["name"], landingChannel: r.table("channels").filter({isLanding: true, groupId: g["id"]})[0]["id"]}
r.table("users").filter({id: userId}).map{|user|
                         channels: r.table("channels").filter{|ch| ch["groupId"] == groupId}.coerce_to("array"),
                         activeChannel: r.branch(user["activeChannel"].has_fields("channelId"),
                                          {groupId: user["activeChannel"]["groupId"], channelId: user["activeChannel"]["channelId"], name: r.table("channels").get(user["activeChannel"]["channelId"])["name"]},
                                          {groupId: "", channelId: "", name: ""}),
                         groupId: r.table("groups").get(groupId)["id"],
                         name: r.table("groups").get(groupId)["name"]
Finding One
  r.table("users").filter({id: userId}).map{|user|
                        channels: r.table("channels").filter{|ch| r.expr(user["channels"]).contains(ch["id"])}.filter{|ch| ch["groupId"] == groupId}.coerce_to("array"),
                        groups: r.table("groups").filter{|g| r.expr(user["groups"]).contains(g["id"])}.map{|g| {id: g["id"], name: g["name"], landingChannel: r.table("channels").filter({isLanding: true, groupId: g["id"]})[0]["id"]}}.coerce_to("array"),
                        messages: r.table("messages").filter{|m| m["channelId"] == channelId }.map{|m| {messageId: m["id"], userId: m["userId"], name: r.table("users").get(m["userId"])["name"], content: m["content"], date: m["date"]} }.coerce_to("array"),
                        channel: r.table("channels").get(channelId),
                        group: r.table("groups").get(groupId),
                        userIsOnline: user["isOnline"]
r.table("users").get(userId).update({isOnline: isOnline}).run(@connection)
        {channels: u.get_field("channels").set_insert(channelId),
         groups: u.get_field("groups").set_insert(groupId),
         activeChannel: {groupId: groupId, channelId: channelId}
      }.run(@connection) }
r.table("users").get(userId).update{|u| {groups: u.get_field("groups").set_insert(groupId)}}.run(@connection)
Creating a database
def recreate_database
      puts "dropping database: #{}"

        r.db_drop( @connection)
      rescue ex
        puts ex.message

      puts "creating database: #{}"

      # add tables
      puts "adding tables: users, groups, channels, messages"

      puts "done"


  1. Fork it (
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


  • kingsleyh Kingsley Hendrickse - creator, maintainer
  • Caspiano Caspian Baska - contributor
You can’t perform that action at this time.