couchdb validation based on content from existing documents

风格不统一 提交于 2019-12-24 18:17:47

问题


QUESTION

Is it possible to query other couchdb documents as part of a standard couchdb validation function ?

If not, what is the standard approach for including properties of other documents as part of a validation rule inside a couchdb validation function?

RATIONALE

Consider a run-of-the-mill address book application where the validation function is intended to prevent two or more entries having the same value for the 'e-mail' in one of the address book entry fields.

Consider also an address book application where it is possible to specify validation rules in separate documents, based on whether the postal code is a US-based postal code or something else.


回答1:


No, it is not possible to query other couchdb documents in a validate_doc_update function. Each runs in isolation passing references only to: the new document, the old document, and user (where applicable).

My personal experience has been there are at least three options for dealing with duplicate checking:

  1. Use Cloudant as your CouchDB provider. They offer a free tier for now if you'd like to experiment, but they guarantee consistency across nodes for a CouchDB database. (See #2)
  2. I've used a secondary "reserve table" for names using the type-key as the ID. Then, you need to check for conflicts if not using a system like Cloudant. Basically, there's a simple document that maintains a key to prevent duplicates. It's not fun code to write given that you need to watch for conflicts. (Even with cloudant, you need to deal with failed requests to write, but it's easier than dealing with timing issues surrounding data replication across multiple nodes).
  3. Use a traditional DB like MySQL for example that can maintain a unique and consistent index for specific data values like you're describing. Store the documents away in CouchDB though. While slightly annoying that you need different data providers, it's reliable.
  4. (Optional: decide that CouchDB isn't a great fit for the type of system you're building)


来源:https://stackoverflow.com/questions/13128730/couchdb-validation-based-on-content-from-existing-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!