Skip to content

feat: implement sqlcommenter plugins for Prisma Client#28769

Merged
aqrln merged 1 commit intomainfrom
aqrln-xrtkmtxnsrly
Dec 2, 2025
Merged

feat: implement sqlcommenter plugins for Prisma Client#28769
aqrln merged 1 commit intomainfrom
aqrln-xrtkmtxnsrly

Conversation

@aqrln
Copy link
Copy Markdown
Member

@aqrln aqrln commented Nov 28, 2025

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

  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)

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 types live in a new @prisma/sqlcommenter package to keep plugin dependencies minimal.

I also had to extract a small internal @prisma/json-protocol package. 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 by PrismaClient and 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

@aqrln aqrln added this to the 7.1.0 milestone Nov 28, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 28, 2025

size-limit report 📦

Path Size
packages/client/runtime/index-browser.js 2.23 KB (0%)
packages/client/runtime/index-browser.d.ts 3.28 KB (0%)
packages/cli/build/index.js 2.51 MB (+0.06% 🔺)
packages/client/prisma-client-0.0.0.tgz 10.23 MB (+0.14% 🔺)
packages/cli/prisma-0.0.0.tgz 6.71 MB (+0.01% 🔺)
packages/bundle-size/da-workers-libsql/output.tgz 805.59 KB (+0.09% 🔺)
packages/bundle-size/da-workers-neon/output.tgz 859.27 KB (+0.08% 🔺)
packages/bundle-size/da-workers-pg/output.tgz 852.2 KB (+0.09% 🔺)
packages/bundle-size/da-workers-planetscale/output.tgz 804.98 KB (+0.09% 🔺)
packages/bundle-size/da-workers-d1/output.tgz 788.47 KB (+0.09% 🔺)

@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch 4 times, most recently from e6a7f4a to 5ce15c5 Compare December 1, 2025 11:51
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from 5ce15c5 to 5209041 Compare December 1, 2025 13:26
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from 5209041 to f6ac66a Compare December 1, 2025 13:56
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln marked this pull request as ready for review December 1, 2025 14:02
Copilot AI review requested due to automatic review settings December 1, 2025 14:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from f6ac66a to 8e76f7f Compare December 1, 2025 14:24
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from 8e76f7f to 556ac2c Compare December 1, 2025 14:29
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from 556ac2c to 66b0a6c Compare December 1, 2025 17:52
aqrln added a commit that referenced this pull request Dec 1, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for solving the use case of <#10746>.
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from 66b0a6c to a993fb7 Compare December 1, 2025 17:56
## 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
@aqrln aqrln force-pushed the aqrln-xrtkmtxnsrly branch from a993fb7 to 6194dde Compare December 2, 2025 09:48
Copy link
Copy Markdown
Contributor

@FGoessler FGoessler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@aqrln aqrln merged commit 44cac9f into main Dec 2, 2025
252 of 254 checks passed
@aqrln aqrln deleted the aqrln-xrtkmtxnsrly branch December 2, 2025 15:11
aqrln added a commit that referenced this pull request Dec 3, 2025
This serves as a demo plugin for
#28769 and provides a higher-level
abstraction for <#10746>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Prisma Tags to our Sql Commenter tracing for extra information Query Comments Google Cloud Query Insights Tags (with sqlcommenter) support

3 participants