compound data structures for couchbase built in node.js

Couchbase Structures

compound data structures for couchbase built in node.js

This project creates helpful wrappers around normal couchbase documents to allow you to create more complex objects. Being couchbase, this means that your objects will be redundant and distributed!

Note that this code it likley to be very buggy and slow. You were warned!

npm install couchbase-structures

var couchbase = require("couchbase"); 
var CouchbaseStructures = require("couchbase-structures");
var couchbase_config = {
  debug    : false,
  hosts    : [ "localhost:8091" ],
  password : "password",
  bucket   : "demo",
  user     : "demo"
couchbase.connect(couchbase_config, function(errbucket){
    // arrays 
    var arr = new CouchbaseStructures.array("myArray", bucket);
      arr.set(0, {'name': 'array thing 1'}, function(err){
        arr.set(5, {'name': 'array thing 5'}, function(err){
            console.log(length) // 6 
            arr.get(5, function(errdoc){
              console.log(doc); // doc = {'name': 'array thing 5'} 
            arr.get(3, function(errdoc){
              console.log(doc); // doc = null 
    // queues 
    var queue = new CouchbaseStructures.queue("myQueue", bucket);
      queue.push({'name': 'queue thing #1'}, function(err){
        queue.push({'name': 'queue thing #2'}, function(err){
            console.log(length) // 2 
              console.log(doc); // doc = {'name': 'queue thing #1'} 
              console.log(doc); // doc = {'name': 'queue thing #2'} 
    // hash 
    var hash = new CouchbaseStructures.hash("myHash", bucket);
      hash.set('key_1', {'name': 'hash thing #1'}, function(err){
        hash.set('key_3', {'name': 'hash thing #3'}, function(err){ 
            console.log(length) // 2 
            hash.get('key_3', function(errdoc){
              console.log(doc) // {'name': 'hash thing #3'} 
            hash.get('some_other_key', function(errdoc){
              console.log(doc) // null 

You can run these tests from demo.js, which is included in this project.

The only configuration options are key seperators and timeout options. You can overide the defaults like so:

var CouchbaseStructures = require("couchbase-structures");
CouchbaseStructures.structure.prototype.counterPrefix = function(){ return "_counter"; }
CouchbaseStructures.structure.prototype.createPlaceholder = function(){ return "PLACEHOLDER"; }
CouchbaseStructures.structure.prototype.keySeperator = function(){ return ":"; }
CouchbaseStructures.structure.prototype.lockDuration = function(){ return 10; } // seconds 
CouchbaseStructures.structure.prototype.lockAttempts = function(){ return 10; }
CouchbaseStructures.structure.prototype.lockWaitSleep = function(){ return 100; } // miliseconds 

All of the types defined in this project inherit from structure. This class holds the primitives for getting and setting child documents, updating metadata, etc. The public methods are listed here:

  • bucket is a couchbase bucket object returned from esablishing a connection
  • the name should be unique
  • used to create a new object. Will fail if the document already exists
  • used to load in a previously created object
  • used to delete an object and all children
  • set a key in an array
  • an id can be set (0 -> infinity). This will increase the size of the array if needed
  • return an element of an array
  • possible to get an out of bounds error
  • get the size of the array (including null values)
  • get every item in the array
  • this is likely to be very slow
  • push a document to the end of the queue
  • return and remove the first element from a queue
  • possible to null if the queue is empty
  • get the size of the queue
  • get every item in the queue
  • this is likely to be very slow

Note that these hashes differ from normal couchbase documents in that they will allow you to update one key of the hash indepentandly from the the entirety of the document.

  • set a key in an hash
  • return an element of a hash
  • null will be returned if the key is not set
  • get the count of the set keys in a hash
  • get every item in the hash
  • this is likely to be very slow

npm test should be all you need. Be sure that your couchbase settings match what is defined in the test:

var couchbase_config = {
  "debug" : false,
  "hosts" : [ "localhost:8091" ],
  "password" : "password",
  "bucket" : "test",
  "user" : "test"