views:

49

answers:

1

Is it possible to have couch update or change fields on the fly when you create/update a doc? For example in the design view.... validate_doc_update:

function(newDoc, oldDoc, userCtx) {
}

Within that function I can throw errors like:

if(!newDoc.user_email && !newDoc.user_name && !newDoc.user_password){
    throw({forbidden : 'all fields required'});
}

My Question is how would I reassign a field? I tried this:

newDoc.user_password ="changed";

with changed being some new value or hashed value. My overall goal is to build a user registration/login system with node and couchdb and have not found very good examples.

+1  A: 

The validate_doc_update function cannot have any side effects and cannot change the document before storage. It only has the power to block an update or to let it through. This is important, because the function is not only called when a user requests an update, but also when changes are replicated from one CouchDB instance to another. So the function can be called multiple times for one document.

However, CouchDB now supports Document Update Handlers that can modify a document or even build it from scratch. These can be used to convert non-JSON input data into usable documents. You can find some documentation in the CouchDB Wiki.

Before you build your own user registration/login system, I'd suggest you look into the built-in CouchDB security features (if you haven't - some information here). They might not be enough for you (e.g. if you need email validation or something similar), but maybe you can build on them.

Christian Berg