Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

couchdb views tied between two databases?

Say I have several databases on my couch instance (in my case a user account database and login session database) The sessions have fields corresponding to a field in the user database. Is there a way to create a view, or make a call that encompasses this correlation, or would it just have to be done with an external script? To be more clear, here's an example.

Account db:
{
   "_id": "78555fdfdd345debf427373f9dfaeca4",
...
   "username" : "bob"
}

Sessions db:

{
   "_id": "78555fdfdd345debf427373f9dfcd7ae",
..
   "accountId": "78555fdfdd345debf427373f9dfaeca4",
   "username": "bob"
}

Can I use emit or something like that to bundle together all this information in one call?

like image 939
V_H Avatar asked Mar 24 '11 16:03

V_H


2 Answers

No, however a common workaround is to have a "type" attribute for documents.

For example...

Application db:
{
   "_id": "account.78555fdfdd345debf427373f9dfaeca4",
   "type": "account",
...
   "username" : "bob"
}

{
   "_id": "session.78555fdfdd345debf427373f9dfcd7ae",
   "type": "session",
..
   "accountId": "account.78555fdfdd345debf427373f9dfaeca4",
   "username": "bob"
}

And then in your views:

map:
    function (doc) {
        if (doc.type=='account') {
            # ...
        }
    }
like image 115
sausman Avatar answered Oct 04 '22 21:10

sausman


No, unfortunately there is no way to connect data from multiple databases.

Each view is supposed to be self-contained, otherwise updating any document in 1 database will immediately need to trigger views indexes in every other database to be recalculated in all cases.

like image 32
Dominic Barnes Avatar answered Oct 04 '22 21:10

Dominic Barnes