Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Defining Scan Consistency in a Node.js With Couchbase Application

DZone's Guide to

Defining Scan Consistency in a Node.js With Couchbase Application

See how to define scan consistency within a Node.js application that will yield variable results in Couchbase N1QL queries.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

Have you ever created an application where you needed to save a document into Couchbase and then immediately query for that created document? It happens to me all the time when I’m developing. Take, for example, adding new data into a list where each list item is a new document. In many scenarios, you’ll want to update the UI that renders the list immediately after adding to it. The problem here is that the document you had just created may not show up in query results. This is because Couchbase indexers may not have processed the latest mutations against the Couchbase Bucket.

We’re going to see how to define our own scan consistency within a Node.js application that will yield variable results in our Couchbase N1QL queries.

Defining the scan consistency in an application will be similar regardless of the programming technology used. For this particular example, we’ll be using Node.js with both N1QL and Ottoman.js.

Before going forward, the assumption is that you’ve got Couchbase installed and configured for N1QL. This means that you have at least one index ready to go, even if it is a primary index.

Defining Scan Consistency With N1QL Queries

The first thing we’re going to worry about is N1QL related queries. Take a look at the following snippet of Node.js JavaScript code:

app.get("/endpoint", function(request, response) {
    var statement = N1qlQuery.fromString("SELECT META().id, `" + bucket._name + "`.* FROM `" + bucket._name + "`");
    bucket.query(statement, function(error, result) {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

By default, the SELECT query is unbounded. This means that the query will return only data that is currently indexed, yielding the fastest possible response. Remember, if our new data hasn’t been indexed, it won’t be returned in the results.

If we wanted to wait until our data was indexed, we can define our scan consistency by doing the following:

var statement = N1qlQuery.fromString("SELECT META().id, `" + bucket._name + "`.* FROM `" + bucket._name + "`");
statement.consistency(N1qlQuery.Consistency.REQUEST_PLUS);

Notice that we’ve changed the consistency in the above snippet to REQUEST_PLUS rather than leaving it unbounded. This means that the query won’t execute until all the mutations in the Bucket have been processed.

So what if we’re not using N1QL, but Ottoman.js instead?

Defining Scan Consistency With Ottoman.js

Ottoman is a little different, but the rules still apply because when using the find operator, N1QL is being used under the covers.

Take a look at the following JavaScript snippet:

app.get("/endpoint", function(request, response) {
    Person.find({}, function(error, result) {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

The goal here is to accomplish the same thing we had seen previously. Above, we are doing an unbounded query and will only receive results that were processed by the index.

This can easily be changed by the following:

app.get("/endpoint", function(request, response) {
    Person.find({}, { consistency: Ottoman.Consistency.LOCAL }, function(error, result) {
        if(error) {
            return response.status(500).send(error);
        }
        response.send(result);
    });
});

In the above code, LOCAL consistency is the same as saying REQUEST_PLUS.

Not so bad, right?

Conclusion

You just saw how to define your own scan consistency in Couchbase using Node.js and either N1QL or Ottoman. By default, queries are performance-first, but should you need to make adjustments (at least it is available to you as an option).

If you’d like to learn more about scan consistency and the available options, check out the Couchbase documentation on the subject.

Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

Topics:
database ,n1ql ,node.js ,couchbase ,consistency ,ottoman ,tutorial

Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}