feat: implement sqlcommenter plugins for Prisma Client#28769
Merged
Conversation
Contributor
size-limit report 📦
|
e6a7f4a to
5ce15c5
Compare
5ce15c5 to
5209041
Compare
5209041 to
f6ac66a
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR implements SQL commenter plugin support for Prisma Client, enabling users to attach metadata to SQL queries as comments following the sqlcommenter format. The implementation adds a new comments option to PrismaClient constructor that accepts plugin functions to dynamically generate comment tags.
Key changes:
- Created two new packages:
@prisma/sqlcommenter(public plugin types) and@prisma/json-protocol(internal protocol types) - Integrated comment injection into both LocalExecutor (driver adapters) and RemoteExecutor (Accelerate) execution paths
- Added validation, unit tests, and end-to-end tests for the feature
Reviewed changes
Copilot reviewed 52 out of 53 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/sqlcommenter/* | New public package with plugin type definitions and documentation |
| packages/json-protocol/* | New internal package for JSON protocol types |
| packages/client-engine-runtime/src/sql-commenter.ts | Core implementation of sqlcommenter formatting algorithm |
| packages/client/src/runtime/getPrismaClient.ts | Added comments option to PrismaClientOptions |
| packages/client/src/runtime/utils/validatePrismaClientOptions.ts | Validation logic for comments option |
| packages/client/src/runtime/core/engines/client/*.ts | Executor integration to pass queryInfo and plugins through execution chain |
| packages/client-engine-runtime/src/interpreter/query-interpreter.ts | Comment injection into SQL queries before execution |
| packages/query-plan-executor/src/* | Support for pre-computed comments from RemoteExecutor |
| packages/client-generator-/src/ | Generated TypeScript types for comments option (SQL providers only) |
| packages/client/tests/* | Unit tests for validation and E2E test for end-to-end functionality |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/client/src/runtime/core/engines/client/ClientEngine.ts
Outdated
Show resolved
Hide resolved
packages/client-engine-runtime/src/interpreter/query-interpreter.ts
Outdated
Show resolved
Hide resolved
f6ac66a to
8e76f7f
Compare
8e76f7f to
556ac2c
Compare
556ac2c to
66b0a6c
Compare
66b0a6c to
a993fb7
Compare
## Overview This PR implements support for SQL commenter plugins to Prisma Client, as described in the [RFC](https://www.notion.so/prismaio/SQL-Query-Comments-2b99e8aecef78057bbafd3f4fde73430). The feature will allow users to add metadata to SQL queries as comments following the [sqlcommenter format](https://google.github.io/sqlcommenter/). ## Goals 1. Create a new `@prisma/sqlcommenter` package with public-facing types 2. Implement the `SqlCommenterPlugin` interface and `SqlCommenterContext` type 3. Add a `comments` option to `PrismaClient` constructor 4. Implement the sqlcommenter algorithm for formatting comments 5. Integrate comment injection into the query execution pipeline (both LocalExecutor and RemoteExecutor/Accelerate) 6. Add unit tests ## Out of Scope - Implementing actual plugins (e.g., `traceContext`, `queryInsights`) - Deciding where the first-party plugins should be exported from (e.g., `@prisma/client/sqlcommenter`) ## Component Overview We try to execute the comment plugins as close to the query execution time as possible, to have the most accurate context available. However, for RemoteExecutor (Accelerate), we need to pre-compute the comments before sending the request to the query plan executor server because we can't serialize the plugins themselves. This means that the future `traceContext` plugin will point at the query span as its traceparent when using LocalExecutor, but when using RemoteExecutor, the traceparent will be the `prisma:client:operation` span. ``` ┌─────────────────────────────────────────────────────────────────┐ │ PrismaClient Constructor │ │ │ │ new PrismaClient({ │ │ adapter: ..., │ │ comments: [plugin1, plugin2, ...] ← NEW OPTION │ │ }) │ └─────────────────────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ EngineConfig │ │ │ │ - Stores the comments plugins array │ │ - Passes to LocalExecutor or RemoteExecutor │ └─────────────────────────────────────────────────────────────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌──────────────────────────┐ ┌──────────────────────────┐ │ LocalExecutor │ │ RemoteExecutor │ │ │ │ (Accelerate) │ │ Passes plugins to │ │ │ │ QueryInterpreter │ │ Pre-computes comments │ │ │ │ and sends in request │ └──────────────────────────┘ └──────────────────────────┘ │ │ ▼ ▼ ┌──────────────────────────┐ ┌──────────────────────────┐ │ QueryInterpreter │ │ Query Plan Executor │ │ │ │ │ │ For each SQL query: │ │ Receives pre-computed │ │ 1. Call plugins │ │ comments in request, │ │ 2. Merge results │ │ uses constant plugin │ │ 3. Format & append │ │ in QueryInterpreter │ └──────────────────────────┘ └──────────────────────────┘ ``` ### Type Definitions The typeswill live in a new `@prisma/sqlcommenter` package to keep plugin dependencies minimal: ```typescript // packages/sqlcommenter/src/index.ts /** * Information about a single Prisma query. */ export interface SqlCommenterSingleQueryInfo { /** * The model name (e.g., "User", "Post"). Undefined for raw queries. */ modelName?: string /** * The Prisma operation (e.g., "findMany", "createOne", "queryRaw"). */ action: string /** * The full query object (selection, arguments, etc.). */ query: unknown } /** * Information about the query or queries being executed. * * - `single`: A single query is being executed * - `compacted`: Multiple queries have been compacted into a single SQL statement * (e.g., automatic batching of findUnique queries, or explicit $transaction batches) */ export type SqlCommenterQueryInfo = | ({ type: 'single' } & SqlCommenterSingleQueryInfo) | { type: 'compacted'; queries: SqlCommenterSingleQueryInfo[] } /** * Context provided to SQL commenter plugins. */ export interface SqlCommenterContext { /** * Information about the Prisma query being executed. */ query: SqlCommenterQueryInfo } /** * A SQL commenter plugin that returns key-value pairs to be added as comments. * Return an empty object to add no comments. */ export interface SqlCommenterPlugin { (context: SqlCommenterContext): Record<string, string> } ``` --- Ref: https://linear.app/prisma-company/issue/TML-1542/query-insights-statement-annotation Closes: #13360 Closes: #8508 Closes: #10746
a993fb7 to
6194dde
Compare
aqrln
added a commit
that referenced
this pull request
Dec 3, 2025
This was referenced Dec 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements support for SQL commenter plugins to Prisma Client, as described in the RFC. The feature will allow users to add metadata to SQL queries as comments following the sqlcommenter format.
Goals
@prisma/sqlcommenterpackage with public-facing typesSqlCommenterPlugininterface andSqlCommenterContexttypecommentsoption toPrismaClientconstructorOut of Scope
traceContext,queryInsights)Component Overview
We try to execute the comment plugins as close to the query execution time as possible, to have the most accurate context available. However, for RemoteExecutor (Accelerate), we need to pre-compute the comments before sending the request to the query plan executor server because we can't serialize the plugins themselves. This means that the future
traceContextplugin will point at the query span as its traceparent when using LocalExecutor, but when using RemoteExecutor, the traceparent will be theprisma:client:operationspan.Type Definitions
The types live in a new
@prisma/sqlcommenterpackage to keep plugin dependencies minimal.I also had to extract a small internal
@prisma/json-protocolpackage. Most of the JSON protocol types are kept internal for now and the query shapes are made opaque in the sqlcommenter plugins, but the available operation types (JsonQueryAction) are exposed to users because they directly correspond to the operations provided byPrismaClientand therefore don't expand our public API surface.Closes: https://linear.app/prisma-company/issue/TML-1644/implement-sqlcommenter-plugins-for-prisma-orm
Closes: #13360
Closes: #8508
Closes: #10746