packages/sqlcommenter-query-insights/README.md
A SQL commenter plugin for Prisma ORM that adds query shape information to SQL comments. This enables observability tools to analyze and group queries by their structural patterns rather than specific values.
npm install @prisma/sqlcommenter-query-insights
import { prismaQueryInsights } from '@prisma/sqlcommenter-query-insights'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({
adapter: myAdapter, // Driver adapter required (alternatively, Accelerate URL)
comments: [prismaQueryInsights()],
})
The resulting SQL will include comments like:
SELECT ... FROM "User" /*prismaQuery='User.findMany:eyJ3aGVyZSI6eyJhY3RpdmUiOnsiJHR5cGUiOiJQYXJhbSJ9fSwiaW5jbHVkZSI6eyJwb3N0cyI6dHJ1ZX19'*/
This plugin adds a prismaQuery comment tag to every SQL query. The tag contains:
User, Post)findMany, createOne, updateOne)| Query Type | Output Format |
|---|---|
| Raw query | queryRaw |
| Simple find (all fields) | User.findMany:e30 |
| Find with where | User.findUnique:eyJ3aGVyZSI6eyJpZCI6eyIkdHlwZSI6IlBhcmFtIn19fQ |
| Find with include | User.findMany:eyJpbmNsdWRlIjp7InBvc3RzIjp0cnVlfX0 |
| Find with select | User.findMany:eyJzZWxlY3QiOnsibmFtZSI6dHJ1ZX19 |
| Batched queries | User.findUnique:W3sid2hlcmUiOnsiaWQiOnsiJHR5cGUiOiJQYXJhbSJ9fX1d |
User data is never included in the comments. All values that could contain user data are replaced with placeholder markers before encoding. This includes:
where clauses)create/update operations)equals, contains, in, etc.)Only structural information is preserved:
take, skip)Group queries by their shape to identify:
The prismaQuery tag can be parsed by observability tools to:
Quickly identify which Prisma operation generated a specific SQL query in your database logs.
import { prismaQueryInsights } from '@prisma/sqlcommenter-query-insights'
import { traceContext } from '@prisma/sqlcommenter-trace-context'
import { queryTags, withQueryTags } from '@prisma/sqlcommenter-query-tags'
const prisma = new PrismaClient({
adapter: myAdapter,
comments: [prismaQueryInsights(), traceContext(), queryTags()],
})
// All tags are merged into the SQL comment
await withQueryTags({ route: '/api/users' }, () => prisma.user.findMany())
prismaQueryInsights()Creates a SQL commenter plugin that adds query shape information.
function prismaQueryInsights(): SqlCommenterPlugin
Returns: A SqlCommenterPlugin that adds the prismaQuery tag.
For integrators building observability tools that parse these comments, see the Embedder Documentation.
Apache-2.0