Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created May 16, 2012 19:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbranyen/2713385 to your computer and use it in GitHub Desktop.
Save tbranyen/2713385 to your computer and use it in GitHub Desktop.
Miso Project - Dataset: CouchDB Importer
// Underscore utility
var _ = require("underscore");
// Miso library
var Miso = require("miso.dataset");
// Nano library
var nano = require("nano");
/*
* The Couchdb importer is responsible for fetching data from a CouchDB
* database.
*
* Parameters:
* options
* auth - Authentication to the database server
* host - Address to the database server
* db - Name of the database
* query - Query to make to the database
*/
Miso.Importers.Couchdb = function(options) {
_.defaults(this, options, {
auth: "",
host: "",
db: "",
query: ""
});
// Generate the CouchDB url
var url = [ this.auth, this.host, this.db ].join("");
// Establish a connection
this.connection = nano([ url, this.query && "?" + this.query ].join(""));
};
_.extend(Miso.Importers.Couchdb.prototype, {
fetch: function(options) {
if (_.isFunction(this.view)) {
this.view(this.connection, function(err, data) {
if (err) {
return options.error(err);
}
return options.success(data.rows);
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment