Skip to content

SQLCommenter for MikroORM

import { Tabs, TabItem } from “@astrojs/starlight/components”;

@query-doctor/sqlcommenter-mikroorm hooks into MikroORM’s built-in onQuery configuration — no monkey-patching required. Every SQL query carries a structured SQLCommenter comment with contextual metadata. Query Doctor reads these comments to connect queries back to the code and request that produced them.

  • Node.js >= 24.8.0
  • MikroORM >= 6.4.0
  • ESM project ("type": "module" in your package.json)
```sh npm install @query-doctor/sqlcommenter-mikroorm ``` ```sh pnpm add @query-doctor/sqlcommenter-mikroorm ```

Call patchMikroORM on your MikroORM instance after initialization:

import { MikroORM } from "@mikro-orm/core";
import { patchMikroORM } from "@query-doctor/sqlcommenter-mikroorm";
const mikroORM = await MikroORM.init({
dbName: "my-db",
entities: [...],
});
const orm = patchMikroORM(mikroORM);

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

TagIncluded by defaultDescription
db_driverYesIdentifies MikroORM 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, use the withRequestContext function from the /http export. You can also pass any arbitrary key/value metadata.

import { withRequestContext } from "@query-doctor/sqlcommenter-mikroorm/http";
app.use((req, res, next) => {
withRequestContext({ route: req.route.path, method: req.method }, next);
});
import { withRequestContext } from "@query-doctor/sqlcommenter-mikroorm/http";
import { routePath } from "hono/route";
app.use((c, next) => {
withRequestContext({ route: routePath(c), method: c.req.method }, next);
});
import { withRequestContext } from "@query-doctor/sqlcommenter-mikroorm/http";
app.addHook("onRequest", (request, _, done) => {
withRequestContext(
{ route: request.routerPath, method: request.method },
done
);
});
import { withRequestContext } from "@query-doctor/sqlcommenter-mikroorm/http";
@Injectable()
export class SqlcommenterMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
withRequestContext({ route: req.path, method: req.method }, next);
}
}