compound data structures for couchbase built in node.js
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(err, bucket){
if(err){
console.log(err);
process.exit();
}else{
// arrays
var arr = new CouchbaseStructures.array("myArray", bucket);
arr.create(function(err){
arr.set(0, {'name': 'array thing 1'}, function(err){
arr.set(5, {'name': 'array thing 5'}, function(err){
arr.length(function(err, length){
console.log(length) // 6
arr.get(5, function(err, doc){
console.log(doc); // doc = {'name': 'array thing 5'}
});
arr.get(3, function(err, doc){
console.log(doc); // doc = null
});
});
});
});
});
// queues
var queue = new CouchbaseStructures.queue("myQueue", bucket);
queue.create(function(err){
queue.push({'name': 'queue thing #1'}, function(err){
queue.push({'name': 'queue thing #2'}, function(err){
queue.length(function(err, length){
console.log(length) // 2
queue.pop(function(err, doc){
console.log(doc); // doc = {'name': 'queue thing #1'}
});
queue.pop(function(err, doc){
console.log(doc); // doc = {'name': 'queue thing #2'}
});
});
});
});
});
// hash
var hash = new CouchbaseStructures.hash("myHash", bucket);
hash.create(function(err){
hash.set('key_1', {'name': 'hash thing #1'}, function(err){
hash.set('key_3', {'name': 'hash thing #3'}, function(err){
hash.length(function(err, length){
console.log(length) // 2
hash.get('key_3', function(err, doc){
console.log(doc) // {'name': 'hash thing #3'}
});
hash.get('some_other_key', function(err, doc){
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
object returned from esablishing a connectionout of bounds
errornull
values)null
if the queue is emptyNote 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.
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"
}