Skip to content

Instantly share code, notes, and snippets.

@leocristofani
Forked from auggernaut/couch.js
Created September 2, 2017 16:51
Show Gist options
  • Save leocristofani/b8639f71ef13925212f214f41ba7af48 to your computer and use it in GitHub Desktop.
Save leocristofani/b8639f71ef13925212f214f41ba7af48 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;
}
});
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment