Skip to content

SQLCommenter for TypeORM

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

@query-doctor/sqlcommenter-typeorm wraps your TypeORM DataSource so 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
  • TypeORM >= 0.3.0
  • ESM project ("type": "module" in your package.json)
```sh npm install @query-doctor/sqlcommenter-typeorm ``` ```sh pnpm add @query-doctor/sqlcommenter-typeorm ```

Wrap your TypeORM DataSource with patchTypeORM:

import { DataSource } from "typeorm";
import { patchTypeORM } from "@query-doctor/sqlcommenter-typeorm";
const dataSource = patchTypeORM(new DataSource({
type: "postgres",
url: process.env.DATABASE_URL,
}));

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

TagIncluded by defaultDescription
db_driverYesIdentifies TypeORM 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-typeorm/http";
app.use((req, res, next) => {
withRequestContext({ route: req.route.path, method: req.method }, next);
});
import { withRequestContext } from "@query-doctor/sqlcommenter-typeorm/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-typeorm/http";
app.addHook("onRequest", (request, _, done) => {
withRequestContext(
{ route: request.routerPath, method: request.method },
done
);
});
import { withRequestContext } from "@query-doctor/sqlcommenter-typeorm/http";
@Injectable()
export class SqlcommenterMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
withRequestContext({ route: req.path, method: req.method }, next);
}
}