First try with RethinkDB

I just wrapped up a project where we wanted to store some statistical data and a team member said they wanted to use MongoDB or RethinkDB. I recommended CouchDB for it but I wanted to see what RethinkDB was and if it was a good fit.

I discovered it has query language called ReQL and other interesting features. I used it both locally and on a hosting service called compose.io.

brew install rethinkdb
npm install rethinkdb
rethinkdb

It will launch an admin interface on port 8080. (Example images below)

Since the API I was writing was a node app I used the default driver offered by the group who does RethinkDB. The driver enables connection to the server and queries via ReQL. There are other drivers including one with connection pooling and other ORMs. In this case I wasn’t concerned about more advanced features and we were only implementing a few simple calls.

Connect

var r = require('rethinkdb'),
    con;
r.connect(api.config, function(err, connection) {
    if (err) {
        // connect error
    }
    // connected
    con = connection
});

Write a ReQL query

r.db('httpstats')
    .table('requests')
    .filter({ user: 'tbone' })
    .filter({ ip: '127.0.0.1' })
.run(con, function(err, cursor) {
    if (err) {
        // error running query
    }
    cursor.each(function(err, row) {
        if (err) {
            // row error
        }
        // do something with the data
        console.log(row.id);
    }, function(err) {
        // iteration via each complete
    });
});

As you can see it’s all pretty straightforward. By now many of us are accustomed to the chained style of function calls on JS APIs. The only problem I had was trying to query on the timestamps. I experimented with date strings, ISO8601 dates, and finally ended up with UNIX timestamps.

There are many features I’ll continue learning about including how to watch for real-time changes to tables. It was a pleasant experience all-in-all implementing this project using RethinkDB. We got the job done and it works well even at load.

rethinkdb admin query

rethinkdb admin interface

Author Tony Crowe, Salt Lake City, UT
top
home
Attribution
Lato font by Łukasz Dziedzicwww.latofonts.com/team
Roboto font by Christian Robertson, Google Incchristianrobertson.comwww.google.com/fonts
Inconsolata font by Raph Levien, Google Incwww.levien.comwww.google.com/fonts
hexo: fast, simple & powerful blog frameworkhexo.io
ace editorace.c9.io
virtual-domwww.npmjs.com/package/virtual-dom
Black Granite Water Droplets, William Warbyhttps://www.flickr.com/photos/wwarby