These days, I'm busy working on a reimplementation of KnitMinder that uses Mobile Couchbase for data storage. One of the issues that I've been thinking about recently is how to set up an elegant system for shared references that incorporate both built-in values and user-entered data. Examples of these types of sets include colors, counter names, and needle/hook sizes. If I were using an RDBMS, I would create a reference table with synthetic IDs for each value, then use those IDs in the entity table. I could do something similar in CouchDb, but it would be a pain to do all the lookups and maintain referential integrity by hand. Eventually, I realized that CouchDb already does efficient lookups: they're called "views". Duh.
Let's say that I need to present a sorted list of all standard and user-entered colors as part of editing a yarn entity. I define a color as a JSON object containing hue, saturation, value, and name:
{ name: "periwinkle", h:0.097, s:0.225, v:0.925 }
All entities that use color will conform to this object definition, so I might have an existing yarn entry with the following property:
colors: [
{ name: "cinnabar", h:0.014, s:0.760, v:0.550, pct:0.25 },
{ name: "olive", h:0.167, s:1.0, v:0.25, pct:0.75 }
]
Now I can write a view that emits one row for each value in the "colors" property of the yarn object with a key of [ h, s, v ] and a value of "name". With a little bit of property name standardization, I can extract colors from any entity type, not just yarn. I can also provide standard colors by simply creating a document with a field named "colors". Note that each color object in the yarn has an additional field that shows its relative percentage in the color makeup of the yarn. For the purposes of generating the color list, any extra fields are ignored.
The main downside to this design is that the name and color values are copied in each document that uses them, so storage costs are higher than a referential system. For a few hundred to a few thousand documents, though, this shouldn't be an issue, and the advantages far outweigh the additional storage costs for the color data and indexes.
The same system can be used for any other combination of user-entered and provided data.
source for colors: http://en.wikipedia.org/wiki/List_of_colors