Catch N+1 queries before they hit production

Zero-config N+1 detection for TypeScript. Intercepts at the database driver level. Works with any ORM. Fails your tests when it finds a problem.

$ npm install qguard click to copy
vitest run
FAIL src/handlers/dashboard.test.ts > dashboard handler
has no N+1 queries (3ms)
QueryGuardError: N+1 query detected
Repeated query executed 10 times:
SELECT * FROM "posts" WHERE "user_id" = ?
Called from:
src/handlers/dashboard.ts:47
users.map(u => prisma.post.findMany({ where: { userId: u.id } }))
Total queries: 11 (1 expected + 10 duplicates)
Time in duplicates: 247ms (89% of query time)
Fingerprint: a3f2b1c9
Test Files 1 failed (1)
Tests 1 failed | 4 passed (5)
Duration 1.24s
0
Runtime dependencies
18KB
Package size
128
Tests passing
0%
Performance overhead
Three lines of code. No config files.
QueryGuard intercepts pg.Client and pg.Pool queries at the driver level. Every query is fingerprinted and tracked per async context. If the same query repeats above a threshold, your test fails.
01

Install

npm install qguard

No plugins to register. No setup files to create. No environment variables to set.

02

Wrap your test

await assertNoNPlusOne(() => handler(req, res))

Import from queryguard/vitest or queryguard/jest. One function call wraps your handler.

03

Get caught

QueryGuard fingerprints every SQL query. Repeated fingerprints above the threshold fail the test with the exact SQL, call site, and query count.

Works with your stack. No adapters needed.
QueryGuard operates at the pg driver level, below your ORM. Any library that talks to PostgreSQL through pg is automatically supported.
Prisma 7
with @prisma/adapter-pg
Drizzle
TypeORM
Kysely
Knex
Raw pg
Found 6 N+1 queries in Logto
We ran QueryGuard against Logto, an 11,900-star open-source auth platform. Their admin Roles page fires 6 individual queries per role in a paginated list of 20. Every admin, every visit, unconditionally.

Before

122
queries per page load
734ms
query time

After

8
queries per page load
~5ms
query time
Tested on real codebases. Not toy examples.

Logto

11,900 stars on GitHub
N+1 patterns found6
Queries before fix122
Queries after fix8
Query reduction93%

Payload CMS

41,700 stars on GitHub
Tests run with QueryGuard136
False positives0
Performance overhead0%
Tests broken0
How QueryGuard compares
queryguard Sentry DataLoader Manual logging
Detects at test time Yes No No No
Zero config Yes No No No
Works across ORMs Yes Yes No Yes
No SaaS required Yes No Yes Yes
CI assertions Yes No No No
Open source MIT Paid MIT --

Stop shipping N+1 queries

Ruby has had Bullet since 2009. Node.js has QueryGuard now.