Lazy Load In Loop (ARCH3059)
Key typescript.orm.lazy-load-in-loop · Severity warning · Category orm
Accessing a lazily-loaded relation on each element of a collection when eager loading was omitted is the runtime trigger of N+1. Load the relation up front.
Incorrect
Section titled “Incorrect”const teams = await teamRepo.find();for (const team of teams) { const members = await team.members; // lazy, a query per iteration console.log(members.length);}Correct
Section titled “Correct”const teams = await teamRepo.find({ relations: { members: true } });for (const team of teams) { console.log(team.members.length);}Eager-load the relation in the query so no lazy load is triggered inside the loop.