You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document how to access two independent databases #76
I'm using Express to build an application that accesses two Postgres databases. However it's not clear from documentation whether this is possible with the middleware.
It's not. But probably you want to connect to both databases, define models in both and access them without needing to know where the model was defined, right?
Possibly. I believe Django has what's called 'database routers' which allow the user to define multiple databases, as well as read/write permissions for each of them. One of the databases in my use case, for instance, is to be accessed read-only, and it'd be nice if node-orm2 could enforce that.
That's another cool feature that should be available even outside the middleware. I'm going to try to change the express middleware to support several connections. About the read-only option maybe it fits better on model definition.
This change is not backwards compliant, although I still can try to change to be. Usage:
app.use(orm.express("mysql://...",{define: function(db,models){models.Model1=db.define("model1",{ ... });}});app.use(orm.express("pg://...",{define: function(db,models){models.Model2=db.define("model2",{ ... });}});app.get("/",function(req,res){// if connections are established:// req.db should be an Array with both connections (order chan change from definition)// req.models should have Model1 and Model2});
req.db has 3 possible types:
null before any successful connection is made;
Object (the db) if only one successful connection is made;
Array (of dbs) if more than one successful connection is made.
Activity
dresende commentedon Mar 14, 2013
It's not. But probably you want to connect to both databases, define models in both and access them without needing to know where the model was defined, right?
hokster7 commentedon Mar 14, 2013
Possibly. I believe Django has what's called 'database routers' which allow the user to define multiple databases, as well as read/write permissions for each of them. One of the databases in my use case, for instance, is to be accessed read-only, and it'd be nice if node-orm2 could enforce that.
dresende commentedon Mar 14, 2013
That's another cool feature that should be available even outside the middleware. I'm going to try to change the express middleware to support several connections. About the read-only option maybe it fits better on model definition.
Changes Express middleware to be able to have more than one connection (
dresende commentedon Mar 14, 2013
This change is not backwards compliant, although I still can try to change to be. Usage:
req.dbhas 3 possible types:nullbefore any successful connection is made;Object(the db) if only one successful connection is made;Array(of dbs) if more than one successful connection is made.Changes express middleware returned function to avoid assigned req.db…