Created
January 25, 2016 22:41
-
-
Save swashcap/7918a0fbd5f197d1853d to your computer and use it in GitHub Desktop.
nodeapi/coinstac-server-core integration
This file contains 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
// coinstac-server-client/index.js | |
'use strict'; | |
var assign = require('lodash.assign'); | |
var Pouchy = require('pouchy'); | |
module.exports.handlersFactory = function(config) { | |
// Private, configured PouchDB/Pouchy instance | |
// May need async config of instance if it's Cloudant and security settings | |
// must be adjusted. | |
var myPouchy = new Pouchy(config); | |
return { | |
get: function(id) { | |
return myPouchy.find(id); | |
}, | |
post: function(body) { | |
return myPouchy.save(body); | |
}, | |
put: function(id, body) { | |
return myPouchy.find(id) | |
.then(function(doc) { | |
return assign({}, doc, body); | |
}) | |
.then(myPouchy.save.bind(this)); | |
}, | |
remove: function(id) { | |
return myPouchy.delete(id); | |
} | |
}; | |
}; |
This file contains 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
// lib/controllers/coinstac/consortia.js | |
'use strict'; | |
var config = require('config'); | |
var getHandlers = require('coinstac-server-client').handlersFactory; | |
var handlers = getHandlers(config.get('coinstac.pouchdb')); | |
var logError = require('../path/to/error-handler.js').logError; | |
module.exports.get = { | |
handler: function(request, reply) { | |
return handlers.get(req.params.id).then(reply).catch(logError); | |
} | |
}; | |
module.exports.post = { | |
handler: function(request, reply) { | |
return handlers.post(request.body).then(reply).catch(logError); | |
} | |
}; | |
module.exports.put = { | |
handler: function(request, reply) { | |
return handlers | |
.put(request.params.id, request.body) | |
.then(reply) | |
.catch(logError); | |
} | |
}; | |
module.exports.delete = { | |
handler: function(request, reply) { | |
return handlers.remove(request.params.id).then(reply).catch(logError); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment