Now that we have everything installed, it's time to start interacting with RethinkDB through Node.js. In the following examples, we'll be adding some fake data to a table in our RethinkDB cluster. We'll be running these commands on a server running Ubuntu Linux.
To start, let's look at how to connect to our RethinkDB instance. First, make sure that your database is up and running. If it's not running, you can start RethinkDB by executing the following command from a terminal window:
sudo /etc/init.d/rethinkdb start
This will start the database. Now, let's have a look at the code used to connect to our instance:
r.connect({host: 'localhost', port: 28015 }, function(err, conn) { // });
As you can see, the RethinkDB module provides us with a connect
function, which creates a new connection to the database. This function accepts a few parameters, such as the server host and port. Optionally, you can also specify the name of the database and an authentication key (more information on this is covered in the following chapter). When the connection is established, the provided callback function will be called.
Now that we know how to connect to the database, let's put this into practice by connecting to the database and creating a new table. As this is an extremely simple example, you can run it from the Node.js CLI without creating a source file.
First, run the node
command, and then type the following code:
$ node >r = require('rethinkdb'); // 1 >r.connect({host: 'localhost', port: 28015}, function(err, conn) { // 2 if(err) throw err; // 3 r.db('test').tableCreate('fake_data').run(conn, function(err, res) { // 4 if(err) throw err; // 5 console.log(res); // 6 }); });
If the command succeeds, the resulting JSON output will be similar to this:
{ config_changes:[ { new_val: [Object], old_val: null } ], tables_created: 1 }
Let's have a closer look at the code by going over it step by step:
require
command, which instructs Node to import the RethinkDB module and assign it to a local variable called r
. From now on, we can use this variable to access RethinkDB's API.connect
function, which we took a look at earlier. We pass this function our database's hostname (localhost) and port (default is 28015
). When a connection is established, our callback function will be called to pass it two parameters: err
and conn
. The first variable tells us if there are any errors, whereas the second variable represents the connection to the database. We will then use this variable to run queries.err
variable. If it is set to true, there has been an error and the connection is unsuccessful, so we throw an error, and if not, then the following line is executed.test
database, and then we create a new table in this database by chaining the tableCreate
command at the end of the query. To actually run the query, we append the run
command at the end of the query. This command requires two arguments: a valid connection to the database and a callback
function that is executed when the query is complete. When the callback
function is called, it is passed two variables, the err
variable and the res
variable, that represent the query result.res
variable—the query result.Although, all this may seem complicated at first, when you get used to the syntax, you will find it quite intuitive as the syntax of the queries is identical to the ones that we executed through the web interface; the only difference is that in Node.js, we need to append the run
command at the end of the query.
Now that we know how to run a simple query, let's move on to something more interesting.
3.145.97.170