Skip to content

SQLCommenter for Drizzle

@query-doctor/sqlcommenter-drizzle patches Drizzle ORM so every SQL query it executes carries a structured comment with contextual metadata — the source file, HTTP route, request method, and OpenTelemetry trace context. Query Doctor reads these comments to connect slow or failing queries back to the code and request that produced them.

  • Node.js >= 24.8.0
  • Drizzle ORM >= 0.35.0
  • ESM project ("type": "module" in your package.json) — the package does not ship a CommonJS build
Terminal window
npm install @query-doctor/sqlcommenter-drizzle

Wrap your Drizzle instance with patchDrizzle:

import { drizzle } from "drizzle-orm/node-postgres";
import { patchDrizzle } from "@query-doctor/sqlcommenter-drizzle";
const db = patchDrizzle(drizzle(process.env.DATABASE_URL));

Every query executed through db now includes a SQL comment with:

FieldIncluded by defaultDescription
db_driverYesIdentifies Drizzle as the driver
fileYesSource file that triggered the query
routeNoHTTP route (requires framework middleware)
methodNoHTTP request method (requires framework middleware)
Trace contextAutomaticOpenTelemetry trace/span IDs when a tracer is active
Custom fieldsNoAny arbitrary key/value metadata you provide

To capture route and method, add the withRequestContext middleware from the /http export.

import express from "express";
import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http";
const app = express();
app.use(withRequestContext());
import { Hono } from "hono";
import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http";
const app = new Hono();
app.use(withRequestContext());
import Fastify from "fastify";
import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http";
const fastify = Fastify();
fastify.addHook("onRequest", withRequestContext());

The package is primarily tested on PostgreSQL but should work with any database Drizzle supports, since the annotation is applied at the query string level.