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 = new CouchDBDocument();
couchDBDocument.setTransport(transport);
couchDBDocument.sync("myDatabase", "myDocument")
.then(function () {
couchDBDocument.toJSON();
})
.then(function () {
couchDBDocument.set("myProperty", "hello");
return couchDBDocument.upload();
})
.then(function () {
couchDBDocument.toJSON();
couchDBDocument.remove();
});
##How to install it?
####In node.js
CouchDB Emily Tools is based on Emily
npm install emily couchdb-emily-tools
var emily = require("emily"),
CouchDBTools = require("couchdb-emily-tools");
CouchDBTools.configuration.hostname: "my.ip.address";
CouchDBTools.configuration.port: 5984;
emily.handlers.set("CouchDB", CouchDBTools.handler);
var CouchDBDocument = require("tools").CouchDBDocument,
transport = require("emily").Transport;
var cdb = new CouchDBDocument,
transport = new Transport(emily.handlers);
cdb.setTransport(transport);
cdb.sync("mydatabase", "mydocument")
.then(function () {
console.log(cdb.toJSON());
}, function (error) {
console.log(error);
});
####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 = require("olives"),
CouchDBTools = require("couchdb-emily-tools");
CouchDBTools.configuration.sessionStore = sessionStore;
CouchDBTools.configuration.CookieID: "myApplication";
olives.handlers.set("CouchDB", CouchDBTools.handler);
And on the client side:
<script src="/socket.io/socket.io.js"></script>
<script src="emily.js"></script>
<script src="olives.js"></script>
<script src="CouchDBTools.js"></script>
var CouchDBDocument = require("tools").CouchDBDocument,
SocketIOTransport = require("olives").SocketIOTransport;
var socket = io.connect("http://localhost"),
cdb = new CouchDBDocument,
transport = new SocketIOTransport(socket);
cdb.setTransport(transport);
cdb.sync("mydatabase", "mydocument")
.then(function () {
console.log(cdb.toJSON());
});
##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.sync("database", "document")
.then(this.onSuccess, this);
.then(this.onSuccess, this.onError);
.then(this.onSuccess, this, this.onError);
.then(this.onSuccess, this, this.onError, this);
.then(this.onSuccess, this.onError, this);
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.sync("database", "newDocument")
.then(function () {
return this.upload();
}, newDocument)
.then(function () {
this.set("property", "hello");
return this.upload();
}, newDocument)
.then(function () {
this.remove();
}, 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 = require("tools").CouchDBDocument,
transport = require("emily").Transport;
var couchDBDocument = new CouchDBDocument();
couchDBDocument.setTransport(transport);
Synchronizing with a document
couchDBDocument.sync("myDatabase", "myDocument").then(...);
couchDBDocument.get("name");
You just have to add an extra JSON object that will be serialized to look like:
?rev=49-2eafd494d37475e4a2ca7255f6e582f2
couchDBDocument.sync("myDatabase", "myDocument", {
"rev": "49-2eafd494d37475e4a2ca7255f6e582f2"
}).then(...);
Creating a new document
couchDBDocument.sync("myDatabase", "newDocument").then(function () {
this.upload();
}, couchDBDocument);
Removing an existing document
couchDBDocument.sync("myDatabase", "oldDocument").then(function () {
this.remove();
}, couchDBDocument);
Updating an existing document
couchDBDocument.sync("myDatabase", "oldDocument").then(function () {
this.set("newField", "myValue");
this.upload();
}, couchDBDocument);
Unsynchronizing a synchronized document so it can be synchronized with another doc
couchDBDocument.sync("myDatabase", "oldDocument").then(funciton () {
this.unsync();
}, couchDBDocument);
couchDBDocument.sync("myDatabase", "otherDocument").then(...);
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.sync("myDatabase", "myDocument").then(function () {
this.watchValue("field", function (newValue) {
console.log(newValue);
}, this);
}, documentA);
documentB.sync("myDatabase", "myDocument").then(function () {
this.set("field", "hello!");
this.upload();
}, 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 = require("tools").CouchDBView,
Transport = require("emily").Transport;
var couchDBView = new CouchDBView();
couchDBView.setTransport(transport);
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.sync("myDatabase", "myDesignDocument", "_view/myView").then(function () {
couchDBView.get(0);
couchDBView.count();
});
Adding parameters to the request is as easy as adding an extra object. CouchDBView will serialize it.
couchDBView.sync("myDatabase", "myDesignDocument", "_view/myView", {
startkey: "documentA",
endKey: "documentZ",
limit: 10,
skip: 10
}).then(...);
Watching for document added
When a new document appears in the current view, a "added" event is published
couchDBView.watch("added", function onDocumentAdded(index, value) {
this.get(index) === value;
}, couchDBView);
Watching for document updated
When a document is updated in CouchDB, couchDBView will publish an "updated" event
couchDBView.watch("updated", function onDocumentUpdated(index, value) {
this.get(index) === value;
}, couchDBView);
Watching for updates on a given document
couchDBView.watchValue(0, function (newValue, action) {
this.get(0) === newValue;
action;
});
Watching for document removed
When a document is removed in CouchDB, couchDBView will publish a "deleted" event
couchDBView.watch("deleted", function onDocumentDeleted(index) {
this.get(index);
}, couchDBView);
Unsynchronizing a CouchDBView:
Unsynching is required for synchronizing the store on another view.
couchDBView.unsync();
##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 = require("tools").CouchDBBulkDocuments,
transport = require("emily").Transport;
var couchDBBulkDocuments = new CouchDBBulkDocuments();
couchDBBulkDocuments.setTransport(transport);
Synchronizing with a bulk of CouchDB documents
In a couchDBBulkDocuments, the document's properties are saved in the 'doc' object.
couchDBBulkDocuments.sync("myDatabase", {
keys: ["document1", "document2", "document3"]
}).then(function () {
this.get(1);
this.count();
});
Updating one or several documents
CouchDBBulkDocuments can also update documents in CouchDB by uploading the changes done in the data store.
couchDBBulkDocuments.update(0, "doc.myProperty", "newValue");
couchDBBulkDocuments.update(3, "doc.myProperty", "newValue");
couchDBBulkDocuments.upload();
Removing one or several documents
CouchDBBulkDocuments can remove one or several documents by adding them the _deleted property set to true.
couchDBBulkDocuments.loop(function (document) {
document.doc._deleted = true;
}, this);
couchDBBulkDocuments.upload();
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.