Created
May 16, 2012 19:45
-
-
Save tbranyen/2713385 to your computer and use it in GitHub Desktop.
Miso Project - Dataset: CouchDB Importer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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