dresende/node-orm2 — pull request #393 by Tape (2013-11-20 17:30:46+00:00)
Title: Implement eager loading

### Description

Here's a simple implementation of eager loading (**NOTE:** I am not familiar with MongoDB so this will need to be added or refactored in a way that Mongo can handle it, I left relevant TODO comments on what to remove). Autofetch on large queries can be quite detrimental and this should help remedy the problem. This should also resolve #389, however bulk updates should be added.

### How it works

Assume I have a database table with the models `Image` and `Tag` with a relationship `Image.hasMany("tags", Tag)`. If I do `Image.find(...).run(...)` currently it will do n+1 queries (query for images, n queries for tags). The implementation I have provided will only perform two queries to fetch the data. In order to use it, autofetch can be left off and do `Image.find(...).eager("tags").run(...)`. The returned result will be the same, but will be far more performant.