Skip to content

Instantly share code, notes, and snippets.

@hassox
Created November 26, 2008 11:08
Show Gist options
  • Save hassox/29363 to your computer and use it in GitHub Desktop.
Save hassox/29363 to your computer and use it in GitHub Desktop.
function(doc) {
if ( doc["couchrest-type"] == "Account" ) {
emit(doc._id, ["Account", doc]);
}
if ( doc["couchrest-type"] == "Contact" ) {
emit(doc.account_id, ["Contact", doc]);
}
}
function(account_id, results, rereduce) {
contacts = [];
var account;
if( rereduce ) {
return results;
} else {
for(var i=0; i< results.length; i++){
if( results[i][0] == "Contact" ){
contacts.push(results[i][1]);
}
if ( results[i][0] == "Account" ) {
account = results[i][1];
}
}
if(account) {
account.contacts = contacts;
return account;
} else { return null; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment