Last active
August 23, 2021 16:47
-
-
Save auggernaut/6004780 to your computer and use it in GitHub Desktop.
Creating a per-user Database in CouchDB with nano.
This file contains hidden or 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
exports.findOrCreateDB = function (config, creds, cb) { | |
var nano = require('nano')("http://" + config.couch_admin + ":" + config.couch_password + "@" + config.couch_host + ':' + config.couch_port); | |
var users = nano.use('_users'); | |
var user = { | |
_id: "org.couchdb.user:" + creds.username, | |
name: creds.username, | |
roles: [], | |
type: "user", | |
password: creds.password | |
}; | |
var userDB = nano.use(creds.username); | |
var secObj = { | |
admins: { | |
names: [], | |
roles: [] | |
}, | |
members: { | |
names: [creds.username], | |
roles: [] | |
} | |
}; | |
console.log(user); | |
users.insert(user, creds._id, function (err, body) { | |
if (err) { | |
console.log('[_users.insert] ', err.message); | |
return; | |
} | |
else { | |
console.log('user created.'); | |
nano.db.create(creds.username, function (err, body) { | |
if (err) { | |
console.log('[db.create] ', err.message); | |
return; | |
} | |
else { | |
console.log('database created!'); | |
userDB.insert(secObj, "_security", function (err, body) { | |
if (err) { | |
console.log('[_security.insert] ', err.message); | |
return; | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
}; | |
https://gist.github.com/auggernaut/6004780#file-couch-js-L28 should be user._id, no?
@standup75 It seems you're right : ) Yet, I starred this gist.
=> Please, have a look at my fork
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/auggernaut/6004780#file-couch-js-L28 should be user._id, no?