Aliasing and multiple database support works great, even if the server is the same; however, what about if the definition is the same?
IOW, the following code works 100% great:
[code]
import mongoengine as me
asset = me.connect('asset', alias='default', host='mongodb://nexusone/asset')
resource = me.connect('resource', alias='resource', host='mongodb://nexusone/resource')
class Person(me.Document):
meta = {'allow_inheritance': 0, 'db_alias' : 'resource'}
name = me.StringField()
age = me.IntField()
class User( me.DynamicDocument ):
meta = {'allow_inheritance':0}
user = me.StringField( required=1, unique=1, max_length=20 )
a = Person(age=29)
a.save()
u = User.objects(user='moranoa').first()
print u.user
[/code]
but what if I wanted to use the User collection in the first database?
trying to inject 'db_alias':'resource' into the meta for the User class does not change it's connection.
It almost seems (I have not dug too far) that the meta dict and the class itself are cached elsewhere, and the meta dict is not re-read for updated information.
The use case here is simple:
We have a single schema laid out, but want to sync items from one to another, OR, we have data in another data center, like MySQL, Filemaker, etc... and want to be able to copy/update from that to more than one database.
For example: We currently have data in our primary production database in SQL, but we have Maya, Nuke, etc... tools built off using Mongo (via Mongoengine).
During every 10 or so mins, we look at the live production in SQL, and check a dirty field for updates, we then copy parts of those records to the mongo layer.
What we don't want to do is mix out development and live production data areas, so as we make new tools against the Mongo layer, we want to have it hit a seperate database, but that has the exact same schema as the live.
In theory we can get around this by making a new module, copied from the schema in the live, but that throws a monkey wrench into update once, reuse, because we'd have to copy sections out of the development version into the live.
One way around this, and what SQLObject does, is put the meta info as a class instance and refer to it pre-op. That way, if I wanted to stupidly change the connection at runtime, I could.
I dont think, nor suggest, going that way, I am just bringing up a design point: It would be nice to be able to use a single module as schema definition for multiple connections.
Currently, no matter what I do, it will always use the first connection the meta dict had at the time of initialization.
Is any of this use case valid for anyone? Or am I over-complicating it?
I wish I could share my DBO factory, to show you guys exactly what we are doing. It wraps Filemaker, MySQL and Mongo great, just this last bit that would be nice to have.
Cheers.