Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.