[PonyORM-list] Always prefetch some related entities ?
Tomislav Tustonic
ttustonic at outlook.com
Sat Dec 13 22:15:44 UTC 2014
Hello!
Alexander Kozlovsky wrote:
> Hi, Tomislav!
>
> > Is it possible to make an entity always prefetch some related entities?
>
> At this moment it is not possible. You can read fl.fullPath before exiting of
> db_session in order to force fetching of necessary related object.When we
> using Pony in our current projects, we always construct JSON data (or some
> other kind of response) withing the db session, so it is not necessary to us
> to use the prefetch functionality at all. I think that the approach when db
> session wraps entire request processing is the most easy and efficient. But we
> can add the ability that you requested. It may be done in two different way.
>
> 1) We can add prefetch option to attribute:
>
> directory = Required(lambda: Directory, prefetch=True)
>
> 2) We can add prefetch option to db_session:
>
> with db_session(prefetch=[ File.directory ]):
> fl = File[1]
>
> Currently I prefer the second way more, because it allows to prefetch data
> only if it is explicitly requested, whereas the first variant requires to do
> prefetching in each db_session even if it is not necessary.
I'm trying to create a kind of generic repository class, with usual methods:
getById, getAll, search etc...
So I have something like:
class BasePonyRepository(object):
_class = None
def __init__(self, clss=None, *args, **kwargs):
if clss:
self._class = clss
def getById(self, id):
with db_session:
item = self._class[id]
return item
This is nice and simple, and works for all entities. I can then override this
and set the _class for the specific repository, or create an instance of
BaseRepository using clss parameter. But, if I need a FileRepository I can't
just create a BaseRepository using File as a parameter, but I have to create a
new Repo class and override all the methods.
It's not a great problem since there are only a few methods to override, I was
just hoping it could be defined on an Entity level and keep a repository class
the same. For this case your option 1 would be preferable, option 2 doesn't
offer much over the code that already works.
True, it always hits a database, which is an overhead, but in this case, a
developer can decide does he need eager loading or not and adjust entities and
queries.
I should probably rethink this design, or perhaps create a repository as an
context manager would give me an abstraction over database access, but still
allow me to use db_session in the __enter__/__exit__ methods...
> > It would be nice to be able to have 'calculated' attributes, which wouldn't
> be saved and loaded from the database, but would be serialized using to_dict()
>
> We have a plan to add computed attributes, but our intention is different, we
> want to use such attributes inside queries, as a way to simplify complex
> expressions. That is, Pony should be able to translate such computed
> attributes to SQL and inline them into the query.
That would be nice too.
>
> Until computed attributes are implemented, you can solve your current problem
> by overriding `to_dict` method and adding calculated values manually:
Nice, will try this.
Thanks, Tom
More information about the ponyorm-list
mailing list