Couchbase Binary Collection increment

I’m trying to write a very simple data loader script which uses the binary collections increment to increment a document which stores a counter of the current numerical id against a prefix (e.g. 1 => rank_1 , 2 => rank_2, where rank_1 is a document id)

Once we have a document id, I’m trying to do a simple upsert into the collections.

2 questions:

  1. Why does my node.js app hang if i use the promise .then and .catch after increment? It feels like I need to explicitly close a connection or something, but I’m not sure what to do. If I comment the entire binary increment function with all the .then and .catch out, the node.js main app that calls this runs through fine.

  2. In the main documentation, code sample is given that supports await syntax

e.g. var upsertResult = await collection.upsert(“my-document”, {name: ‘mike’});

but when you actually try to use this with node.js sdk 3.0 from npm and a couchbase 6.5 server, you get an error in using this syntax? Is this actually ready to be used?

const couchbase = require(‘couchbase’);

const cluster = new couchbase.Cluster(‘couchbase://localhost’, { username: ‘Administrator’, password: ‘password’ });

const upsertData = (bucketName, docType, data) => {

bucketName = bucketName.toLowerCase()

docType = docType.toLowerCase()

const bucket = cluster.bucket(bucketName);

const coll = bucket.defaultCollection();

const binColl = coll.binary();

//data = data.slice(0, 4)

console.log('upserting ' + docType + ' into bucket ' + bucketName + '...')

let counterName = bucketName + '_' + docType + '_counter'

binColl.increment(counterName, 1, { initial: "1" }).then( value => {

    

    // due to the implementation of the binary increment done as a promise, we need to implement an async

    // callback and quickly iterate through the batch of documents we want to insert/update and then increment

    // the counter all the way up to the last id we used

    // e.g. we initially increment counter to 1, and then blast 30 documents, and then increment the counter to 30 in one step

    // it would be safer to increment by steps of 1 lock-step, but not without the ability to use await and do it in a synchronized

    // fashion in a for-loop

    console.log(value.value);

    let i = 0;

    for(var count = value.value; i < data.length; i++,count++) {

        let docID = docType + '_' + count

        console.log(docID);

        console.log(data[i]);

        upsertObj(coll, docID, data[i])

    }

    binColl.increment(counterName, i-1).then( value => {                

        console.log(value.value);

        console.log('finished incrementing counter!')

    }).catch( err => {

        console.log(err)

    })

    

}).catch( err => {

    console.log(err)

})

}

Hey @fredwang00,

The SDK maintains persistent connections to the server for performance, thus in order to gracefully shut down the Node.js event loop you must call cluster.close() first. In regards to the use of promises, all methods also implement a callback pattern which should allow you to write callback-based code if you are not interested in using Promises or async/await.

collection.binary().increment('key', 1, (err, res) => {
  // ...
})

Cheers, Brett