Create users own document in CouchDB with node.js

232 Views Asked by At

I want to make documents, which can be updated/changed by one concrete user(the user which has created it). Document content wich all users can see but only the owner can change it. The question is: If I have user with username "TheUser" how can create his own document named "TheUserDocument". It's not a problem to use just Nodejs, Nano or something else for database.

1

There are 1 best solutions below

2
On

This can be accomplished with a document update validation function. When you originally create the document, store the user who created it, then your update validation function might look something like this:

function(newDoc, oldDoc, userCtx, secObj) {
  if ( oldDoc.creator == userCtx.name ){
    return;
  }
  throw({forbidden: 'Permission denied.'});
}

You can read more about validation functions here: http://docs.couchdb.org/en/latest/ddocs.html#validate-document-update-functions