Possibility of accessing same models on different databases?

113 views
Skip to first unread message

George Antoniadis

unread,
Sep 19, 2012, 10:17:50 AM9/19/12
to mongoo...@googlegroups.com
This is a very uncommon question I am aware of that, but please bear with me.

I need to have a number of databases that will have the same kind of collections and models.Let's say for example we have a model "photograph" and a nodejs app that multiple users can access.
Depending on the user's name I'd like to be able to access the "photograph" documents on a different database.

I guess the logical thing would be to create a connection manager for the mongoose objects...
But should I initialize the models so I can query specific connection?

Thanks in advance.! ^_^

Edmund von der Burg

unread,
Sep 19, 2012, 10:48:10 AM9/19/12
to mongoo...@googlegroups.com
On 19 September 2012 15:17, George Antoniadis <geo...@noodles.gr> wrote:
> This is a very uncommon question I am aware of that, but please bear with
> me.

It's not actually :)

> I need to have a number of databases that will have the same kind of
> collections and models.Let's say for example we have a model "photograph"
> and a nodejs app that multiple users can access.
> Depending on the user's name I'd like to be able to access the "photograph"
> documents on a different database.
>
> I guess the logical thing would be to create a connection manager for the
> mongoose objects...
> But should I initialize the models so I can query specific connection?

This is what we do in our project. We decide the database to use based
on the subdomain but any lookup will do the job really. Once we know
the database a connection manager is used to provide access to the
models.

Code that selects db based on subdomain:

https://github.com/mysociety/popit/blob/master/lib/middleware/instance-selector.js

Code that manages the connections (and other stuff):

https://github.com/mysociety/popit/blob/master/lib/popit.js

Inside a request you get a model using req.popit.model('ModelName').
In a script or similar you agoin create the popit object at the start
and then use that again.

There are, as you can imagine, numerous gotchas. One is that many of
the auth libraries assume that you only have one database of users. We
don't and this is causing us annoyance (everyauth used to support it
but now does not, passport will once
https://github.com/jaredhanson/passport/pull/58 in applied). Same
applies for the session storage although that is less of a hassle.

There is also the matter of preloading the models - see
pre_load_schemas in popit.js for haw we solve that.

Cheers,
Edmund.


> Thanks in advance.! ^_^
>
> --
> --
> http://mongoosejs.com - docs
> http://plugins.mongoosejs.com - plugins search
> http://github.com/learnboost/mongoose - source code
>
> You received this message because you are subscribed to the Google
> Groups "Mongoose Node.JS ORM" group.
> To post to this group, send email to mongoo...@googlegroups.com
> To unsubscribe from this group, send email to
> mongoose-orm...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/mongoose-orm?hl=en
>
>

George Antoniadis

unread,
Sep 19, 2012, 11:12:31 AM9/19/12
to mongoo...@googlegroups.com, ev...@mysociety.org
This is exactly what I was trying to accomplish! :)
Storing the ref of the db in the req was the only way I could think too. :P

The auth will be custom so I am not worried about it.
I'll try creating a singleton mongoose manager that will auto initialize the models on each new connection.

Your code seems pretty solid, thank you very much for sharing this and replying so promptly! :)
I'll get back to you when I have made some more progress!

Thanks again.
Reply all
Reply to author
Forward
0 new messages