Skip to content

Document how to access two independent databases #76

Closed

Description

@hokster7

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.

Activity

  1. dresende commented on Mar 14, 2013

    @dresende
    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?

  2. hokster7 commented on Mar 14, 2013

    @hokster7
    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.

  3. dresende commented on Mar 14, 2013

    @dresende
    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.

  4. dresende commented on Mar 14, 2013

    @dresende
    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions