natevw / fermata

Succinct native REST client, for client-side web apps and node.js. Turns URLs into (optionally: magic!) JavaScript objects.
328 stars 24 forks source link

Using couchdb plugin in node.js #19

Closed ghost closed 12 years ago

ghost commented 12 years ago

Hi, I have moved to node.js and want to use the couchdb plugin to track changes, but I am struggling to get the plugin initialised.

I tried this:

var f = require('fermata'); require('fermata/plugins/couchdb');

var db = fermata.couchdb()('dev'); // db() === "http://localhost:5984/dev" fermata.plugins.couchdb.watchChanges(db, 50000, function (r) { console.log("Got change results:", r); });

But the plugin does not appear to auto register with fermata.

Do you have an node.js based template example?

TIA

Andy

natevw commented 12 years ago

Here's a plugin example I had handy for Chargify: https://gist.github.com/2068774

The main thing is plugins do not auto-register in node.js, so you need to connect them yourself:

var f = require('fermata'),
    _c = require('fermata/plugins/couchdb');
f.registerPlugin('couchdb', _c);
// then remainder of your code should work

Let me know if that works. Also would love to get more feedback on the CouchDB plugin — right now it's pretty rough and I'm debating (e.g.) whether the changes handler should keep returning full result set or if the callback you provide should just get called once for every change.

ghost commented 12 years ago

Hi

Working now thanks.

I have made one mod to the couchdb plugin

if (d.results.length) { callback(d); }

I need to track the last sequence processed, so that in the event of a restart I can start tracking changes from the last processed sequence. Is there a better way for the client to get this? I don't think I can track the seq from the actual results as I filter these on the couchdb side so the seq of the last result may be considerably lower that that of the last seq in the changes feed.

For me, I prefer receiving the complete changes object, this allows me to plug-n-play the actual mechanism used for retrieving the changes (actually I'm only using fermata :-)), Did I see somewhere that you can chain plugins? If so then maybe a changes wrapper plugin that runs track changes and breaks the changes into individual results? That way the developer can use the method thats most convenient to them?