Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to access two independent databases #76

Closed
fatuhoku opened this issue Mar 14, 2013 · 4 comments
Closed

Document how to access two independent databases #76

fatuhoku opened this issue Mar 14, 2013 · 4 comments

Comments

@fatuhoku
Copy link

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.

@dresende
Copy link
Owner

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?

@fatuhoku
Copy link
Author

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
Copy link
Owner

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.

dresende added a commit that referenced this issue Mar 14, 2013
#76)

There are 2 changes that break backwards compliance:

1. req.db can be: a) null b) db connection c) array of db connections
2. req.models will be filled with the models defined on each connection
@dresende
Copy link
Owner

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.

dresende added a commit that referenced this issue Mar 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants