@prisma/sqlcommenter-query-tags 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +260 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.d.ts +176 -0
- package/dist/index.js +46 -0
- package/dist/index.mjs +19 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @prisma/sqlcommenter-query-tags
|
|
2
|
+
|
|
3
|
+
An `AsyncLocalStorage`-based query tagging plugin for Prisma ORM's SQL commenter feature. This package allows you to add ad-hoc tags to your SQL queries that will be appended as comments, useful for tracing, debugging, and observability.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @prisma/sqlcommenter-query-tags
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { queryTags, withQueryTags } from '@prisma/sqlcommenter-query-tags'
|
|
15
|
+
import { PrismaClient } from '@prisma/client'
|
|
16
|
+
|
|
17
|
+
const prisma = new PrismaClient({
|
|
18
|
+
adapter: myAdapter, // Driver adapter required (alternatively, Accelerate URL)
|
|
19
|
+
comments: [queryTags()],
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
// Wrap your queries to add tags
|
|
23
|
+
const posts = await withQueryTags({ route: '/api/posts', user: 'user-123' }, () => prisma.post.findMany())
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The resulting SQL will include comments like:
|
|
27
|
+
|
|
28
|
+
```sql
|
|
29
|
+
SELECT ... FROM "Post" /*route='/api/posts',user='user-123'*/
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `queryTags()`
|
|
35
|
+
|
|
36
|
+
Creates a SQL commenter plugin that retrieves query tags from `AsyncLocalStorage`.
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
function queryTags(): SqlCommenterPlugin
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### `withQueryTags(tags, scope)`
|
|
43
|
+
|
|
44
|
+
Executes a function with the given query tags added to all SQL queries within its scope.
|
|
45
|
+
Nested calls to `withQueryTags` completely replace the outer tags.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
function withQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Parameters:**
|
|
52
|
+
|
|
53
|
+
- `tags` - Key-value pairs to add as SQL comments. Keys with `undefined` values are filtered out.
|
|
54
|
+
- `scope` - An async function to execute within the tagged scope
|
|
55
|
+
|
|
56
|
+
**Returns:** The result of the scope function
|
|
57
|
+
|
|
58
|
+
### `withMergedQueryTags(tags, scope)`
|
|
59
|
+
|
|
60
|
+
Executes a function with merged query tags added to all SQL queries within its scope.
|
|
61
|
+
Unlike `withQueryTags`, this function merges the provided tags with any existing tags
|
|
62
|
+
from an outer scope, with the new tags taking precedence for keys that exist in both.
|
|
63
|
+
Setting a key to `undefined` removes it from the merged result.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
function withMergedQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Parameters:**
|
|
70
|
+
|
|
71
|
+
- `tags` - Key-value pairs to merge with existing tags. Keys with `undefined` values remove that key from the result.
|
|
72
|
+
- `scope` - An async function to execute within the tagged scope
|
|
73
|
+
|
|
74
|
+
**Returns:** The result of the scope function
|
|
75
|
+
|
|
76
|
+
## Examples
|
|
77
|
+
|
|
78
|
+
### Basic Usage
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const users = await withQueryTags({ requestId: 'abc-123', endpoint: '/api/users' }, () => prisma.user.findMany())
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Multiple Queries in One Scope
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
const result = await withQueryTags({ traceId: 'trace-456' }, async () => {
|
|
88
|
+
const users = await prisma.user.findMany()
|
|
89
|
+
const posts = await prisma.post.findMany()
|
|
90
|
+
return { users, posts }
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Merging Tags in Nested Scopes
|
|
95
|
+
|
|
96
|
+
Use `withMergedQueryTags` when you want to add tags without losing the outer context:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// Set base tags at the request level
|
|
100
|
+
await withQueryTags({ requestId: 'req-123', source: 'api' }, async () => {
|
|
101
|
+
// Add handler-specific tags while keeping requestId
|
|
102
|
+
await withMergedQueryTags({ userId: 'user-456', source: 'handler' }, async () => {
|
|
103
|
+
// Queries here will have: requestId='req-123', userId='user-456', source='handler'
|
|
104
|
+
await prisma.user.findMany()
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Removing Tags in Nested Scopes
|
|
110
|
+
|
|
111
|
+
Use `undefined` to explicitly remove a tag set by an outer scope:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
await withQueryTags({ requestId: 'req-123', debug: 'true' }, async () => {
|
|
115
|
+
// Remove 'debug' tag for this inner scope
|
|
116
|
+
await withMergedQueryTags({ userId: 'user-456', debug: undefined }, async () => {
|
|
117
|
+
// Queries here will have: requestId='req-123', userId='user-456' (debug removed)
|
|
118
|
+
await prisma.user.findMany()
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Combining with Other Plugins
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import type { SqlCommenterPlugin } from '@prisma/sqlcommenter'
|
|
127
|
+
|
|
128
|
+
const appPlugin: SqlCommenterPlugin = () => ({
|
|
129
|
+
application: 'my-app',
|
|
130
|
+
version: '1.0.0',
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const prisma = new PrismaClient({
|
|
134
|
+
adapter: myAdapter,
|
|
135
|
+
comments: [appPlugin, queryTags()], // Tags from both plugins are merged
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Request Context in Web Frameworks
|
|
140
|
+
|
|
141
|
+
For `withQueryTags` to work correctly, all database queries must execute **inside** the callback scope. This requires proper async middleware that awaits downstream handlers (Hono, Koa), or wrapping handlers/services directly (Express, Fastify, NestJS).
|
|
142
|
+
|
|
143
|
+
#### Hono
|
|
144
|
+
|
|
145
|
+
Hono's middleware properly awaits downstream handlers, making it ideal for request-scoped tagging:
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { createMiddleware } from 'hono/factory'
|
|
149
|
+
|
|
150
|
+
app.use(
|
|
151
|
+
createMiddleware(async (c, next) => {
|
|
152
|
+
await withQueryTags(
|
|
153
|
+
{
|
|
154
|
+
route: c.req.path,
|
|
155
|
+
method: c.req.method,
|
|
156
|
+
requestId: c.req.header('x-request-id') ?? crypto.randomUUID(),
|
|
157
|
+
},
|
|
158
|
+
() => next(),
|
|
159
|
+
)
|
|
160
|
+
}),
|
|
161
|
+
)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### Koa
|
|
165
|
+
|
|
166
|
+
Koa's middleware properly awaits downstream handlers:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
app.use(async (ctx, next) => {
|
|
170
|
+
await withQueryTags(
|
|
171
|
+
{
|
|
172
|
+
route: ctx.path,
|
|
173
|
+
method: ctx.method,
|
|
174
|
+
requestId: ctx.get('x-request-id') || crypto.randomUUID(),
|
|
175
|
+
},
|
|
176
|
+
() => next(),
|
|
177
|
+
)
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### Fastify
|
|
182
|
+
|
|
183
|
+
Wrap individual route handlers since hooks complete before the handler runs:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
fastify.get('/users', (request, reply) => {
|
|
187
|
+
return withQueryTags(
|
|
188
|
+
{
|
|
189
|
+
route: '/users',
|
|
190
|
+
method: 'GET',
|
|
191
|
+
requestId: request.id,
|
|
192
|
+
},
|
|
193
|
+
() => prisma.user.findMany(),
|
|
194
|
+
)
|
|
195
|
+
})
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Express
|
|
199
|
+
|
|
200
|
+
Express middleware uses callbacks, so `next()` returns immediately without waiting for downstream handlers. Wrap route handlers directly instead:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
app.get('/users', (req, res, next) => {
|
|
204
|
+
withQueryTags(
|
|
205
|
+
{
|
|
206
|
+
route: req.path,
|
|
207
|
+
method: req.method,
|
|
208
|
+
requestId: req.header('x-request-id') ?? crypto.randomUUID(),
|
|
209
|
+
},
|
|
210
|
+
() => prisma.user.findMany(),
|
|
211
|
+
)
|
|
212
|
+
.then((users) => res.json(users))
|
|
213
|
+
.catch(next)
|
|
214
|
+
})
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### NestJS
|
|
218
|
+
|
|
219
|
+
Use an interceptor, which properly wraps the handler execution:
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'
|
|
223
|
+
import { Observable, from } from 'rxjs'
|
|
224
|
+
|
|
225
|
+
@Injectable()
|
|
226
|
+
export class QueryTagsInterceptor implements NestInterceptor {
|
|
227
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
228
|
+
const request = context.switchToHttp().getRequest<Request>()
|
|
229
|
+
return from(
|
|
230
|
+
withQueryTags(
|
|
231
|
+
{
|
|
232
|
+
route: request.path,
|
|
233
|
+
method: request.method,
|
|
234
|
+
requestId: request.header('x-request-id') ?? crypto.randomUUID(),
|
|
235
|
+
},
|
|
236
|
+
() => lastValueFrom(next.handle()),
|
|
237
|
+
),
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Apply globally in main.ts
|
|
243
|
+
app.useGlobalInterceptors(new QueryTagsInterceptor())
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## How It Works
|
|
247
|
+
|
|
248
|
+
This package uses Node.js `AsyncLocalStorage` to maintain context across async operations. When you call `withQueryTags()` or `withMergedQueryTags()`, the tags are stored in the async local storage context. The `queryTags()` plugin retrieves these tags when Prisma executes queries within that scope.
|
|
249
|
+
|
|
250
|
+
**Key behaviors:**
|
|
251
|
+
|
|
252
|
+
- Tags are scoped to the callback and don't leak to queries outside
|
|
253
|
+
- `withQueryTags` replaces the outer tags entirely in nested calls
|
|
254
|
+
- `withMergedQueryTags` merges with outer tags, with new tags taking precedence for duplicate keys; `undefined` values remove keys
|
|
255
|
+
- Multiple `queryTags()` plugin instances share the same `AsyncLocalStorage` context
|
|
256
|
+
- Tags from multiple plugins are merged, with later plugins overriding earlier ones for the same key
|
|
257
|
+
|
|
258
|
+
## License
|
|
259
|
+
|
|
260
|
+
Apache-2.0
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
declare type JsonQueryAction = 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'createOne' | 'createMany' | 'createManyAndReturn' | 'updateOne' | 'updateMany' | 'updateManyAndReturn' | 'deleteOne' | 'deleteMany' | 'upsertOne' | 'aggregate' | 'groupBy' | 'executeRaw' | 'queryRaw' | 'runCommandRaw' | 'findRaw' | 'aggregateRaw';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a SQL commenter plugin that retrieves query tags from AsyncLocalStorage.
|
|
5
|
+
* Use this plugin with `withQueryTags()` to add ad-hoc tags to your queries.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { queryTags, withQueryTags } from '@prisma/sqlcommenter-query-tags'
|
|
10
|
+
*
|
|
11
|
+
* const prisma = new PrismaClient({
|
|
12
|
+
* adapter: myAdapter,
|
|
13
|
+
* comments: [queryTags()],
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* // Later, wrap your queries to add tags:
|
|
17
|
+
* const posts = await withQueryTags(
|
|
18
|
+
* { route: '/api/posts', user: 'user-123' },
|
|
19
|
+
* () => prisma.post.findMany()
|
|
20
|
+
* )
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function queryTags(): SqlCommenterPlugin;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Information about a compacted batch query (e.g. multiple independent
|
|
27
|
+
* `findUnique` queries automatically merged into a single `SELECT` SQL
|
|
28
|
+
* statement).
|
|
29
|
+
*/
|
|
30
|
+
declare interface SqlCommenterCompactedQueryInfo {
|
|
31
|
+
/**
|
|
32
|
+
* The model name (e.g., "User", "Post").
|
|
33
|
+
*/
|
|
34
|
+
readonly modelName: string;
|
|
35
|
+
/**
|
|
36
|
+
* The Prisma operation (e.g., "findUnique").
|
|
37
|
+
*/
|
|
38
|
+
readonly action: SqlCommenterQueryAction;
|
|
39
|
+
/**
|
|
40
|
+
* The full query objects (selections, arguments, etc.).
|
|
41
|
+
* Specifics of the query representation are not part of the public API yet.
|
|
42
|
+
*/
|
|
43
|
+
readonly queries: ReadonlyArray<unknown>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Context provided to SQL commenter plugins.
|
|
48
|
+
*/
|
|
49
|
+
declare interface SqlCommenterContext {
|
|
50
|
+
/**
|
|
51
|
+
* Information about the Prisma query being executed.
|
|
52
|
+
*/
|
|
53
|
+
readonly query: SqlCommenterQueryInfo;
|
|
54
|
+
/**
|
|
55
|
+
* Raw SQL query generated from this Prisma query.
|
|
56
|
+
*
|
|
57
|
+
* It is always available when `PrismaClient` connects to the database and
|
|
58
|
+
* renders SQL queries directly.
|
|
59
|
+
*
|
|
60
|
+
* When using Prisma Accelerate, SQL rendering happens on Accelerate side and the raw
|
|
61
|
+
* SQL strings are not yet available when SQL commenter plugins are executed.
|
|
62
|
+
*/
|
|
63
|
+
readonly sql?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A SQL commenter plugin that returns key-value pairs to be added as comments.
|
|
68
|
+
* Return an empty object to add no comments. Keys with undefined values will be omitted.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const myPlugin: SqlCommenterPlugin = (context) => {
|
|
73
|
+
* return {
|
|
74
|
+
* application: 'my-app',
|
|
75
|
+
* model: context.query.modelName ?? 'raw',
|
|
76
|
+
* // Conditional key - will be omitted if ctx.sql is undefined
|
|
77
|
+
* sqlLength: context.sql ? String(context.sql.length) : undefined,
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare interface SqlCommenterPlugin {
|
|
83
|
+
(context: SqlCommenterContext): SqlCommenterTags;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Prisma query type corresponding to this SQL query.
|
|
88
|
+
*/
|
|
89
|
+
declare type SqlCommenterQueryAction = JsonQueryAction;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Information about the query or queries being executed.
|
|
93
|
+
*
|
|
94
|
+
* - `single`: A single query is being executed
|
|
95
|
+
* - `compacted`: Multiple queries have been compacted into a single SQL statement
|
|
96
|
+
*/
|
|
97
|
+
declare type SqlCommenterQueryInfo = ({
|
|
98
|
+
readonly type: 'single';
|
|
99
|
+
} & SqlCommenterSingleQueryInfo) | ({
|
|
100
|
+
readonly type: 'compacted';
|
|
101
|
+
} & SqlCommenterCompactedQueryInfo);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Information about a single Prisma query.
|
|
105
|
+
*/
|
|
106
|
+
declare interface SqlCommenterSingleQueryInfo {
|
|
107
|
+
/**
|
|
108
|
+
* The model name (e.g., "User", "Post"). Undefined for raw queries.
|
|
109
|
+
*/
|
|
110
|
+
readonly modelName?: string;
|
|
111
|
+
/**
|
|
112
|
+
* The Prisma operation (e.g., "findMany", "createOne", "queryRaw").
|
|
113
|
+
*/
|
|
114
|
+
readonly action: SqlCommenterQueryAction;
|
|
115
|
+
/**
|
|
116
|
+
* The full query object (selection, arguments, etc.).
|
|
117
|
+
* Specifics of the query representation are not part of the public API yet.
|
|
118
|
+
*/
|
|
119
|
+
readonly query: unknown;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Key-value pairs to add as SQL comments.
|
|
124
|
+
* Keys with undefined values will be omitted from the final comment.
|
|
125
|
+
*/
|
|
126
|
+
declare type SqlCommenterTags = {
|
|
127
|
+
readonly [key: string]: string | undefined;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Executes a function with merged query tags added to all SQL queries within its scope.
|
|
132
|
+
* Unlike `withQueryTags`, this function merges the provided tags with any existing tags
|
|
133
|
+
* from an outer scope, with the new tags taking precedence for keys that exist in both.
|
|
134
|
+
*
|
|
135
|
+
* @param tags - Key-value pairs to merge with existing tags
|
|
136
|
+
* @param scope - An async function to execute within the tagged scope
|
|
137
|
+
* @returns The result of the scope function
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* // Outer scope sets base tags
|
|
142
|
+
* await withQueryTags({ requestId: 'req-123', source: 'api' }, async () => {
|
|
143
|
+
* // Inner scope adds/overrides tags
|
|
144
|
+
* await withMergedQueryTags({ userId: 'user-456', source: 'handler' }, async () => {
|
|
145
|
+
* // Queries here will have: requestId='req-123', userId='user-456', source='handler'
|
|
146
|
+
* await prisma.user.findMany()
|
|
147
|
+
* })
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare function withMergedQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Executes a function with the given query tags added to all SQL queries within its scope.
|
|
155
|
+
* Tags are stored in AsyncLocalStorage and retrieved by the `queryTags()` plugin.
|
|
156
|
+
*
|
|
157
|
+
* @param tags - Key-value pairs to add as SQL comments
|
|
158
|
+
* @param scope - An async function to execute within the tagged scope
|
|
159
|
+
* @returns The result of the scope function
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* const result = await withQueryTags(
|
|
164
|
+
* { route: '/api/users', requestId: 'abc-123' },
|
|
165
|
+
* async () => {
|
|
166
|
+
* // All queries here will have the tags attached
|
|
167
|
+
* const users = await prisma.user.findMany()
|
|
168
|
+
* const posts = await prisma.post.findMany()
|
|
169
|
+
* return { users, posts }
|
|
170
|
+
* }
|
|
171
|
+
* )
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export declare function withQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>;
|
|
175
|
+
|
|
176
|
+
export { }
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
declare type JsonQueryAction = 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'createOne' | 'createMany' | 'createManyAndReturn' | 'updateOne' | 'updateMany' | 'updateManyAndReturn' | 'deleteOne' | 'deleteMany' | 'upsertOne' | 'aggregate' | 'groupBy' | 'executeRaw' | 'queryRaw' | 'runCommandRaw' | 'findRaw' | 'aggregateRaw';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a SQL commenter plugin that retrieves query tags from AsyncLocalStorage.
|
|
5
|
+
* Use this plugin with `withQueryTags()` to add ad-hoc tags to your queries.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { queryTags, withQueryTags } from '@prisma/sqlcommenter-query-tags'
|
|
10
|
+
*
|
|
11
|
+
* const prisma = new PrismaClient({
|
|
12
|
+
* adapter: myAdapter,
|
|
13
|
+
* comments: [queryTags()],
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* // Later, wrap your queries to add tags:
|
|
17
|
+
* const posts = await withQueryTags(
|
|
18
|
+
* { route: '/api/posts', user: 'user-123' },
|
|
19
|
+
* () => prisma.post.findMany()
|
|
20
|
+
* )
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function queryTags(): SqlCommenterPlugin;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Information about a compacted batch query (e.g. multiple independent
|
|
27
|
+
* `findUnique` queries automatically merged into a single `SELECT` SQL
|
|
28
|
+
* statement).
|
|
29
|
+
*/
|
|
30
|
+
declare interface SqlCommenterCompactedQueryInfo {
|
|
31
|
+
/**
|
|
32
|
+
* The model name (e.g., "User", "Post").
|
|
33
|
+
*/
|
|
34
|
+
readonly modelName: string;
|
|
35
|
+
/**
|
|
36
|
+
* The Prisma operation (e.g., "findUnique").
|
|
37
|
+
*/
|
|
38
|
+
readonly action: SqlCommenterQueryAction;
|
|
39
|
+
/**
|
|
40
|
+
* The full query objects (selections, arguments, etc.).
|
|
41
|
+
* Specifics of the query representation are not part of the public API yet.
|
|
42
|
+
*/
|
|
43
|
+
readonly queries: ReadonlyArray<unknown>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Context provided to SQL commenter plugins.
|
|
48
|
+
*/
|
|
49
|
+
declare interface SqlCommenterContext {
|
|
50
|
+
/**
|
|
51
|
+
* Information about the Prisma query being executed.
|
|
52
|
+
*/
|
|
53
|
+
readonly query: SqlCommenterQueryInfo;
|
|
54
|
+
/**
|
|
55
|
+
* Raw SQL query generated from this Prisma query.
|
|
56
|
+
*
|
|
57
|
+
* It is always available when `PrismaClient` connects to the database and
|
|
58
|
+
* renders SQL queries directly.
|
|
59
|
+
*
|
|
60
|
+
* When using Prisma Accelerate, SQL rendering happens on Accelerate side and the raw
|
|
61
|
+
* SQL strings are not yet available when SQL commenter plugins are executed.
|
|
62
|
+
*/
|
|
63
|
+
readonly sql?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A SQL commenter plugin that returns key-value pairs to be added as comments.
|
|
68
|
+
* Return an empty object to add no comments. Keys with undefined values will be omitted.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const myPlugin: SqlCommenterPlugin = (context) => {
|
|
73
|
+
* return {
|
|
74
|
+
* application: 'my-app',
|
|
75
|
+
* model: context.query.modelName ?? 'raw',
|
|
76
|
+
* // Conditional key - will be omitted if ctx.sql is undefined
|
|
77
|
+
* sqlLength: context.sql ? String(context.sql.length) : undefined,
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare interface SqlCommenterPlugin {
|
|
83
|
+
(context: SqlCommenterContext): SqlCommenterTags;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Prisma query type corresponding to this SQL query.
|
|
88
|
+
*/
|
|
89
|
+
declare type SqlCommenterQueryAction = JsonQueryAction;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Information about the query or queries being executed.
|
|
93
|
+
*
|
|
94
|
+
* - `single`: A single query is being executed
|
|
95
|
+
* - `compacted`: Multiple queries have been compacted into a single SQL statement
|
|
96
|
+
*/
|
|
97
|
+
declare type SqlCommenterQueryInfo = ({
|
|
98
|
+
readonly type: 'single';
|
|
99
|
+
} & SqlCommenterSingleQueryInfo) | ({
|
|
100
|
+
readonly type: 'compacted';
|
|
101
|
+
} & SqlCommenterCompactedQueryInfo);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Information about a single Prisma query.
|
|
105
|
+
*/
|
|
106
|
+
declare interface SqlCommenterSingleQueryInfo {
|
|
107
|
+
/**
|
|
108
|
+
* The model name (e.g., "User", "Post"). Undefined for raw queries.
|
|
109
|
+
*/
|
|
110
|
+
readonly modelName?: string;
|
|
111
|
+
/**
|
|
112
|
+
* The Prisma operation (e.g., "findMany", "createOne", "queryRaw").
|
|
113
|
+
*/
|
|
114
|
+
readonly action: SqlCommenterQueryAction;
|
|
115
|
+
/**
|
|
116
|
+
* The full query object (selection, arguments, etc.).
|
|
117
|
+
* Specifics of the query representation are not part of the public API yet.
|
|
118
|
+
*/
|
|
119
|
+
readonly query: unknown;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Key-value pairs to add as SQL comments.
|
|
124
|
+
* Keys with undefined values will be omitted from the final comment.
|
|
125
|
+
*/
|
|
126
|
+
declare type SqlCommenterTags = {
|
|
127
|
+
readonly [key: string]: string | undefined;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Executes a function with merged query tags added to all SQL queries within its scope.
|
|
132
|
+
* Unlike `withQueryTags`, this function merges the provided tags with any existing tags
|
|
133
|
+
* from an outer scope, with the new tags taking precedence for keys that exist in both.
|
|
134
|
+
*
|
|
135
|
+
* @param tags - Key-value pairs to merge with existing tags
|
|
136
|
+
* @param scope - An async function to execute within the tagged scope
|
|
137
|
+
* @returns The result of the scope function
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* // Outer scope sets base tags
|
|
142
|
+
* await withQueryTags({ requestId: 'req-123', source: 'api' }, async () => {
|
|
143
|
+
* // Inner scope adds/overrides tags
|
|
144
|
+
* await withMergedQueryTags({ userId: 'user-456', source: 'handler' }, async () => {
|
|
145
|
+
* // Queries here will have: requestId='req-123', userId='user-456', source='handler'
|
|
146
|
+
* await prisma.user.findMany()
|
|
147
|
+
* })
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare function withMergedQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Executes a function with the given query tags added to all SQL queries within its scope.
|
|
155
|
+
* Tags are stored in AsyncLocalStorage and retrieved by the `queryTags()` plugin.
|
|
156
|
+
*
|
|
157
|
+
* @param tags - Key-value pairs to add as SQL comments
|
|
158
|
+
* @param scope - An async function to execute within the tagged scope
|
|
159
|
+
* @returns The result of the scope function
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* const result = await withQueryTags(
|
|
164
|
+
* { route: '/api/users', requestId: 'abc-123' },
|
|
165
|
+
* async () => {
|
|
166
|
+
* // All queries here will have the tags attached
|
|
167
|
+
* const users = await prisma.user.findMany()
|
|
168
|
+
* const posts = await prisma.post.findMany()
|
|
169
|
+
* return { users, posts }
|
|
170
|
+
* }
|
|
171
|
+
* )
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export declare function withQueryTags<T>(tags: SqlCommenterTags, scope: () => PromiseLike<T>): Promise<T>;
|
|
175
|
+
|
|
176
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
queryTags: () => queryTags,
|
|
24
|
+
withMergedQueryTags: () => withMergedQueryTags,
|
|
25
|
+
withQueryTags: () => withQueryTags
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_node_async_hooks = require("node:async_hooks");
|
|
29
|
+
var asyncLocalStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
30
|
+
function queryTags() {
|
|
31
|
+
return () => asyncLocalStorage.getStore() ?? {};
|
|
32
|
+
}
|
|
33
|
+
function withQueryTags(tags, scope) {
|
|
34
|
+
return asyncLocalStorage.run(tags, async () => await scope());
|
|
35
|
+
}
|
|
36
|
+
function withMergedQueryTags(tags, scope) {
|
|
37
|
+
const existingTags = asyncLocalStorage.getStore() ?? {};
|
|
38
|
+
const mergedTags = { ...existingTags, ...tags };
|
|
39
|
+
return asyncLocalStorage.run(mergedTags, async () => await scope());
|
|
40
|
+
}
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
queryTags,
|
|
44
|
+
withMergedQueryTags,
|
|
45
|
+
withQueryTags
|
|
46
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
var asyncLocalStorage = new AsyncLocalStorage();
|
|
4
|
+
function queryTags() {
|
|
5
|
+
return () => asyncLocalStorage.getStore() ?? {};
|
|
6
|
+
}
|
|
7
|
+
function withQueryTags(tags, scope) {
|
|
8
|
+
return asyncLocalStorage.run(tags, async () => await scope());
|
|
9
|
+
}
|
|
10
|
+
function withMergedQueryTags(tags, scope) {
|
|
11
|
+
const existingTags = asyncLocalStorage.getStore() ?? {};
|
|
12
|
+
const mergedTags = { ...existingTags, ...tags };
|
|
13
|
+
return asyncLocalStorage.run(mergedTags, async () => await scope());
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
queryTags,
|
|
17
|
+
withMergedQueryTags,
|
|
18
|
+
withQueryTags
|
|
19
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma/sqlcommenter-query-tags",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "AsyncLocalStorage-based query tagging plugin for Prisma SQL commenter",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.mts",
|
|
16
|
+
"default": "./dist/index.mjs"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"homepage": "https://www.prisma.io",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/prisma/prisma.git",
|
|
25
|
+
"directory": "packages/sqlcommenter-query-tags"
|
|
26
|
+
},
|
|
27
|
+
"bugs": "https://github.com/prisma/prisma/issues",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"vitest": "3.2.4",
|
|
34
|
+
"@prisma/sqlcommenter": "0.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"dev": "DEV=true tsx helpers/build.ts",
|
|
38
|
+
"build": "tsx helpers/build.ts",
|
|
39
|
+
"test": "vitest run"
|
|
40
|
+
}
|
|
41
|
+
}
|