Skip to content

Hierarchy relationships

bmeck edited this page Nov 7, 2011 · 3 revisions

Resourceful has a concept of hierarchy relationships on a per document basis. This is similar to how Google's App Engine handles relationships between Entities. By declaring one Resource to be a child type of another resource several functions will be added to the resource in order to provide methods of interacting those relations. Take a library for example:

resourceful.use('memory');
Author = resourceful.define('author', function () {
  this.property('name');
});
Book = resourceful.define('book', function () {
  this.property('title');
  this.parent('Author');
});
shakespeare = new Author({
  _id:'WilliamShakespeare',
  name:'William Shakespeare'
});
shakespeare.createBook({
  _id:'RomeoAndJuliet',
  title:'Romeo and Juliet'
});
shakespeare.createBook({
  _id:'Hamlet',
  title:'Hamlet'
});
shakespeare.books(function (err, booksByShakespeare) {
  console.log(booksByShakespeare);
});
Book.byAuthor('WilliamShakespeare', function (err, booksByShakespeare) {
  console.log(booksByShakespeare);
});

Notice how we are using _id? That is because like CouchDB resourceful uses _id as a special property to represent a globally unique ID. All relationships revolve around _id and require it in order to function properly.

Clone this wiki locally