Working with the Collections Developer Preview

    +
    Collections is introduced as a Developer Preview feature in Couchbase Server 6.5. The 3.0 API SDKs all work with Collections and Scopes. As a practical demonstration, we have a collections-enabled version of the Travel Sample application.

    The Developer Preview of the upcoming Collections feature in Couchbase Server is fully implemented in the 3.0 API versions of the Couchbase SDKs. When working with other server versions, the defaultcollection is used from the SDK. Here we show how to access individual collections in the Developer Preview version of Couchbase Server 6.5, with a collections-enabled version of our Travel Sample application. User documents and flight documents are split into user and flight collections. Something that previously had to be done with key-value references to different types or categories of data.

    Travel Sample app — with Collections

    Travel Sample Application uses the Travel Sample data Bucket, which ships with Couchbase Server. For Couchbase Server 6.5, make sure that you have at least one node each of data; query; index; and search. For a development box, mixing more than one of these on a single node (given enough memory resources) is perfectly acceptable. If you have yet to install Couchbase Server in your development environment, start here.

    Then load up the Travel Sample Bucket, using either the Web interface or the command line. You will also need to create a Search Index — Query indexes are taken care of by the Sample Bucket.

    Enabling Developer Preview should only be done on a development machine; there is no upgrade path available from a DP-enabled Couchbase Server.

    Preparation

    As well as the Node.js SDK 3.0 and Couchbase Server, set up as described above, you will need git to fetch the travel sample application code:

    $ git clone https://github.com/couchbaselabs/try-cb-nodejs.git

    Change directory into your cloned repository, and check out the Collections branch (in the case of the Node.js SDK, 6.5-collections).

    $ git checkout 6.5-collections
    • Before building the Collections-enabled version of the Travel Sample Application, you need to enable this DP feature (see warning above).

      Enable Collections Developer Preview
      $ /opt/couchbase/bin/couchbase-cli enable-developer-preview --enable -c http://localhost:8091 -u Administrator -p password
      Developer preview cannot be disabled once it is enabled. If you enter developer preview mode you will not be able to upgrade. DO NOT USE IN PRODUCTION.
      Are you sure [y/n]: y

    The Travel Sample Bucket needs altering to be split into collections. There is a script to do this included with the Travel Sample App — run:

    Create Sample Collections
    $ sh create-collections.sh Administrator password 127.0.0.1

    adjusting for any changes you have made to server URL, or admin password. You should now have the Travel Sample Data Bucket split into collections:

    {"uid":"1"}{"uid":"2"}{"uid":"3"}
    
    THE FINAL RESULT
    {"uid":"3","scopes":[{"name":"userData","uid":"8","collections":[{"name":"flights","uid":"9"},{"name":"users","uid":"8"}]},{"name":"_default","uid":"0","collections":[{"name":"_default","uid":"0"}]}]}

    Running the Travel Sample Application

    Next, edit the storage.host field in src/main/resources/application.properties to the one for your containerised Couchbase Server (or localhost, 127.0.0.1, if appropriate), and any other local changes — such as password. From here onwards, we’ll assume the defaults.

    And run with

    $ npm run start

    After the build, with your Web browser of choice, head to port 8080 of the local machine — http://localhost:8080.

    Using the Sample App is the same as with the non-collections version that we cover in our introductory doc, but we’re assuming you’ve come here to see Collections in use in the codebase.

    Sample App Backend

    The backend code shows Couchbase Node.js SDK in action with Query and Search, but also how to plug together all of the elements and build an application with Couchbase Server and the Node.js SDK.

    Collections and Scope are set immediately after opening the bucket:

    
    // Open travel-users bucket, with scope and specific collections
    // The bucket, scope and collection must be created already
    // curl -X POST -u Administrator:password http://127.0.0.1:8091/pools/default/buckets -d name=travel-users -d ramQuotaMB=10 -d authType=none
    // curl -X POST -v -u Administrator:password http://localhost:8091/pools/default/buckets/travel-users/collections -d name=userData // scope
    // curl -X POST -v -u Administrator:password http://localhost:8091/pools/default/buckets/travel-users/collections/userData -d name=users // collection
    
    try {
    const sampleOptions = {username: 'Administrator', password: 'password'};
    const sampleCluster = new couchbase.Cluster("http://localhost", sampleOptions);
    const sampleBucket = sampleCluster.bucket("travel-users");
    const sampleScope = sampleBucket.scope("userData");
    sampleColl = sampleScope.collection("users");
    } catch (e){
            console.log("travel-user bucket, scope or collection not set up?");
    	console.log(e);
    }

    And operations are now performed on the specific collection, rather than the whole bucket (or the whole bucket via _default collection, as in Server 6.5 without the Developer Preview enabled).

      result = await sampleColl.get(user, function(err, doc) {
          if (err) {
            if (err.cause && !err.cause.code!=301) { // something other than 'does not exist'
              console.log('err='+err);
            }
          }
    });