bmuller / twistar

Twistar is an object-relational mapper (ORM) for Python that uses the Twisted library to provide asynchronous DB interaction.
http://findingscience.com/twistar
Other
132 stars 38 forks source link

eager loading relationships #62

Open giulioprovasi opened 8 years ago

giulioprovasi commented 8 years ago

Is it possible to load the model with its relationships directly ? something like User.with('pictures') to avoid a huge callback chain ?

This example can be ok for one relationship, but what about models having 4 or 5 relationships depth:

def foundPicture(picture):
     print picture.file

def foundUser(user):
     user.picture.get().addCallback(foundPicture)

User.find(where=['first_name = ?', "Bob"], limit=1).addCallback(foundUser)

Imagine I have this structure: User [HAS_MANY] Picture [HAS_ONE] Path [HAS_ONE] descriptor (doesn't make sens, just to see how to do this correctly without running 4 queries)

bmuller commented 8 years ago

@kitensei - it's definitely technically possible. I think one of the reasons I haven't tackled this yet is because I'm not sure what the API would look like. If you have any ideas on how to keep it simple, let me know, and I can handle the implementation.

Thanks!