|
| 1 | +# @query-doctor/sqlcommenter-drizzle |
| 2 | + |
| 3 | +Drizzle sqlcommenter support for drizzle >= 0.34.0 (including 1.0.0 beta). |
| 4 | + |
| 5 | +Only tested on Postgres, but theoretically it should be compatible with all clients supported by drizzle. |
| 6 | + |
| 7 | +Emits the following fields into the query: |
| 8 | + |
| 9 | +| name | included by default? | description | |
| 10 | +| ------------- | -------------------- | --------------------------------------------------------------- | |
| 11 | +| db_driver | Yes | The driver used to connect to the database. (Drizzle) | |
| 12 | +| file | Yes | The file that the query was executed in. | |
| 13 | +| route | No | The route that the query was executed in. | |
| 14 | +| method | No | The http method for the request that the query was executed in. | |
| 15 | +| anything else | No | Any other information that the user wants to add to the query. | |
| 16 | + |
| 17 | +It also emits the trace context, if available. |
| 18 | + |
| 19 | +### Installation |
| 20 | + |
| 21 | +```shell |
| 22 | +npm install @query-doctor/sqlcommenter-drizzle |
| 23 | +pnpm add @query-doctor/sqlcommenter-drizzle |
| 24 | +``` |
| 25 | + |
| 26 | +### Usage |
| 27 | + |
| 28 | +Simply wrap your drizzle instance with the `patchDrizzle` function. |
| 29 | + |
| 30 | +Before: |
| 31 | + |
| 32 | +```ts |
| 33 | +// db/drizle.ts |
| 34 | +import { drizzle } from "drizzle-orm/postgres-js"; |
| 35 | + |
| 36 | +const db = drizzle(process.env.DATABASE_URL); |
| 37 | +``` |
| 38 | + |
| 39 | +After: |
| 40 | + |
| 41 | +```ts |
| 42 | +// db/drizle.ts |
| 43 | +import { drizzle } from "drizzle-orm/postgres-js"; |
| 44 | +import { patchDrizzle } from "@query-doctor/sqlcommenter-drizzle"; |
| 45 | + |
| 46 | +const db = patchDrizzle(drizzle(process.env.DATABASE_URL)); |
| 47 | +``` |
| 48 | + |
| 49 | +### Emitting route information |
| 50 | + |
| 51 | +To include route information in the comments, patching Drizzle by itself is not enough. You need to use the `withRequestContext` function to pass along relevant information to the query comments. |
| 52 | + |
| 53 | +You can add any arbitrary information to the request context aside from `route`, `method` and `controller`. |
| 54 | + |
| 55 | +Here are some examples of how to use it with different frameworks: |
| 56 | + |
| 57 | +#### Express |
| 58 | + |
| 59 | +```ts |
| 60 | +import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http"; |
| 61 | + |
| 62 | +app.use((req, res, next) => { |
| 63 | + withRequestContext({ route: req.route.path, method: req.method }, next); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +#### Hono |
| 68 | + |
| 69 | +```ts |
| 70 | +import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http"; |
| 71 | +import { routePath } from "hono/route"; |
| 72 | + |
| 73 | +app.use((c, next) => { |
| 74 | + withRequestContext({ route: routePath(c), method: c.req.method }, next); |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +#### Fastify |
| 79 | + |
| 80 | +```ts |
| 81 | +import { withRequestContext } from "@query-doctor/sqlcommenter-drizzle/http"; |
| 82 | + |
| 83 | +app.addHook("onRequest", (request, _, done) => { |
| 84 | + withRequestContext( |
| 85 | + { |
| 86 | + route: request.routerPath, |
| 87 | + method: request.method, |
| 88 | + }, |
| 89 | + done |
| 90 | + ); |
| 91 | +}); |
| 92 | +``` |
0 commit comments