RethinkDB is a JSON based, non-relational database that provides a promise oriented, Node JS backend. It integrates seamlessly with JSON type data and is a production ready option for Node infrastructures.
Pre-reqs: Docker, Node, NPM
You can pull and run the official rethink docker image to start the database locally. Simply give it a name and you’re on your way!
docker run -d -P --name <your container name> rethinkdb
To check the port mappings in docker, simply run
docker port <your container name>
This will show you something like this:
28015/tcp -> 0.0.0.0:32769
29015/tcp -> 0.0.0.0:32768
8080/tcp -> 0.0.0.0:32770
Each of the local port mappings appear on the right and the docker container exposed ports are on the left.
So, if you wanted to access the containers 8080 port, we would navigate to
localhost:32770. We can see this from the example as
8080/tcp -> 0.0.0.0:32770.
Alternatively, you can install rethinkdb for your specific machine and run it locally. This can be found on the RethinkDB install page. Using the docker container & image is a nice, light weight, modular way to run rethink, similar to how a production microservice architecture might be configured. I also like being able to control the exact environment that my rethink database is running in, it’s ports, and other fun docker quality of life things!
If using the docker container, navigate to the Admin Panel by going to
localhost:32770 in a browser. From our previous example, we can see that this local port is mapped to the docker container port 8080 (which is the web admin panel). If you’re running rethink on your machine locally, you should be able to simply navigate to
In the admin pannel, you create new databases, explor data, see logs, track performance, and see what connections are running. Lets create a database with a few tables.
In the top navigation bar, go to the “Data Explorer” and enter the following:
These raw rethink queries create and build our initial database. This can also be acomplished from “Tables” in the top navigation bar or right in your node app! However, the “Data Explorer” is an essential tool for viewing, manuipulating, and creating data. This is a great link for useful Data Explorer queries.
In order to use the rethink drivers in our Node app, we need to install them via NPM. From the command line:
npm install rethinkdb
node_modules folder will now contain the necessary rethink drivers for accessing our rethink instance. To access the rethink drivers from your Node app, require the drivers:
const r = require('rethinkdb');
let connection = null;
Now, the connection variable will hold the raw data necessary to connect to the rethink instance. Make special note of what port you specify. This should be the port that maps to 28015 in the docker container.
For this local instance of the rethinkdb, we won’t worry too much about dynamic ports or not exposing the ports to the public in production. Here is a good article about one way you can create production ready ports and configurations.
This step can be quite complicated. You can do a number of things per your needs, including placing this step into some middleware to connect automatically, check the configuration of your database, reconfigure settings if something is wrong, or validate authorization. Check out this repository from the rethink people for more complex operations around connecting.
// Inserts 2 battleships
We can see that we are inserting raw JSON objects! Awesome! Now, from the Data explorer, if we query the
We will see the following JSON has been entered into the database:
// Get all battle ships
In this example, we are getting all the ships in the battle_ships table. Most rethink queries of this size will return a cursor by default, so to get the raw results, we must make it an array with the
.toArray method. The callback will contain the results that can than be parsed further.
// Get specific battleship
This gets a single ship from the battle_ships table based on the primary key. The primary key is the ID automatically assigned to the inserted JSON. The results we get back in the callback function is the JSON object of the provided key.
// Update the length of The Texas battle ship
Here, we update a ship’s length by providing an updated JSON object. Note that we don’t need to provide all fields of the object in order for it to be updated. Once we
run the query, the returned result will be what was updated in the database. This snippet also introduces the
.filter rethink method. This can be used to pull specific records based on a number of conditions. Finding json objects this way is very powerful and can be chained with other queries. Almost anything you can do with SQL or Mongo, you can do with rethink queries. Check out this awesome page for some really useful queries.
// Remove the Iowa battle ship
Here, we again use the
.filter method to find a document in the database. Then, we delete it using the
.delete() rethink method. After running this query, the JSON will be removed from the database.