fermata-couchdb
Advanced tools
Comparing version 0.10.7 to 0.10.8
@@ -16,5 +16,10 @@ /* CouchDB (and friends) plugin for Fermata, with preliminary support for change events and default base URL. | ||
(function () { | ||
function autoBase(url) { | ||
// TODO: better URL guessing (especially if window.location is available) | ||
return url || "http://localhost:5984"; | ||
} | ||
var plugin = function (transport, url) { | ||
// TODO: better URL guessing (especially if window.location is available) | ||
this.base = url || "http://localhost:5984"; | ||
this.base = autoBase(url); | ||
return transport.using('statusCheck').using('autoConvert', "application/json"); | ||
@@ -133,5 +138,28 @@ }; | ||
// WORKAROUND: custom statusCheck to treat 202 responses as an error | ||
// if Cloudant sends this, we can't be sure the write won't conflict | ||
// see https://github.com/cloudant/bigcouch/issues/55#issuecomment-30186518 | ||
// and https://cloudant.com/for-developers/faq/data/ | ||
// and http://bigcouch.cloudant.com/api | ||
// it's futile to attempt a unanimous quorum read to be sure (r=N is only advisory!!) | ||
// so…assume the worst (e.g. conflicting or lost update) and treat these like a 500 | ||
plugin.bigcouch = function (transport, url) { | ||
this.base = autoBase(url); | ||
transport = transport.using('statusCheck').using('autoConvert', "application/json"); | ||
return function (req, cb) { | ||
return transport(req, function (e, d, m) { | ||
var status = m && m['X-Status-Code']; | ||
if (!e && status === 202) { | ||
e = Error("Troublesome status code from server: " + status); | ||
e.status = status; | ||
} | ||
cb(e, d, m); | ||
}); | ||
}; | ||
}; | ||
// some boilerplate to deal with browser vs. CommonJS | ||
if (fermata) { | ||
fermata.registerPlugin("couchdb", plugin); | ||
fermata.registerPlugin("bigcouch", plugin.bigcouch); | ||
} else { | ||
@@ -138,0 +166,0 @@ module.exports = plugin; |
{ | ||
"name": "fermata-couchdb", | ||
"version": "0.10.7", | ||
"version": "0.10.8", | ||
"description": "CouchDB helpers for Fermata", | ||
@@ -5,0 +5,0 @@ "main": "fermata-couchdb.js", |
@@ -56,2 +56,16 @@ # fermata-couchdb | ||
## Alternate Cloudant / BigCouch (/ CouchDB 2.0?) extended plugin | ||
Cloudant's "BigCouch" version of CouchDB [does not actually enforce read quorums](https://github.com/cloudant/bigcouch/issues/55#issuecomment-30186518)! 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); | ||
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. | ||
## Changes watcher API | ||
@@ -58,0 +72,0 @@ |
14298
154
102