Skip to content

Commit

Permalink
fix: handle clustered couchdb versions (2.x, 3.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
garbados committed May 21, 2020
1 parent 884468a commit 1f4574f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 42 deletions.
86 changes: 50 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,58 @@ module.exports = function configure (url, source, callback) {
assert(typeof couch.request === 'function',
'URL must point to the root of a CouchDB server (not to a database).')

compile(source, { index: true }, function (error, config) {
if (error) {
return callback(error)
}

var settings = Object.keys(config)
.reduce(function (memo, key) {
if (typeof config[key] !== 'object') return memo

var section = Object.keys(config[key])
.map(function (k) {
return {
path: encodeURIComponent(key) + '/' + encodeURIComponent(k),
value: config[key][k].toString()
}
async.waterfall([
(done) => {
async.series([
(done) => {
couch.request({ path: '' }, (error, result) => {
if (error) { return done(error) }
return done(null, result.version)
})
},
(done) => {
async.waterfall([
compile.bind(null, source, { index: true }),
(config, done) => {
var settings = Object.keys(config)
.reduce(function (memo, key) {
if (typeof config[key] !== 'object') return memo

return memo.concat(section)
}, [])
var section = Object.keys(config[key])
.map(function (k) {
return {
path: encodeURIComponent(key) + '/' + encodeURIComponent(k),
value: config[key][k].toString()
}
})

async.map(settings, function (setting, next) {
couch.request({
method: 'PUT',
path: '_config/' + setting.path,
body: setting.value
}, function (error, oldValue) {
if (error) return next(error)
return memo.concat(section)
}, [])
return done(null, settings)
}
], done)
}
], done)
},
([ version, settings ], done) => {
const configPath = version > '2' ? '_node/_local/_config/' : '_config/'
async.map(settings, (setting, next) => {
couch.request({
method: 'PUT',
path: configPath + setting.path,
body: setting.value
}, function (error, oldValue) {
if (error) return next(error)

next(null, {
path: setting.path,
value: setting.value,
oldValue: oldValue
next(null, {
path: setting.path,
value: setting.value,
oldValue: oldValue
})
})
})
}, function (error, responses) {
if (error) return callback(error)

}, done)
},
(responses, done) => {
var response = responses.reduce(function (memo, response) {
memo[response.path] = {
ok: true,
Expand All @@ -57,8 +72,7 @@ module.exports = function configure (url, source, callback) {

return memo
}, {})

callback(null, response)
})
})
return done(null, response)
}
], callback)
}
23 changes: 17 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ var configure = require('../')
var url = process.env.COUCH || 'http://localhost:5984'
var couch = nano(url)

// get couchdb version to set _config path
let configPath
couch.request({ path: '' }, (error, result) => {
if (error) { throw error }
if (result.version > '2') {
configPath = '_node/_local/_config'
} else {
configPath = '_config'
}
})

// There is an issue with section deletion in CouchDB.
// You cannot delete an entire section:
// $ curl -XDELETE http://localhost:5984/_config/couchdb-bootstrap
// {"error":"method_not_allowed","reason":"Only GET,PUT,DELETE allowed"}
function clear (callback) {
couch.request({
path: '_config/couchdb-configure'
path: `${configPath}/couchdb-configure`
}, function (error, config) {
if (error) return callback(error)

async.map(Object.keys(config), function (key, next) {
couch.request({
method: 'DELETE',
path: '_config/couchdb-configure/' + encodeURIComponent(key)
path: `${configPath}/couchdb-configure/${encodeURIComponent(key)}`
}, next)
}, callback)
})
Expand Down Expand Up @@ -56,7 +67,7 @@ test('configure from json', function (t) {
t.error(error)

couch.request({
path: '_config/couchdb-configure/foo'
path: `${configPath}/couchdb-configure/foo`
}, function (error, config) {
t.error(error)
t.equal(config, 'bar')
Expand All @@ -74,7 +85,7 @@ test('configure from commonjs', function (t) {
t.error(error)

couch.request({
path: '_config/couchdb-configure/baz'
path: `${configPath}/couchdb-configure/baz`
}, function (error, config) {
t.error(error)
t.equal(config, 'foo')
Expand All @@ -92,7 +103,7 @@ test('configure from commonjs/index', function (t) {
t.error(error)

couch.request({
path: '_config/couchdb-configure/bar'
path: `${configPath}/couchdb-configure/bar`
}, function (error, config) {
t.error(error)
t.equal(config, 'baz')
Expand All @@ -110,7 +121,7 @@ test('configure from filesystem', function (t) {
t.error(error)

couch.request({
path: '_config/couchdb-configure/foo'
path: `${configPath}/couchdb-configure/foo`
}, function (error, config) {
t.error(error)
t.equal(config, 'bar')
Expand Down

0 comments on commit 1f4574f

Please sign in to comment.