A different couch db for each request...

49 views
Skip to first unread message

RicRoberts

unread,
Sep 22, 2009, 8:40:23 AM9/22/09
to CouchRest
In a web framework like rails or sinatra for example, can anyone
suggest how best to specify a different database for CouchRest to use
on each request, based on the url? e.g. http://mysite.com/my_couch_db/rest/of/url

I'm anticipating a small number of accounts (i.e. a few tens) and I'd
like to store data for different accounts in separate couch
databases. It'd be great if in a before_filter (or whatever), I could
set the site for the whole request, like this:

db = CouchRest.database("http://127.0.0.1:5984/my_couch_db")
CouchRest::ExtendedDocument.use_database db

The CouchRest docs say not to change the model databases at runtime,
and the above code doesn't work in cases where couchrest is not
reloaded on each request - i.e. "production mode". Instead, I have to
call use_database on each individual model to get different requests
to use different dbs. Am I doing something silly here?

Any tips appreciated!
Cheers,
Ric

Peter Gumeson

unread,
Sep 22, 2009, 6:52:22 PM9/22/09
to couc...@googlegroups.com
Have a look at the ClassProxy class in couchrest.

Peter
--
Peter Gumeson
gum...@gmail.com

RicRoberts

unread,
Sep 23, 2009, 4:19:27 AM9/23/09
to CouchRest
Thanks, Peter - I'd spotted that, but it means that throughout the
code I'd need to do stuff like:

"MyDocument.on(@account_db).by_name", (and also set the database on
document instances before saving them) ... which doesn't feel very
DRY.

The alternative, as I mentioned, is just setting each the database on
each document type once, at the start of the request. But this
doesn't feel right either (as you have to either maintain a list of
all document classes, or do some clunky meta-programming to find them
all).

I'm not sure which is worse. :)

On Sep 22, 11:52 pm, Peter Gumeson <gume...@gmail.com> wrote:
> Have a look at the ClassProxy class in couchrest.
>
> Peter
>
>
>
> On Tue, Sep 22, 2009 at 5:40 AM, RicRoberts <ricisb...@googlemail.com> wrote:
>
> > In a web framework like rails or sinatra for example, can anyone
> > suggest how best to specify a different database for CouchRest to use
> > on each request, based on the url?  e.g.http://mysite.com/my_couch_db/rest/of/url
>
> > I'm anticipating a small number of accounts (i.e. a few tens) and I'd
> > like to store data for different accounts in separate couch
> > databases.  It'd be great if in a before_filter (or whatever), I could
> > set the site for the whole request, like this:
>
> > db = CouchRest.database("http://127.0.0.1:5984/my_couch_db")
> > CouchRest::ExtendedDocument.use_database db
>
> > The CouchRest docs say not to change the model databases at runtime,
> > and the above code doesn't work in cases where couchrest is not
> > reloaded on each request - i.e. "production mode".  Instead, I have to
> > call use_database on each individual model to get different requests
> > to use different dbs.  Am I doing something silly here?
>
> > Any tips appreciated!
> > Cheers,
> > Ric
>
> --
> Peter Gumeson
> gume...@gmail.com

Alex

unread,
Oct 7, 2009, 1:52:46 AM10/7/09
to CouchRest
I fall into the same. Also, we can:

class ApplicationController
before_filter...
def set_database
Thread.current[:account_db] = current_user.db_name
end
end

class Shop < CouchRest::ExtendedDocument
use_database db
self.db
Thread.current[:account_db]
end
end

I'm also not sure which is worse. :)
> > > The CouchRest docs say not to change the modeldatabasesat runtime,

RicRoberts

unread,
Oct 11, 2009, 2:31:35 PM10/11/09
to CouchRest
I quite like your idea, Alex, although it does depend on your chosen
web server using a single thread per request (which admittedly is
normally the case).

Alex

unread,
Oct 12, 2009, 3:11:43 AM10/12/09
to CouchRest
On Oct 12, 12:31 am, RicRoberts <ricisb...@googlemail.com> wrote:
> I quite like your idea, Alex,

I was wrong a bit. The proper way is to override self.database instead
of use_database.
Also, if you are using pagination look at this commit
http://github.com/wildchild/couchrest/commit/3137e652a56e9f50f6cd95ed871975c66f663b1a

> although it does depend on your chosen
> web server using a single thread per request (which admittedly is
> normally the case).

IIRC rails i18n uses the same technique to set a locale per request.

timmacfarlane

unread,
Nov 19, 2009, 7:53:29 AM11/19/09
to CouchRest
I've been using this method (doc.database=) instead of the
"set_database $db" method, just because I don't like globals much.
However when I get new documents from views their database isn't set
so I have to set the database for each new doc coming out of the view.
If I don't, I can't .save() or .destroy() the viewed docs.

I've got a patch if CouchRest maintainers want it (to upload later, I
don't have it right now)

Tim.

On Oct 12, 7:11 am, Alex <se...@rambler.ru> wrote:
> On Oct 12, 12:31 am, RicRoberts <ricisb...@googlemail.com> wrote:
>
> > I quite like your idea, Alex,
>
> I was wrong a bit. The proper way is to override self.database instead
> of use_database.
> Also, if you are using pagination look at this commithttp://github.com/wildchild/couchrest/commit/3137e652a56e9f50f6cd95ed...
Reply all
Reply to author
Forward
0 new messages