Skip to content
vetkitChecksChangelogDocumentation

vetkit.data-access.query-in-loop.model

A database query runs inside a loop, once per item (the N+1 query pattern). The number of round trips grows with the data, so code that is fast on a development database becomes slow and holds connections for longer once real collections and lists grow.

Last updated:

Suite: Production readiness

javascript, typescript

A database query runs inside a loop, once per item (the N+1 query pattern). The number of round trips grows with the data, so code that is fast on a development database becomes slow and holds connections for longer once real collections and lists grow.

Category: DATABASE · Engine rule severity: WARNING

How to fix it

Load what the loop needs with one query before it (Mongoose find({ _id: { $in: ids } }), Sequelize findAll({ where: { id: ids } }), populate / include for related rows) and look documents up in a Map inside the loop. Batch writes with insertMany, bulkWrite, bulkCreate or a single updateMany.

References

Source: packages/engine/rules/data-access/javascript.yml.

Generated from shipped rule definitions at build time. Matching is a review signal, not proof of exploitability. “generic” denotes text matching, not complete language or framework coverage. Runtime configuration and available tools affect whether a rule runs.