closeio / flask-mongorest

Restful API framework wrapped around MongoEngine
Other
521 stars 88 forks source link

Connecting to and switching multiple databases #64

Open mgmonteleone opened 9 years ago

mgmonteleone commented 9 years ago

Trying to implement a reporting API for multiple mongo databases with the same structure using mongorest...

Have tried to implement this using the mongoengine connect mechanism:

for mongoconfig in configmongos:
    print "Connecting: "+mongoconfig["name"]
    db.connect(
        name = mongoconfig["name"],
        host = mongoconfig["host"],
        port = mongoconfig["port"],
        db = mongoconfig["database"]
    )

and then using a variable in my document class: class Products(db.Document): productName = db.StringField() businessUnit = db.StringField() brand = db.StringField() subBrand = db.StringField() franchise = db.StringField() category = db.StringField() subCategory = db.StringField()

   meta = {'collection': 'products',"db_alias": selectedInstance}

But it seams that the db alias is always "default"

Any way to implement this?

wojcikstefan commented 9 years ago

Hi, I think MongoEngine can only connect to one database at a time. You can open a ticket with them at https://github.com/MongoEngine/mongoengine.

mgmonteleone commented 9 years ago

MongoEngine has the ability to connect to multiple databases, and then switch that database using an alias (see above), the issue is that the MongoRest always registers the connection as "default" alias. I will take a look at the code to see where this is being done, and perhaps I can come up with a patch to have MongoRest support multiple aliases....