Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created January 25, 2016 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/7918a0fbd5f197d1853d to your computer and use it in GitHub Desktop.
Save swashcap/7918a0fbd5f197d1853d to your computer and use it in GitHub Desktop.
nodeapi/coinstac-server-core integration
// 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);
}
};
};
// 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