Skip to content

Instantly share code, notes, and snippets.

@jhs
Created June 15, 2016 07:28
Show Gist options
  • Save jhs/8d8aa2a3b24e7bb924451f452b55c401 to your computer and use it in GitHub Desktop.
Save jhs/8d8aa2a3b24e7bb924451f452b55c401 to your computer and use it in GitHub Desktop.
How to use fun-pouchdb
var DB = require('fun-pouchdb').defaults({prefix: __dirname})
// Production app needs:
// 1. Validation functions
// 2. Cloudant credentials
// 3. Design documents
// Optional Cloudant credentials
var cloudant = {account:process.env.cloudant_account,
password:process.env.cloudant_password}
function check_the_doc(doc) {
if (doc.bad)
throw new Error('Bad doc!')
// Otherwise, the doc passes.
}
// Options for "products" db.
var products = { ddoc:{_id:'_design/example'}
, cloudant: cloudant
, validate: check_the_doc
}
// Other databases, etc.
var admin_db = {cloudant:cloudant}
var foo_db = {}
DB({products:products, admin_db:admin_db, foo_db:foo_db},
function(er, dbs) {
if (er) throw er // TODO
// Databases will be in dbs.products, dbs.admin_db, etc.
console.log('DBs ready')
// The database is ready to use!
dbs.products.allDocs({include_docs:true}, function(er, result) {
if (er) throw er // TODO
for (var row of result.rows) {
// There is a nice utility function to get a link to edit a document.
var url = dbs.products.fun.cloudant.edit(row.doc._id)
console.log('%s %j', url, row.doc)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment