Operation basics

Operations use basic JavaScript data types and callback functions.

Data types

All JavaScript data types that can be serialized via the JSON functions are supported by default. Recursive structures cannot be serialized. All textual data is represented by UTF-8 when stored to Couchbase Server. You may alternatively implement your own transcoders which modify the method by which your documents are serialized to Couchbase. The following is an example of a transcoder which will transcode all documents as UTF-8 encoded JSON documents.

bucket.setTranscoder(function(value) {
  return {
    value: new Buffer(JSON.stringify(value), 'utf8')
    flags: 0
  };
}, function(doc) {
  return JSON.parse(doc.value.toString('utf8'));
});

Callbacks

The Node.js Couchbase driver uses a callback pattern to notify the application when results are ready. You pass callback functions to the operation methods and the callbacks are invoked when the results or errors are ready. All storage and retrieval operations (both singular and batch methods) follow the same callback pattern, but the format of the results differs.

Callbacks use the following parameters:

  • error

    Singular operation. If no errors occurred during the operation this parameter contains null. If an error occurred, it contains an Error object.

    Batch operation. This parameter contains the number of errors that occurred on any of the batched operations.

  • result

    Singular operation. This parameter contains one object with the properties listed in the following table.

    Batch operation. This parameter contains an object where the keys match the keys your operation targeted and the values contain an object with the properties listed in the following table.

    Result objects contain the following properties:

    Name Description
    value For retrieval-type operations, this field contains the value of the requested key.
    error For batch operations, this field contains an Error object representing any errors that occurred during execution of this particular operation.
    cas An opaque object representing the resulting CAS value of the key that was operated on. This value is not meant to be user facing, but should be passed directly to other operations for locking purposes.