Transforming Views with List Functions

51 views
Skip to first unread message

Howie

unread,
Dec 6, 2011, 11:00:40 AM12/6/11
to couchdbkit
Hi

It's day 3 of me learning CouchDb and Couchdbkit. Great app.

I've just mastered how to create List Functions combined with View
Collations to produce a traditional 'join' query in one request. My
document seems fine as it works using cURL

e.g. curl -X GET http://127.0.0.1:5984/playertweets/_design/team/_list/team_players/with_players

where 'with_players' is the collated view:

function(doc) {
if (doc.doc_type == "Team") {
emit([doc.name, 1], doc);
} else if (doc.doc_type == "Player") {
emit([doc.name, 2], doc);
}
}

and team_players is the list:

function(head, req) {
start({'headers':{'Content-Type':'application/json'}});

var first_team = true
, first_player = true
, row
;

send('{"rows":[');

while(row = getRow()) {
if(row.key[1] === 2) {
// player for team
if (first_player) {
first_player = false;
} else {
send(',');
}
send(JSON.stringify(row.value));
}
else if (row.key[1] === 1) {
// New team
if (first_team) {
first_team = false;
} else {
send(']},');
}
send('{"team":');
send(JSON.stringify(row.key[0]));
send(',"players":[');
first_player = true;
}
}
if (!first_team)
send(']}');

send('\n]}');
}

So, how can I call this using couchdbkit? I realise my list returns
hand-written JSON, so I'm not expecting it to map to the model, but at
least be able to execute the request/query using the API

Thanks

Benoit Chesneau

unread,
Dec 6, 2011, 11:48:10 AM12/6/11
to couch...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages