I have a registration page which asks the user to enter a username and email address.
I need to check to see if the username + email is available. How to do I do this with just one HTTP request?
BTW, the username is used as the docID.
What I'm doing right now is checking to see if the docID exists, then using a view to check for the availability of the email address, but that's 2 HTTP requests.
I thought about using a view to emit [ username, email ] as the key and query the view with a "key" param = [ username, email ]. But this won't work if the username and email belong to different existing users.
Jason. Sam's solution is quite helpful however as you say, it may not be a perfect fit for you. I can think of two other options.
Pretend for a moment you are back in SQL. What is the primary key of a user? It is neither the username nor the email but both columns. Same for CouchDB.
So, to absolutely guarantee uniqueness, the answer is simple: it must be the doc _id
. For example, for user "jason" with email "[email protected]", POST this doc:
{ "_id": "jason:[email protected]",
"other stuff": "blah blah blah"
}
That is an atomic, transactional, user creation. It could:
_rev
property. Great, that user/email combo is already taken!Of course, to simply check if the name is available you can GET /db/jason:[email protected]
. (You could prefix the id like the _users
database does, like users:jason:[email protected]
—it's up to you.) Then you can POST
it back later. There is a chance it will be taken in the mean time but that's normal in all "check first, reserve later" forms which are quite common on the web now.
The second idea is more just to think carefully about your situation. You said that two users can have the same username and email. That sounds odd. Maybe I read wrong. Here are some things to think about:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With