natevw / fermata-couchdb

CouchDB helpers for Fermata
1 stars 0 forks source link

fermata-couchdb

NOTE: the currently-published fermata@0.10.6 already bundles this plugin, this is split out for an upcoming version!

This is a CouchDB plugin for Fermata. It primarily offers a watchChanges helper for monitoring a _changes feed, as well as a few other convenience helpers where it makes sense.

Setup

For classic in-browser usage, copy the main script file for both Fermata and this plugin alongside your static files. Then:

<script src="https://github.com/natevw/fermata-couchdb/raw/master/your/static/files/fermata.js"></script>
<script src="https://github.com/natevw/fermata-couchdb/raw/master/your/static/files/fermata-couchdb.js"></script>
<script>
var db = fermata.couchdb()('my-database');    // will try to auto-detect server

// … see example usage below …
</script>

Under node.js/browserify you will again need to npm install fermata fermata-couchdb. Then in your code, you will need to register this plugin manually:

var fermata = require('fermata'),
    couchdb = require('fermata-couchdb);
fermata.registerPlugin('couchdb', couchdb);   // you could pick a different name if you'd like

var db = fermata.couchdb()('my-database');
// …

Example usage

TBD: improve, and actually test, this…

var autoDatabase = fermata.couchdb()('my-database'),    // will try to auto-detect server
    corsDatabase = fermata.couchdb("https://other-server.example.com")('your-database');

// implement a lousy sort of "replicator", copying changed docs from the other server into our own…
fermata.plugins.couchb.watchChanges(corsDatabase({since:'now', include_docs:true}), function (arr) {
    arr.forEach(function (change) {
      autoDatabase.put(change.doc, function (e,d) {
          if (e) console.error(e, d);
          else console.log("New rev is:", d.rev);
      });
    });
});

Plugin API

The following documentation assumes this plugin has been registered with the name 'couchdb' (as by default the browser, or if you follow the node.js example above).

Changes watcher API

Parameters to the .watchChanges(databaseUrl, lastSeq, callback[, interval]) method introduced above are as follows:

The .watchChanges(…) method will return an watcher object with the following methods:

Alternate Cloudant / BigCouch (/ CouchDB 2.0?) extended plugin

Cloudant's "BigCouch" version of CouchDB does not actually enforce read quorums! This means that if a quorum write request yields a 202 response, there is no way to ever know whether the write ended up conflicting or not. So my recommendation is to treat such writes as failed from the upstream code's perspective.

To facilitate this, this plugin offers a variant which, in the browser, is registered under the "bigcouch" name. To register under node.js you can follow this example, changing the variable names and/or registered name if you'd like:

var fermata = require('fermata'),
    plugin = require('fermata-couchdb');
fermata.registerPlugin('bigcouch', plugin.bigcouch);

var db = fermata.bigcouch()('my-database');
// …

This alternate plugin is used exactly as the main plugin. The only difference is that a response with a 202 status code will cause an error callback rather than a successful callback.

License (MIT)

Copyright © 2011–2015 Nathan Vander Wilt.

Released under the terms of the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.