angel-dart-archive / orm

moved to angel-dart/angel/packages/orm

Home Page:https://github.com/angel-dart/angel/tree/master/packages/orm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support lazy loading of relations

omarbelkhodja opened this issue · comments

*This issue was automatically moved to: angel-dart/angel#214.

I think this would be nice to have. My question is, if fetching is lazy, how will the user indicate when to fetch the data?

Possibly:

var query = AuthorQuery(fetchBooks: true);

But then, this raises the question: why lazy-fetch things, when you can instead just manually query them when they are needed? I want to be sure to only codegen features that specifically need to be generated, as angel_orm_generator is already complex enough, and I want to keep it as maintainable as possible for the long-term.

var author = await authorQuery.get(executor);
var bookQuery = BookQuery()..where.authorId.equals(author.idAsInt);
var books = await bookQuery.get(executor); // Fetch the books when they are needed.

There is an interesting article about how it works in hibernates you can read here:
https://www.baeldung.com/hibernate-lazy-eager-loading

Your proposal

var query = AuthorQuery(fetchBooks: true);

would solve the problem of not loading the data if not required. But the problem is that you won't be able to query them simple by calling a getter, as in the hibernates approach.

With the lazy initialization approach, orderDetailSet will get initialized only when it is explicitly called by using a getter or some other method

A middle-of-the-road option is maybe to add a Future<List<Book>> fetchBooks(QueryExecutor executor) method, that simply generates a BookQuery and returns its value.

Also, for what it's worth, at least based on the Hibernate example, I don't think there is much meaningful difference between "eager" and "lazy." To me, their definition of "lazy" sounds like "eager, just at another time."

One other option, though, could be a Future<void> fetchBooks(QueryExecutor executor), where the field books is set to the result of a BookQuery. This won't add considerable complexity to the codegen.