Agent-Written Endpoint Triggers N+1 Queries; Query-Count Test Catches It Early

A developer describes an AI coding agent's GET /orders handler that returned correct data but issued one database query per order, 101 queries for a 100-row page. Existing functional tests passed because they only checked the response body, not the query count. The author shows how the N+1 pattern arises in node-postgres and Express code, even though each line looks correct. To catch it, the author proposes a test helper that wraps the pool's query method to record each SQL statement while still executing it. Running this counter within a test lets a branch fail before the regression is merged.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in