Connecting to RethinkDB

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.

Running a simple query

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:

  • In line 1, we call Node's 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.
  • Line 2 calls the 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.
  • In line 3, we check the value of the 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.
  • Line 4 is our first query! First, we select the 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.
  • Line 5 should be familiar to you; once again, we check if the previous query has been successful; if not, we throw an error.
  • Finally, line 6 prints the contents of the 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.97.170