Add slow-query warning custom middleware example to prisma-next-demo#912
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAdds a new ChangesSlow Query Warning Middleware
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
size-limit report 📦
|
@prisma-next/extension-author-tools
@prisma-next/mongo-runtime
@prisma-next/family-mongo
@prisma-next/sql-runtime
@prisma-next/family-sql
@prisma-next/extension-arktype-json
@prisma-next/middleware-cache
@prisma-next/mongo
@prisma-next/extension-paradedb
@prisma-next/extension-pgvector
@prisma-next/extension-postgis
@prisma-next/postgres
@prisma-next/sql-orm-client
@prisma-next/sqlite
@prisma-next/extension-supabase
@prisma-next/target-mongo
@prisma-next/adapter-mongo
@prisma-next/driver-mongo
@prisma-next/contract
@prisma-next/utils
@prisma-next/config
@prisma-next/errors
@prisma-next/framework-components
@prisma-next/operations
@prisma-next/ts-render
@prisma-next/contract-authoring
@prisma-next/ids
@prisma-next/psl-parser
@prisma-next/psl-printer
@prisma-next/cli
@prisma-next/cli-telemetry
@prisma-next/config-loader
@prisma-next/emitter
@prisma-next/language-server
@prisma-next/migration-tools
prisma-next
@prisma-next/vite-plugin-contract-emit
@prisma-next/mongo-codec
@prisma-next/mongo-contract
@prisma-next/mongo-value
@prisma-next/mongo-contract-psl
@prisma-next/mongo-contract-ts
@prisma-next/mongo-emitter
@prisma-next/mongo-schema-ir
@prisma-next/mongo-query-ast
@prisma-next/mongo-orm
@prisma-next/mongo-query-builder
@prisma-next/mongo-lowering
@prisma-next/mongo-wire
@prisma-next/sql-contract
@prisma-next/sql-errors
@prisma-next/sql-operations
@prisma-next/sql-schema-ir
@prisma-next/sql-contract-psl
@prisma-next/sql-contract-ts
@prisma-next/sql-contract-emitter
@prisma-next/sql-lane-query-builder
@prisma-next/sql-relational-core
@prisma-next/sql-builder
@prisma-next/target-postgres
@prisma-next/target-sqlite
@prisma-next/adapter-postgres
@prisma-next/adapter-sqlite
@prisma-next/driver-postgres
@prisma-next/driver-sqlite
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
examples/prisma-next-demo/test/slow-query-warning.test.ts (1)
54-63: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrefer separate
expect()calls overtoMatchObject.Based on learnings, this repo's test convention favors individual field assertions for clearer failure messages rather than a single combined
toMatchObjectcheck.♻️ Proposed refactor
expect(warnEvents).toHaveLength(1); - expect(warnEvents[0]).toMatchObject({ - code: 'APP.SLOW_QUERY', - details: { - sql: 'SELECT "id", "email" FROM "user" LIMIT 1', - latencyMs: 900, - rowCount: 1, - source: 'driver', - planExecutionId: 'test-plan-execution', - }, - }); + const event = warnEvents[0] as { code: string; details: Record<string, unknown> }; + expect(event.code).toBe('APP.SLOW_QUERY'); + expect(event.details.sql).toBe('SELECT "id", "email" FROM "user" LIMIT 1'); + expect(event.details.latencyMs).toBe(900); + expect(event.details.rowCount).toBe(1); + expect(event.details.source).toBe('driver'); + expect(event.details.planExecutionId).toBe('test-plan-execution');🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/prisma-next-demo/test/slow-query-warning.test.ts` around lines 54 - 63, The assertion in slow-query-warning.test.ts uses a single toMatchObject check for warnEvents[0], but this test suite prefers separate expect() calls for clearer failures. Update the assertion around warnEvents[0] to verify code and each details field individually, keeping the same values for sql, latencyMs, rowCount, source, and planExecutionId, and use the existing warnEvents array access in the test.Source: Learnings
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@examples/prisma-next-demo/test/slow-query-warning.test.ts`:
- Around line 54-63: The assertion in slow-query-warning.test.ts uses a single
toMatchObject check for warnEvents[0], but this test suite prefers separate
expect() calls for clearer failures. Update the assertion around warnEvents[0]
to verify code and each details field individually, keeping the same values for
sql, latencyMs, rowCount, source, and planExecutionId, and use the existing
warnEvents array access in the test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 1d93a813-197a-4b73-8a27-e9942b0d2f6c
📒 Files selected for processing (3)
examples/prisma-next-demo/src/prisma/db.tsexamples/prisma-next-demo/src/prisma/slow-query-warning.tsexamples/prisma-next-demo/test/slow-query-warning.test.ts
Adds a slowQueryWarning middleware to the prisma-next-demo example, wired into the runtime middleware chain in db.ts, with offline unit tests driving afterExecute directly. Written as the canonical custom middleware example for the Prisma Next docs (middleware section). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Søren Bramer Schmidt <sorenbs@gmail.com>
…idental The slow-query-warning middleware addition to prisma-next-demo is example-only documentation code exercising the existing SqlMiddleware afterExecute hook; no consumer upgrade action is required. Signed-off-by: Alexey Orlenko's AI Agent <robot@aqrln.net>
391d7eb to
3520774
Compare
Linked issue
n/a — companion example for the Prisma Next user docs Middleware section. The docs PR that embeds this example is prisma/web#8008; tracking for the docs work lives in the docs Linear project, and there is no framework ticket for this change.
At a glance
Before this PR,
prisma-next-demoregistered only built-in middleware (cache,lints,budgets); the repo had no example of authoring a middleware from scratch.Summary
This PR ships the canonical custom-middleware example: a
slowQueryWarningobserver inprisma-next-demo, registered on the demo runtime and covered by offline unit tests. The new Prisma Next docs page "Authoring custom middleware" (prisma/web#8008) embeds this file verbatim, so the doc example is code that compiles and has tests behind it.How it fits together
SqlMiddleware, so contextual typing infers every hook's parameters and the example needs no deep type imports. It implements onlyafterExecuteand warns throughctx.logwhenresult.latencyMscrosses the threshold.source: 'middleware'.afterExecutedirectly, no database required: a real DSLbuild()supplies the plan'sastandmeta, and a hand-builtSqlMiddlewareContextcaptureslog.warnevents. Three cases: warns above the threshold, silent at the threshold, and the 250ms default.Reviewer notes
Object.freeze(...)and annotate hook parameters explicitly; this example deliberately returns a plain literal instead, because the docs page teaches the minimal authoring shape and contextual typing from theSqlMiddlewarereturn type keeps it fully typed.db.contractsupplies a real contract and the plan'sast/metacome from a real DSL build, so the shapes stay honest to what the runtime passes.slow-query-warning.tsverbatim. If this file changes shape later, the docs page needs the same edit.Behavior changes & evidence
APP.SLOW_QUERYwarning for any execution slower than 250ms, whichever surface issued it (slow-query-warning.ts, db.ts). Evidence: test/slow-query-warning.test.ts. No behavior change outsideexamples/prisma-next-demo.Testing performed
pnpm --filter prisma-next-demo typecheckpnpm --filter prisma-next-demo exec vitest run slow-query-warning— 3 passedbiome checkon the three touched files (also enforced by lint-staged on commit)Skill update
n/a — example-only change; no CLI, public API, config, or error-code surface changed.
Alternatives considered
budgetsbuilt-in already demonstrates enforcement (BUDGET.TIME_EXCEEDED); the example's job is to show a pure observer, which is the more common authoring starting point, so it warns and never fails the query.examples/prisma-next-demoinstead, so the docs can link one runnable app that shows built-ins, an extension, and a custom middleware together, where users actually look first.Checklist
git commit -s) per the DCO.TML-NNNN: <sentence-case title>form — no framework Linear ticket exists for this companion-example work, so the title names the deliverable directly.🤖 Generated with Claude Code
Summary by CodeRabbit