CouchDB Referential Integrity -
i new couchdb
, nosql
scene , coming sql
background. have questions on referential integrity, example have product document below
{ type: "product" name: "sweet necklace" category: "necklace" }
and each category have own document
{ type: "category", name: "necklace", custom_attr: ".." }
just sake of argument, happens when stakeholder chose rename category "necklace" "accessories", should happen on products have category field set "necklace"? do bulk update on products category equal necklace? (i don't think couchdb
allows perform "update where" kinda statement)
what best practice on handling such situation?
p/s: chose save category name in product document instead of category id since nosql
encourages denormalization anyway.
if you're maintaining separate document category you've not denormalized data @ all. in fact, doing you're doing, you're getting worst of both worlds - no normalization , no denormalization.
try this:
product document:
{ _id:"product:first_product", name:"first product" category:"category:category_1" }
category document:
{ _id:"category:category_1", name:"category 1", custom_attr: {} }
this way, when change name of category, you're still referring correct document product documents have category.
note: can still have type
field , let _id
remain currently.
edit:
to product/category info, can define map function so:
function(doc){ if(doc.id.indexof('product:') === 0){ // or if(doc.type === 'product') if use type field emit(doc, {'_id': doc.category}); } }
now whenever use view , set include_docs
true, category information included in results.
Comments
Post a Comment