What is CouchDB-emily-tools?
CouchDB Emily tools synchronizes an Emily key/value observable store with a couchDB document, view or bulk of documents. It can further manipulate CouchDB User and Security documents. The exact same code will work in both the browser and in node.js, the only difference being the transport layer.
It can manipulate CouchDB documents:
- Get a CouchDB document and save it in a JavaScript object
- Update the JavaScript object when the document is updated in the database
- Upload a modified document to the database
- Create a document
- Remove a document
- publishes events when a property on the JavaScript object is added/removed/updated
It can manipulate CouchDB views:
- Get a CouchDB view and save it in a JavaScript array
- Update the JavaScript array when a document is added or removed from the databae
- Update the JavaScript array when a document is updated in the database
- publishes events when a document on the JavaScript array is added/removed/updated
It can manupulate CouchDB bulk documents:
- Get a CouchDB bulk of documents and save it in a JavaScript array
- Update the JavaScript array when a document is added or removed from the databae
- Update the JavaScript array when a document is updated in the database
- Update the database when one of the documents is updated
- Add or remove documents in the database when they are add or removed from the JavaScript array
- publishes events when a document on the JavaScript array is added/removed/updated
In other words, CouchDB emily tools will reflect the status of your CouchDB in observable JavaScript data stores, and you can subscribe to their changes to update the views.
It's a subtype of an Emily observable store:
http://flams.github.io/emily/#store
- It has getters/setters and update methods
- It publishes events when a property is added/modified
- It can be based on a JavaScript object for a key/value store
- It can be based on a JavaScript Array for an ordered list of items
- It can dump its current state
Synchronizing a CouchDBDocument to an existing document is as easy as:
var couchDBDocument = ; // Transport will make the link to the node.js server that issues the request.couchDBDocument; couchDBDocument ;
How to install it?
In node.js
CouchDB Emily Tools is based on Emily
npm install emily couchdb-emily-tools
var emily = CouchDBTools = ; // The url to CouchDB can be configured, it's localhost by defaultCouchDBToolsconfigurationhostname: "my.ip.address"; // The port can be configured too; 5984 being the default oneCouchDBToolsconfigurationport: 5984; // Add the CouchDB handler to Emily. The handler is what issues the requests to CouchDBemilyhandlers; // Now we can require and use the CouchDB Tools, here's an example with a document.var CouchDBDocument = CouchDBDocument transport = Transport; var cdb = transport = emilyhandlers; cdb; cdb;
On the client side
CouchDB-emily-tools requires Olives to work on the client side. Olives embeds Emily for you. It also expects you to have socket.io installed, and a store for storing sessions, like redis store, which is the only supported for now. In future implementations, redis store will probably be optional and an adapter will be accepted to any other store. An example can be found in the suggestions application
npm install olives couchdb-emily-tools
The server side has some specific configuration:
var olives = CouchDBTools = ; // sessionStore is a new redis-store: https://npmjs.org/package/connect-redisCouchDBToolsconfigurationsessionStore = sessionStore; // The name of the cookie sent to the client must be set tooCouchDBToolsconfigurationCookieID: "myApplication"; // Add the CouchDB handler to Olivesoliveshandlers;
And on the client side:
var CouchDBDocument = CouchDBDocument SocketIOTransport = SocketIOTransport; // Create a socket.io socketvar socket = io // Create a couchDBDocumentcdb = // And the transport that will issue the requests to CouchDBtransport = socket; cdb; cdb ;
API
After you've installed CouchDB Emily tools for use either on the server or the client side, you are ready to play with it. To cope with the asynchronous nature of database operations, CouchDB Emily Tools returns a new promise for each asynchronous method call, such as 'sync', 'upload' or 'remove'.
As the tools are based on Emily, the promises are completely compliant with the Promise/A+ specs, so you know how they work. The only extra feature that is added to Promise/A+ is the ability to give an extra scope to the fulfillment/error callbacks. All the examples below are valid:
couchDBDocument ; // or; // or; // or; // or;
Also, when a handler returns a new promise, the current promise will be resolved when the new one does. This basically allows you nicely chain operations on a couchDB tool, such as:
newDocument ;
CouchDBDocument API
CouchDBDocument is designed to allow you to perform all of the operations that are possible using standard HTTP requests.
Creating a CouchDBDocument
var CouchDBDocument = CouchDBDocument transport = Transport; var couchDBDocument = ; // check the installation section to see how to create the transport layer// depending on the environment (browser or node.js)couchDBDocument;
Synchronizing with a document
couchDBDocument; // this will save the following in the store's internal object://{"_id":"myDocument","_rev":"50-edaa4bb883be679e9407d7c4c0da15d6","name":"couchdb emily tools"} couchDBDocument; // couchdb emily tools
Synchronizing with a document with extra parameters, like a previous revision
You just have to add an extra JSON object that will be serialized to look like:
?rev=49-2eafd494d37475e4a2ca7255f6e582f2
couchDBDocument;
Creating a new document
couchDBDocument;
Removing an existing document
couchDBDocument;
Updating an existing document
couchDBDocument;
Unsynchronizing a synchronized document so it can be synchronized with another doc
couchDBDocument; couchDBDocument;
Listening for changes on a document
couchDBDocuments are a subtype of Emily's Store, so they publish events. When a document is updated in CouchDB, the changes are reflected in all synchronized CouchDBDocument.
documentA; documentB;
Attachements
Attachements are not yet supported, but if you clap your hands enough, it will eventually come :)
CouchDBView API
Creating a CouchDBView
var CouchDBView = CouchDBView Transport = Transport;var couchDBView = ; // check the installation section to see how to create the transport layer// depending on the environment (browser or node.js)couchDBView;
Synchronizing with a CouchDB View
A CouchDB View is readonly. Synchronizing a CouchDBView will return a list of documents that will be saved in the data store as documents in a JavaScript array. In a couchDBView, the document's properties are saved in the 'value' object.
couchDBView;
Adding extra parameters
Adding parameters to the request is as easy as adding an extra object. CouchDBView will serialize it.
couchDBView;
Watching for document added
When a new document appears in the current view, a "added" event is published
couchDBView;
Watching for document updated
When a document is updated in CouchDB, couchDBView will publish an "updated" event
couchDBView;
Watching for updates on a given document
couchDBView;
Watching for document removed
When a document is removed in CouchDB, couchDBView will publish a "deleted" event
couchDBView;
Unsynchronizing a CouchDBView:
Unsynching is required for synchronizing the store on another view.
couchDBView;
CouchDBBulkDocuments API
CouchDBBulkDocuments synchronizes the data store with a bulk of documents. New documents can be added or removed to the bulk documents to alter the database. This allows for batch updates of CouchDB.
Creating a CouchDBBulkDocuments
var CouchDBBulkDocuments = CouchDBBulkDocuments transport = Transport; var couchDBBulkDocuments = ; // check the installation section to see how to create the transport layer// depending on the environment (browser or node.js)couchDBBulkDocuments;
Synchronizing with a bulk of CouchDB documents
In a couchDBBulkDocuments, the document's properties are saved in the 'doc' object.
couchDBBulkDocuments;
Updating one or several documents
CouchDBBulkDocuments can also update documents in CouchDB by uploading the changes done in the data store.
// The first document now has a myProperty property with newValuecouchDBBulkDocuments; // The 4th document toocouchDBBulkDocuments; // Upload uploads all of themcouchDBBulkDocuments;
Removing one or several documents
CouchDBBulkDocuments can remove one or several documents by adding them the _deleted property set to true.
couchDBBulkDocuments; couchDBBulkDocuments;
Changelog
3.0.0 - 16 MAR 2014
- remove requirejs
- now works with Emily.js and Olives.js version 2.0.0
2.0.0 - 27 APR 2013
- Complete refactoring of the tools. Documents, BulkDocuments and Views are in distinct files.
- Fixed bugs in document creation/update
- Fixed bulk docs not updating the rev id after upload
- document.remove also returns a promise
- When a document doesn't exist, the promise is now fulfilled instead of rejected
- CouchDBStore doesn't exist anymore, a specific CouchDBView, CouchDBDocument or CouchDBBulkDocuments must be used instead
1.0.6 - 27 MAR 2013
- Aborting a non established connection doesn't fail anymore
1.0.5 - 25 MAR 2013
- Removed specific code imported from another application after 1.0.2
- Updated documentation
1.0.3 - 11 MAR 2013
- Updated emily + requirejs
1.0.2 - 01 JAN 2013
- Now using Emily 1.3.1 promises wich are fully compliant with promise/A+ specs
- Moved Emily's CouchDB handler to CouchDB Emily Tools
- Updated tests
1.0.1 - 08 OCT 2012
- CouchDBStore Views are compatible with couchdb-lucene's
1.0.0 - 07 OCT 2012
- CouchDBStore was removed from Emily and added to CouchDB-Emily-Tools.
- It's now a library by itself, as it concentrates more work than the rest of Emily.