CouchTard
Documentation Index
Introduction
Usage
var ct = require; // Couchtard only manipulates objects derived from the Model prototype.// Basic is provided as an example, it will keep up to date information about// creation/update dates, types and id as properties of the document. var models = ; var bucket = new bucket; var stream = new Stream;
Bucket
bucket.add
// Add one itembucket;// Add multiple itemsbucket;
bucket.get
// Get one itembucket;// Get multiple itemsbucket;
bucket.save
// Save one itembucket;// Save multiple items (array)bucket;// Save multiple items (hash)bucket;
bucket.delete
// Delete one itembucket;// Delete multiple itemsbucket;
bucket.lock
// Lock one itembucket;// Lock multiple itemsbucket;
Note: Saving locked item(s) will release the lock
bucket.unlock
// Unlock one itembucket;// Unlock multiple items (array)bucket;// Unlock multiple item (hash)bucket;
bucket.populate
var item1 = rel_key: 'xxxx-xxxx-xxx';var item2 = rel_key: 'yyyy-yyyy-yyy'; // Get related models instancesbucket;// Get related models instances for multiple items (array)bucket;// Get related models instances for multiple items (hash)bucket;
Models
bucket.new
// Create Item from Model Artists, with key "artist-1"var item = bucket; // If the key is ommited, a random key will be generatedvar item = bucket;
model.add
// Add item (insert)item;
model.save
// Save itemitem;
model.update
// Update (modify and save) itemitem;
model.delete
// Delete itemitem;
model.populate
// Delete itemvar item = rel_key: 'xxxx-xxxx-xxx';item;
Views
The following exemple assume that the view ddoc/view contents are :
"key": "Shiny Strawberry" "id": "item1" "value": null "key": "Shiny Dance Gown" "id": "item2" "value": null "key": "Dull Thingamabob" "id": "item3" "value": null
bucket.view
The basic view mechanism, mostly a wrapper around Couchabase's ViewQuery
// Fetch view resultsbucket;
The prefered way of using views is through the use of a delegate function
// Create view delegate { var opts = startkey: name endkey: name+'\uefff'; bucket;}// Fetch results from delegate with argsbucket;
bucket.glimpse
To use a view as an counter and retrieve the total number of matching items.
This excpects the view to have a _count
reduce function
// Get the number (total) of elements whose name starts with "Shiny"var opts = startkey: 'Shiny' endkey: 'Shiny\uefff';bucket;
// Get the number of elements whose name starts with "Shiny" and the first resultvar opts = startkey: 'Shiny' endkey: 'Shiny\uefff' limit: 1;bucket;
bucket.gaze
To use a view as an index and retrieve the emmiting items rather that the emmited values.
This will in fact also call glimpse, excpecting the view to have a _count
reduce function
// Fetch all items whose name starts with "Shiny"var opts = startkey: 'Shiny' endkey: 'Shiny\uefff' limit: 1;bucket;
The _gaze
primitive does not call _glimpse
// Fetch all items whose name starts with "Shiny"var opts = startkey: 'Shiny' endkey: 'Shiny\uefff' limit: 1;bucket;
Streaming
The streaming support is a parallelized, efficient way of querying large views. It basicaly is a node event stream, that will call a worker function with chunks of results untill done, and works as a drop-in replacement of bucket._view and bucket.gaze.
It recognize two special view parameters
chunkSize
: Maximum number of items to pass on to a worker functionconcurency
: Maximum number of simultaneous workers functions to call
stream.view
// Fetch results for items whose name start with "Shiny"var opts = startkey: 'Shiny' endkey: 'Shiny\uefff' chunkSize: 1;stream;
stream.gaze
// Fetch all items whose name starts with "Shiny"var opts = startkey: 'Shiny' endkey: 'Shiny\uefff' chunkSize: 1;stream;