Interacting with RethinkDB

Now that we've written both the router and the controller, it's time to turn back to RethinkDB and write the code that physically saves and loads data from the database. In the previous section, you learned how different functions in the controller call different functions in the model, so what we need to do now is implement these functions in the models/rethinkdb.js file.

As an example, let's focus on the function that creates a new note and saves it to the database: the user first creates the note from the app in his browser, then the browser performs an HTTP request that is received from the Node.js server and passed on to the router. At this point, the router handles the request and passes the data to the controller, which, in turn, extracts the data and calls the appropriate function in the model. In this case, the function to save a note in the model is called saveNote() and is implemented as this:

model.saveNote = function (note, done) {
    r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
        if(err) throw err;
        r.db('test').table('notes').insert(note).run(conn, function(err, results) {
            done();
        });
    });
}

This function does a few things. Let's look at them one by one. First, it creates a connection to the database on localhost, port 28015, which is the default port. If the connection is successful, we run an insert query on our RethinkDB cluster, saving the note object into the database. If you're wondering where this object comes from, it is passed by the controller and as a JavaScript object is represented in JSON format, we can pass this object directly to RethinkDB's insert function. Once the query succeeds, we call a callback function called done() that resumes the execution of the code in the controller.

Almost all other functions within the model behave similarly; let's see another example: the function that removes a note from the database:

model.deleteNoteByID = function (id, done) {
    r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
        if(err) throw err;
        r.db('test').table('notes').get(id).delete().run(conn, function(err, res) {
            if (err) throw err;
            done();
        });
    });
}

The controller calls this function, passing it the ID of the note to be removed. The model then connects to RethinkDB and runs a get() query to access the document specified by the ID and calls the delete() function on the document in order to remove it from the database. Just as in the previous example, we then call the provided callback function to resume the execution of the controller's code.

Implementing Changefeeds

There is one function in the model that needs to be mentioned, and we're talking about the function that uses Changefeeds to listen for updates. As you may remember, we said that we want our web application to provide updates to all clients in real time, and to do this, we need to write a function that listens for updates.

This function is called listenForUpdates() and is implemented as follows:

model.listenForUpdates = function(cb) {
     r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
        if(err) throw err;
        r.db('test').table('notes').changes().run(conn, function(err, cursor) {
            cursor.each(function(err, item) {
                cb(item);
            });
        });
    });
}

This function receives a single argument as input: a callback function. This is an important aspect as this callback is the function we're going to call every time the database gets updated.

Let's go over the function. First, we create a connection to the database, then we run a query on the notes table, appending the changes() command at the end of the query. You've learned how the changes command allows us to receive updates from RethinkDB; in fact, every time something is changed in the database (notes added, deleted, or updated), the changes are appended to the cursor. Finally, the function calls the callback function for every item present in the cursor.

This function is at the heart of the application's real-time behavior as this single function is responsible for getting updates from the database and pushing them to the application. The last step we need to do in order to implement real-time behavior is sending these updates to clients via WebSockets, and we're going to do this in the following section.

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

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