rethinkdb
Version, currently 0.1.117 versions
- 0.3.1latestSep 9, 2021
- 0.3.0not indexedMar 22, 2022
- 0.2.3not indexedMar 22, 2022
- 0.2.2not indexedMar 22, 2022
- 0.2.1not indexedMar 22, 2022
- 0.2.0not indexedMar 22, 2022
- 0.1.11not indexedMar 22, 2022
- 0.1.10not indexedMar 22, 2022
- 0.1.9not indexedMar 22, 2022
- 0.1.8not indexedMar 22, 2022
- 0.1.7not indexedMar 22, 2022
- 0.1.6not indexedMar 22, 2022
- 0.1.5not indexedMar 22, 2022
- 0.1.4not indexedMar 22, 2022
- 0.1.3not indexedMar 22, 2022
- 0.1.2not indexedMar 22, 2022
- 0.1.1not indexedMar 22, 2022
github.com/place-labs/crystal-rethinkdb
driver for rethinkdb / rebirthdb
Nothing has been indexed for 0.1.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
rethinkdb:
github: place-labs/crystal-rethinkdb
version: ~> 0.1.1Then run:
shards installshard.yml
No shard.yml has been indexed for 0.1.1. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
Crystal RethinkDB
This is a RethinkDB Driver for the Crystal Language.
Installation
Add this to your application's shard.yml:
dependencies:
rethinkdb:
github: kingsleyh/crystal-rethinkdb
Usage
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 "rethinkdb"
include RethinkDB::Shortcuts
# Let’s connect and create a table:
conn = r.connect(host: "localhost")
r.db("test").table_create("tv_shows").run(conn)
# Now, let’s insert some JSON documents into the table:
r.table("tv_shows").insert([
{name: "Star Trek TNG", episodes: 178},
{name: "Battlestar Galactica", episodes: 75}
]).run(conn)
# 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 "rethinkdb"
include RethinkDB::Shortcuts
conn = r.connect(host: "localhost", db: "my_database", user: "bob", password: "secret")
Read more about users and permissions here: https://rethinkdb.com/docs/permissions-and-accounts/
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::QueryResultRethinkDB:CursorRethinkDB::Array(RethinkDB::QueryResult)
Inserting
r.table("users").insert({
name: name, email: email, password: password,
activeChannel: {} of String => String,
channels: [] of String, groups: [] of String,
isOnline: false
}
).run(@connection)
r.table("messages").insert({channelId: channelId, userId: userId, content: content, date: r.now.to_iso8601}).run(@connection)
Finding All
r.table("users").map{|u|
{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"]}
}.run(@connection)
r.table("groups").map{|g|
{id: g["id"], name: g["name"], landingChannel: r.table("channels").filter({isLanding: true, groupId: g["id"]})[0]["id"]}
}.run(@connection)
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"]
}
}.run(@connection)
Finding One
r.table("users").get(userId).run(@connection)
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"]
}
}.run(@connection).to_a.first
Updates
r.table("users").get(userId).update({isOnline: isOnline}).run(@connection)
r.table("users").get(userId).update{|u|
{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: #{@env.database.name}"
begin
r.db_drop(@env.database.name).run( @connection)
rescue ex
puts ex.message
end
puts "creating database: #{@env.database.name}"
r.db_create(@env.database.name).run(@connection)
# add tables
puts "adding tables: users, groups, channels, messages"
r.db(@env.database.name).table_create("users").run(@connection)
r.db(@env.database.name).table_create("groups").run(@connection)
r.db(@env.database.name).table_create("channels").run(@connection)
r.db(@env.database.name).table_create("messages").run(@connection)
puts "done"
end
Contributing
- Fork it (https://github.com/kingsleyh/crystal-rethinkdb/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- kingsleyh Kingsley Hendrickse - creator, maintainer
- caspiano Caspian Baska - contributor, maintainer
- fenicks Christian Kakesa - contributor
Thanks
- Guilherme Bernal for his hard work on which this project is based.
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.1- Tagged
- Mar 22, 2022
- Commit
912360ee3d53- Indexed
- not yet
Dependents
Repository
github.com/place-labs/crystal-rethinkdb
Metadata
- Created
- Aug 18, 2026
- Updated
- Sep 26, 2026
- Synced
- Sep 26, 2026
- Versions
- 17