RethinkDB: JavaScript Docs

The RethinkDB team has done a great job providing detailed documentation for their database platform. Out of the box, RethinkDB has 3 included drivers: JavaScript, Python, & Ruby. Each is API well documented.

Oddly enough, Google appears to favor the Python flavor of docs in it's results. So I often end up looking at a slightly different code example than I need.

The good news is, converting formats isn't difficult. To avoid having to search each function you find in the docs in the API, here are a few tips to help you quickly convert between the two.

It is worth noting the RethinkDB built in Data Explorer only accepts the JavaScript syntax. If you are getting syntax errors, try the tips below.

1. snake_case to camelCase.

RethinkDB uses best practices for naming convention in each drivers language respectfully. This means funct_names in the docs become functNames in JavaScript. The first character is lowercase, and then each word is uppercased.

Example:
// Ruby: Select table and order results
r.table('marvel').order_by('enemiesVanquished').run(conn, callback)

// JavaScript: Select table and order results
r.table('marvel').orderBy('enemiesVanquished').run(conn, callback)

2. Pass Options as a JavaScript Object

When you are using a function that takes optional parameters, Ruby allows to pass the variable and value directly to the function. This will cause a parse error in JavaScript. But, again, it is easy to translate into JavaScript.

Example:
// Ruby: Create multi-indexes
r.table("posts").index_create("tags", multi=True)

// JavaScript: Create multi-indexes
r.table("posts").indexCreate("tags", { multi: true })

3. Lambda's become Anonymous Functions

JavaScript does not support Lambda syntax, but it does have it's own immediately invoked function expressions (IIFEs) which we can use in it's place. Replace the lambda with a function, and pass the lamba variable into the new function.

Example:
// Ruby: Create a compound index
r.table("dc").index_create("parental_planets", lambda hero:
    r.add(user["mothers_home_planet"], "_", user["fathers_home_planet"])).run(conn)

// JavaScript: Create a compound index
r.table('dc').indexCreate('parental_planets', function(hero) {
    return [hero('mothers_home_planet'), hero('fathers_home_planet')];
}).run(conn, callback)

4. Still Stuck? Look It Up

As I mentioned, each API has specific, detailed, instruction. Go the JavaScript API docs, and Ctrl+F5, then type in the first few letters of the function you're working with, it'll be there.

Small dog web development studio
View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News