Skip to content

Commit 729bf1e

Browse files
committed
add drizzle support
1 parent d1c59a4 commit 729bf1e

11 files changed

Lines changed: 1718 additions & 4 deletions

File tree

nodejs/sqlcommenter-nodejs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# sqlcommenter
22

3+
For drizzle changes, see [packages/sqlcommenter-drizzle](./packages/sqlcommenter-drizzle/README.md)
4+
35
sqlcommenter is a suite of plugins/middleware/wrappers to augment SQL statements from ORMs/Querybuilders
46
with comments that can be used later to correlate user code with SQL statements.
57

@@ -15,7 +17,7 @@ It supports Node v6 and above to use ES6 features.
1517
Go into either of the packages [packages/knex](./packages/knex) or [packages/sequelize](./packages/sequelize)
1618
and then you can run respectively
1719

18-
Middleware|Command|URL
19-
---|---|---
20-
Knex.js|`npm install @google-cloud/sqlcommenter-knex`|https://www.npmjs.com/package/@google-cloud/sqlcommenter-knex
21-
Sequelize.js|`npm install @google-cloud/sqlcommenter-sequelize`|https://www.npmjs.com/package/@google-cloud/sqlcommenter-sequelize
20+
| Middleware | Command | URL |
21+
| ------------ | -------------------------------------------------- | ------------------------------------------------------------------ |
22+
| Knex.js | `npm install @google-cloud/sqlcommenter-knex` | https://www.npmjs.com/package/@google-cloud/sqlcommenter-knex |
23+
| Sequelize.js | `npm install @google-cloud/sqlcommenter-sequelize` | https://www.npmjs.com/package/@google-cloud/sqlcommenter-sequelize |
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@query-doctor/sqlcommenter-drizzle",
3+
"version": "0.0.1",
4+
"description": "SQLCommenter patch for drizzle-orm",
5+
"main": "dist/index.js",
6+
"type": "module",
7+
"types": "dist/index.d.ts",
8+
"files": [
9+
"dist"
10+
],
11+
"exports": {
12+
".": {
13+
"import": "./dist/index.js",
14+
"types": "./dist/index.d.ts"
15+
},
16+
"./http": {
17+
"import": "./dist/http.js",
18+
"types": "./dist/http.d.ts"
19+
}
20+
},
21+
"devDependencies": {
22+
"@types/node": "^24.5.2",
23+
"hono": "^4.9.8",
24+
"rewiremock": "^3.14.3"
25+
},
26+
"peerDependencies": {
27+
"@opentelemetry/core": "^2.1.0",
28+
"drizzle-orm": "^0.44.5 || ^1.0.0"
29+
},
30+
"engines": {
31+
"node": ">=20.0.0"
32+
},
33+
"scripts": {
34+
"test": "mocha --recursive",
35+
"build": "tsc"
36+
},
37+
"repository": {
38+
"type": "git",
39+
"url": "query-doctor/sqlcommenter"
40+
},
41+
"author": "Query Doctor",
42+
"license": "Apache-2.0",
43+
"packageManager": "pnpm@10.16.1+sha512.0e155aa2629db8672b49e8475da6226aa4bdea85fdcdfdc15350874946d4f3c91faaf64cbdc4a5d1ab8002f473d5c3fcedcd197989cf0390f9badd3c04678706",
44+
"dependencies": {
45+
"@opentelemetry/api": "^1.9.0"
46+
}
47+
}

0 commit comments

Comments
 (0)