Repository navigation
feat: add express-postgres N+1 example with slow/fast endpoints - #11
Conversation
Added README for Express + PostgreSQL N+1 example.
A real stack rather than a fake client: docker-compose, a seed that clears the threshold, /orders/slow written as a loop and /orders/fast batched, and a test using expectNoNPlusOne. Verified by running it end to end. Both endpoints behave as described — /slow reports 10x on the line that caused it, /fast reports nothing — and the three tests pass. Two fixes on top, both of which meant the test file could not run at all: the imports were one directory level short (correct from the example root, wrong from test/), and the client was never closed so the process hung after the assertions passed. Closes #5
|
Merged — thank you, this is a good example and it does what the issue asked for. I ran it end to end rather than reading the diff, and both endpoints behave exactly as you described:
Two fixes on top, both in the test file. It could not run as submitted: The imports were The client was never closed, so after the assertions passed the process hung on an open handle. Added a closing test with a comment saying why, since it looks like a pointless test until you hit it. I mention this only because the PR description says the test was run — worth double-checking that step next time, since a path that is wrong by one level fails in a way that looks like the library is broken rather than the example. One thing I decided not to change, in case you want to weigh in: the compose file publishes on port 5433, which collided with another Postgres I had running. Not wrong — 5433 is the conventional "second Postgres" port — but for an example that people run casually, something in the 55000s would collide with less. Happy either way. The |
What this changes
Adds a runnable
examples/express-postgres/directory that demonstrates the N+1 detection library against a real Express + PostgreSQL stack. Includesdocker-compose.yml, a seed script, two endpoints (/orders/slowwith the N+1 bug and/orders/fastwith the fix), and a README.Why this way
The existing
examples/demo.mjsuses a fake client. This gives users a real, copy-paste-runnable example with an actual database so they can see the warning fire live against real queries.How it was verified
docker compose up -d→node seed.mjs→node server.mjsran successfully locallycurl localhost:3000/orders/slowtriggers the expected N+1 warning (10× SELECT * FROM order_items WHERE order_id = ?)curl localhost:3000/orders/fastreturns clean with no warning