heynemann / motorengine

Motorengine is a port of MongoEngine for Tornado.
http://motorengine.readthedocs.org
204 stars 67 forks source link

Allow a uri to be passed as the first parameter to connect #63

Closed btaitelb closed 10 years ago

btaitelb commented 10 years ago

I wanted a way to be able to pass a connection uri (e.g. MONGOLAB_URI in the environment) to the connect function. Without this, I had to parse the uri just to get the db name, and then pass the uri itself as the host keyword argument.

heynemann commented 10 years ago

First, thanks a lot for contributing. Hope MotorEngine is helping you.

This pull request is not required, though. Maybe a pull request to update docs on connecting would be better, ehehehehe.

Just pass host=env["MONGOLAB_URI"]. Hosts work with either an actual hostname or a connection string.

btaitelb commented 10 years ago

The issue I have is that you can't just call connect(host=mongolab_uri), you have to call connect(db, host=mongolab_uri), which involves passing the uri through a regex to find the db name, since it's of the format mongodb://host:port/name or mongodb://user:password@host:port/name

heynemann commented 10 years ago

I'll have to try it later, but I'm pretty sure the db name is used only for future reference (if you have many connections to the DB). The name that's actually used for connection is the database in the string.

What I mean is that if you have:

connect("my-pretty-app", "mongodb://user:password@host:port/name")

"my-pretty-app" is only useful if you want to retrieve this specific database connection. The database motorengine will connect to is "name". If that's not the case, then we should fix it.

btaitelb commented 10 years ago

I'm not able to get the functionality you're describing in practice in my app (I'm getting pymongo.errors.OperationFailure: database error: not authorized for query on my_db_name.User). I'm having trouble coming up with a test case that reproduces this however, and it feels more like an integration test might be needed (e.g. connect one way and writes some data, then connect the other way and read it back to ensure that the same db and collection are used each time). Any suggestions for how I can write the test? Do you think it should be possible as a unit test? Thanks!