geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Skip and limit options effect associated records. #192

Open dfmcphee opened 10 years ago

dfmcphee commented 10 years ago

For example, say I have an index page of posts that have associated comments. I only want to show 10 posts on the first page, but all the comments for each post.

If I add a limit option to the query, I now will only get max 10 posts and 10 comments for each post.

It would be nice to be able to specify limits and offsets for nested associations.

ben-ng commented 10 years ago

Hmm I'm not sure if this is possible without using subqueries.. Is this an sql adapter you're using? And eager loading?

dfmcphee commented 10 years ago

Ah ok, I had a feeling it might end up being too complicated. I am using postgres with your new nested eager loading functionality. It works great by the way.

mde commented 10 years ago

Some interesting background: http://darwinweb.net/articles/optimizing-and-simplifying-limited-eager-loading-i

AR apparently handles limited eager-loading with a two-query strategy, grabbing the limited IDs first, then re-querying to get all the associated objects. This might not be that hard to implement, but in the short-term, you can certainly do those same two queries yourself -- do the 'limit' query on Posts, then do the 'includes' query on Posts/Comments where id in the list from the first query.

dfmcphee commented 10 years ago

Right, that makes sense. I will just use two queries for now. Thanks!