تخطَّ إلى المحتوى

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.

const teams = await teamRepo.find();
for (const team of teams) {
const members = await team.members; // lazy, a query per iteration
console.log(members.length);
}
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.