couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "EntityRelationship" by WoutMertens
Date Sun, 12 Apr 2009 16:38:11 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by WoutMertens:
http://wiki.apache.org/couchdb/EntityRelationship

------------------------------------------------------------------------------
  When the user wants to add an address, they enter the information in to a form and the form
saves the information in a model that looks something like this:
  {{{
  {
-   "id":"some unique string that is assigned to the contact",
+   "_id":"some unique string that is assigned to the contact",
    "type":"contact",
    "name":"contact's name",
    "birth_day":"a date in string form",
@@ -21, +21 @@

    "company_address":"the company address in string form"
  }
  }}}
- (Note that ''type'' doesn't mean anything to CouchDB, we're just using it here for our own
convenience. ''id'' is the only thing CouchDB looks at)
+ (Note that ''type'' doesn't mean anything to CouchDB, we're just using it here for our own
convenience. ''_id'' is the only thing CouchDB looks at)
  
  That's great, your users immediately begin to use their address book and soon the datastore
starts to fill up. Not long after the deployment of your new application you hear from someone
that they are not happy that there is only one phone number. What if they want to store someone's
work telephone number in addition to their home number? No problem you think, you can just
add a work phone number to your structure. You change your data structure to look more like
this:
  {{{
@@ -43, +43 @@

  When using separate documents, you could have documents like this for the phone numbers:
  {{{
  {
-   "id":"the phone number",
+   "_id":"the phone number",
    "type":"phone",
    "contact":"id of the contact document that has this phone number",
    "phone_type":"string describing type of phone, like home,work,fax,mobile,..."
  }
  }}}
- (Note the use of the ''id'' field to store the phone number. Phone numbers are unique (when
prefixed with country and area code) and therefore this makes a great ''natural key'')
+ (Note the use of the ''_id'' field to store the phone number. Phone numbers are unique (when
prefixed with country and area code) and therefore this makes a great ''natural key'')
  
- The key to making all this work is the contact property. By storing the contact id in it,
you can refer to the owning contact in a unique way, since ''id'' fields are unique in CouchDB
databases.
+ The key to making all this work is the contact property. By storing the contact id in it,
you can refer to the owning contact in a unique way, since ''_id'' fields are unique in CouchDB
databases.
- = This is as far as I got, work in progress =
+ 
- Creating the relationship between a contact and one of its phone numbers is easy to do.
Let's say you have a contact named "Scott" who has a home phone and a mobile phone. You populate
his contact info like this:
+ Creating the relationship between a contact and one of its phone numbers is easy to do.
Let's say you have a contact named "Scott" who has a home phone and a mobile phone. You populate
his contact info like this (using Perl and Net::CouchDB):
- scott = Contact(name='Scott')
- scott.put()
- PhoneNumber(contact=scott,
-             phone_type='home',
-             number='(650) 555 - 2200').put()
- PhoneNumber(contact=scott,
-             phone_type='mobile',
-             number='(650) 555 - 2201').put()
+ {{{
+ $db->insert({type => 'contact', _id => 'Scott', name => 'My Friend Scott'});
+ $db->insert({type => 'phone', _id => '(650) 555 - 2200', contact => 'Scott',
phone_type => 'home'});
+ $db->insert({type => 'phone', _id => '(650) 555 - 2201', contact => 'Scott',
phone_type => 'mobile'});
+ }}}
+ 
+ To get the contacts and their phone numbers from CouchDB in one search, you need to use
a little trick: You need to create a view that sorts the contacts and their phone numbers
in order. This is the view:
+ 
+ {{{
+ "map":function(doc) {
+    if (doc.type == 'contact') {
+       emit([doc._id, 0], doc);
+    } else if (doc.type == 'phone') {
+       emit([doc._id, 1], doc);
+    }
+ }
+ }}}
+ 
+ If you then query this view with the ''startkey'' parameter set to "[''''''"Scott"]" and
endkey "[''''''"Scott",{}]"
+ 
+ = this is how far I got, work in progress =
+ 
  Because ReferenceProperty creates this special property on Contact, it makes it very easy
to retrieve all the phone numbers for a given person. If you wanted to print all the phone
numbers for a given person, you can do it like this:
  print 'Content-Type: text/html'
  print

Mime
View raw message