Skip to content
Permalink
Browse files

switch to nano

get a new client every request
  • Loading branch information...
robertkowalski committed Jan 21, 2015
1 parent 69c1d72 commit 81c89dcdbaab2aeae15fe69b46ab145b15da37ad
Showing with 53 additions and 40 deletions.
  1. +6 −6 bootstrap.js
  2. +2 −1 config-production.js-dist
  3. +2 −1 config.js
  4. +0 −1 package.json
  5. +2 −1 provisioning/templates/config.js.j2
  6. +40 −29 services/data/data.js
  7. +1 −1 services/data/package.json
@@ -2,8 +2,8 @@ var request = require('request'),
async = require('async-minihelper'),
couchApp = require('couchapp'),
couch = require('./config.js').couch,
root = 'http://' + couch.username + ':' + couch.password + '@' + couch.url.replace('http://', '') + '/',
testDbName = couch.dbName,
root = couch.protocol + '://' + couch.username + ':' +
couch.password + '@' + couch.host + '/' + couch.dbName,
testDataSubmissions = require('./test/fixtures/contribs-interests.json'),
testDataVotes = require('./test/fixtures/votes.json'),
hoek = require('hoek'),
@@ -12,7 +12,7 @@ var request = require('request'),

function createDb (cb) {
request({
uri: root + testDbName,
uri: root,
method: 'PUT',
json: true
}, function (err, res, body) {
@@ -27,7 +27,7 @@ function createDb (cb) {

function populateDb (cb) {
request({
uri: root + testDbName + '/_bulk_docs',
uri: root + '/_bulk_docs',
method: 'POST',
json: true,
body: {
@@ -45,7 +45,7 @@ function populateDb (cb) {

function deleteDb (cb) {
request({
uri: root + testDbName,
uri: root,
method: 'DELETE',
json: true
}, function (err, res, body) {
@@ -59,7 +59,7 @@ function deleteDb (cb) {
}

function createViews (cb) {
couchApp.createApp(require('./couch-data/views.js'), root + testDbName, function (doc) {
couchApp.createApp(require('./couch-data/views.js'), root, function (doc) {
doc.push();
cb && cb();
});
@@ -1,6 +1,7 @@
var config = {
couch: {
url: 'http://127.0.0.1:5984',
protocol: 'https',
host: '127.0.0.1:5984',
username: 'admin',
password: 'admin',
dbName: 'contriboot_dev'
@@ -8,7 +8,8 @@ if (process.env.NODE_ENV === 'test') {

var config = {
couch: {
url: 'http://127.0.0.1:5984',
protocol: 'http',
host: '127.0.0.1:5984',
username: 'admin',
password: 'admin',
dbName: 'contriboot_dev'
@@ -28,7 +28,6 @@
"dependencies": {
"blankie": "~1.0.0",
"boom": "~2.6.1",
"couch-login": "^1.0.0",
"handlebars": "^2.0.0",
"hapi": "~8.0.0",
"hoek": "^2.3.0",
@@ -1,6 +1,7 @@
var config = {
couch: {
url: 'https://{{db_host}}',
protocol: '{{db_protocol}}',
host: '{{db_host}}',
username: '{{db_user}}',
password: '{{db_password}}',
dbName: '{{db_name}}'
@@ -1,4 +1,4 @@
var CouchLogin = require('couch-login'),
var nano = require('nano'),
sanitizer = require('sanitizer');


@@ -16,32 +16,34 @@ function sanitizeSubmissionPayLoad (payload) {
}

exports.register = function Couch (service, couchSettings, next) {
var auth = {
name: couchSettings.username,
password: couchSettings.password
},
couch;
couch = new CouchLogin(couchSettings.url + '/' + couchSettings.dbName, 'basic');
couch.strictSSL = false;
couch.login(auth, function (er, cr, data) {
if (er) {
throw er;
}
});
function getClient () {
var couch = nano({
url: couchSettings.protocol + '://' + couchSettings.username + ':' +
couchSettings.password + '@' + couchSettings.host + '/' + couchSettings.dbName
});
return couch;
}

service.method('getContributionsAndInterests', function (next) {
couch.get('/_design/contributions/_view/byType', function (err, cr, data) {
if (err || cr && cr.statusCode !== 200 || !data) {
var couch = getClient();

couch.get('_design/contributions/_view/byType', function (err, data) {
if (err || !data) {
console.error(err);
return next(err);
}

return next(null, data);
});
});

service.method('getResponsesForInterest', function (id, next) {
var url = '/_design/contributions/_view/getResponsesForInterest';
couch.get(url + '?key="' + id + '"', function (err, cr, data) {
if (err || cr && cr.statusCode !== 200 || !data) {
var couch = getClient();

var url = '_design/contributions/_view/getResponsesForInterest';
couch.get(url + '?key="' + id + '"', function (err, data) {
if (err || !data) {
console.error(err);
return next(err);
}

@@ -50,8 +52,10 @@ exports.register = function Couch (service, couchSettings, next) {
});

service.method('getSubmissionById', function (id, next) {
couch.get('/' + id, function (err, cr, data) {
if (err || cr && cr.statusCode !== 200 || !data) {
var couch = getClient();
couch.get('' + id, function (err, data) {
if (err || !data) {
console.error(err);
return next(err);
}

@@ -60,14 +64,17 @@ exports.register = function Couch (service, couchSettings, next) {
});

service.method('getVotesbySubmissionId', function (id, next) {
var url = '/_design/contributions/_view/voteCountBySubmission';
couch.get(url + '?group=true&key="' + id + '"', function (err, cr, votesData) {
var voteObject = votesData.rows[0];
var votes = voteObject && voteObject.value || 0;
var couch = getClient();

if (err || cr && cr.statusCode !== 200) {
var url = '_design/contributions/_view/voteCountBySubmission';
couch.get(url + '?group=true&key="' + id + '"', function (err, votesData) {
if (err || !votesData) {
console.error(err);
return next(err);
}
var voteObject = votesData.rows[0];
var votes = voteObject && voteObject.value || 0;


return next(null, votes);
});
@@ -76,8 +83,10 @@ exports.register = function Couch (service, couchSettings, next) {
service.method('saveSubmission', function (payload, next) {
payload = sanitizeSubmissionPayLoad(payload);

couch.post('/', payload, function (err, cr, data) {
if (err || cr && cr.statusCode !== 201 || !data) {
var couch = getClient();
couch.insert(payload, function (err, data) {
if (err || !data) {
console.error(err);
return next(err);
}

@@ -88,9 +97,11 @@ exports.register = function Couch (service, couchSettings, next) {
service.method('saveVote', function (payload, next) {
payload.type = 'vote';

couch.post('/', payload, function (err, cr, data) {
var couch = getClient();
couch.insert(payload, function (err, data) {

if (err || cr && cr.statusCode !== 201 || !data) {
if (err || !data) {
console.error(err);
return next(err);
}

@@ -10,6 +10,6 @@
"lab": "^3.2.1"
},
"dependencies": {
"couch-login": "^1.0.0"
"nano": "~6.0.2"
}
}

0 comments on commit 81c89dc

Please sign in to comment.
You can’t perform that action at this time.