Skip to content

Instantly share code, notes, and snippets.

@auggernaut
Last active August 23, 2021 16:47
Show Gist options
  • Save auggernaut/6004780 to your computer and use it in GitHub Desktop.
Save auggernaut/6004780 to your computer and use it in GitHub Desktop.
Creating a per-user Database in CouchDB with nano.
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;
}
});
}
});
}
});
};
@standuprey
Copy link

@bzkdjc
Copy link

bzkdjc commented Mar 11, 2019

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