Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
couchdb-emily-tools
Advanced tools
Complete set of tools for synchronizing an observable key/value stores with CouchDB documents, views, and managing users or security documents. Works both in the browser and in node.js
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:
####It can manipulate CouchDB views:
####It can manupulate CouchDB bulk documents:
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
Synchronizing a CouchDBDocument to an existing document is as easy as:
var couchDBDocument = new CouchDBDocument();
// Transport will make the link to the node.js server that issues the request.
couchDBDocument.setTransport(transport);
couchDBDocument.sync("myDatabase", "myDocument")
.then(function () {
// Will return the structure of the document exactly as it exists in the database
couchDBDocument.toJSON();
})
.then(function () {
couchDBDocument.set("myProperty", "hello");
return couchDBDocument.upload();
})
.then(function () {
// At this point, the document has been updated in CouchDB with a new property
couchDBDocument.toJSON(); // Will have a myProperty property
// and we can also remove the document from CouchDB if we want.
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");
// The url to CouchDB can be configured, it's localhost by default
CouchDBTools.configuration.hostname: "my.ip.address";
// The port can be configured too; 5984 being the default one
CouchDBTools.configuration.port: 5984;
// Add the CouchDB handler to Emily. The handler is what issues the requests to CouchDB
emily.handlers.set("CouchDB", CouchDBTools.handler);
// Now we can require and use the CouchDB Tools, here's an example with a document.
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");
// sessionStore is a new redis-store: https://npmjs.org/package/connect-redis
CouchDBTools.configuration.sessionStore = sessionStore;
// The name of the cookie sent to the client must be set too
CouchDBTools.configuration.CookieID: "myApplication";
// Add the CouchDB handler to Olives
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;
// Create a socket.io socket
var socket = io.connect("http://localhost"),
// Create a couchDBDocument
cdb = new CouchDBDocument,
// And the transport that will issue the requests to CouchDB
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);
// or
.then(this.onSuccess, this.onError);
// or
.then(this.onSuccess, this, this.onError);
// or
.then(this.onSuccess, this, this.onError, this);
// or
.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 () {
// Creates the document
return this.upload();
}, newDocument)
.then(function () {
// Updates the document
this.set("property", "hello");
return this.upload();
}, newDocument)
.then(function () {
// Removes the document
this.remove();
}, newDocument);
##CouchDBDocument API
CouchDBDocument is designed to allow you to perform all of the operations that are possible using standard HTTP requests.
var CouchDBDocument = require("tools").CouchDBDocument,
transport = require("emily").Transport;
var couchDBDocument = new CouchDBDocument();
// check the installation section to see how to create the transport layer
// depending on the environment (browser or node.js)
couchDBDocument.setTransport(transport);
couchDBDocument.sync("myDatabase", "myDocument").then(...);
// this will save the following in the store's internal object:
//{"_id":"myDocument","_rev":"50-edaa4bb883be679e9407d7c4c0da15d6","name":"couchdb emily tools"}
couchDBDocument.get("name"); // couchdb emily tools
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(...);
couchDBDocument.sync("myDatabase", "newDocument").then(function () {
this.upload();
}, couchDBDocument);
couchDBDocument.sync("myDatabase", "oldDocument").then(function () {
this.remove();
}, couchDBDocument);
couchDBDocument.sync("myDatabase", "oldDocument").then(function () {
// Will add or update the newField property to the document
this.set("newField", "myValue");
// Will update it in CouchDB
this.upload();
}, couchDBDocument);
couchDBDocument.sync("myDatabase", "oldDocument").then(funciton () {
this.unsync();
}, couchDBDocument);
couchDBDocument.sync("myDatabase", "otherDocument").then(...);
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); // Will log "hello!" when documentB will be uploaded!
}, this);
}, documentA);
documentB.sync("myDatabase", "myDocument").then(function () {
this.set("field", "hello!");
this.upload();
}, documentB);
Attachements are not yet supported, but if you clap your hands enough, it will eventually come :)
##CouchDBView API
var CouchDBView = require("tools").CouchDBView,
Transport = require("emily").Transport;
var couchDBView = new CouchDBView();
// check the installation section to see how to create the transport layer
// depending on the environment (browser or node.js)
couchDBView.setTransport(transport);
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 () {
// {
// "id" : "documentA",
// "key" : "documentA",
// "value" : {
// "_id" : "documentA",
// "_rev" : "25-201d5eb10f46c4a85676ff44540a4f1e",
// "newProperty" : "hello!"
// }
// }
couchDBView.get(0);
// The number of items in the view
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(...);
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);
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);
couchDBView.watchValue(0, function (newValue, action) {
this.get(0) === newValue;
action; // "updated"
});
When a document is removed in CouchDB, couchDBView will publish a "deleted" event
couchDBView.watch("deleted", function onDocumentDeleted(index) {
this.get(index); // undefined
}, 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.
var CouchDBBulkDocuments = require("tools").CouchDBBulkDocuments,
transport = require("emily").Transport;
var couchDBBulkDocuments = new CouchDBBulkDocuments();
// check the installation section to see how to create the transport layer
// depending on the environment (browser or node.js)
couchDBBulkDocuments.setTransport(transport);
In a couchDBBulkDocuments, the document's properties are saved in the 'doc' object.
couchDBBulkDocuments.sync("myDatabase", {
keys: ["document1", "document2", "document3"]
}).then(function () {
// {
// "id" : "document2",
// "key" : "document2",
// "value" : {
// "rev" : "31-73ef4535724ff2db0a2361c1dab813e7"
// },
// "doc" : {
// "_id" : "document2",
// "_rev" : "31-73ef4535724ff2db0a2361c1dab813e7"
// }
// }
this.get(1);
this.count(); // 3
});
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 newValue
couchDBBulkDocuments.update(0, "doc.myProperty", "newValue");
// The 4th document too
couchDBBulkDocuments.update(3, "doc.myProperty", "newValue");
// Upload uploads all of them
couchDBBulkDocuments.upload();
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();
###3.0.0 - 16 MAR 2014
####2.0.0 - 27 APR 2013
####1.0.6 - 27 MAR 2013
####1.0.5 - 25 MAR 2013
####1.0.3 - 11 MAR 2013
####1.0.2 - 01 JAN 2013
####1.0.1 - 08 OCT 2012
####1.0.0 - 07 OCT 2012
FAQs
Complete set of tools for synchronizing an observable key/value stores with CouchDB documents, views, and managing users or security documents. Works both in the browser and in node.js
The npm package couchdb-emily-tools receives a total of 12 weekly downloads. As such, couchdb-emily-tools popularity was classified as not popular.
We found that couchdb-emily-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.