@prisma/sqlcommenter 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/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # @prisma/sqlcommenter
2
+
3
+ Type definitions for SQL commenter plugins in Prisma Client.
4
+
5
+ ## Overview
6
+
7
+ This package provides TypeScript types for creating SQL commenter plugins that add metadata to SQL queries as comments. The comments follow the [sqlcommenter format](https://google.github.io/sqlcommenter/) developed by Google.
8
+
9
+ SQL comments are useful for:
10
+
11
+ - **Observability**: Correlate database queries with application traces using `traceparent`
12
+ - **Query Insights**: Tag queries with metadata for analysis in database monitoring tools
13
+ - **Debugging**: Add custom context to queries for easier troubleshooting
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @prisma/sqlcommenter
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Creating a Plugin
24
+
25
+ A SQL commenter plugin is a function that receives query context and returns key-value pairs to be added as comments:
26
+
27
+ ```typescript
28
+ import type { SqlCommenterPlugin, SqlCommenterContext } from '@prisma/sqlcommenter'
29
+
30
+ const myPlugin: SqlCommenterPlugin = (context: SqlCommenterContext) => {
31
+ return {
32
+ application: 'my-app',
33
+ version: '1.0.0',
34
+ }
35
+ }
36
+ ```
37
+
38
+ ### Using Plugins with PrismaClient
39
+
40
+ Pass your plugins to the `comments` option when creating a PrismaClient instance:
41
+
42
+ ```typescript
43
+ import { PrismaClient } from '@prisma/client'
44
+ import { PrismaPg } from '@prisma/adapter-pg'
45
+
46
+ const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
47
+
48
+ const prisma = new PrismaClient({
49
+ adapter,
50
+ comments: [myPlugin],
51
+ })
52
+ ```
53
+
54
+ ### Query Context
55
+
56
+ Plugins receive a `SqlCommenterContext` object with information about the query being executed:
57
+
58
+ ```typescript
59
+ interface SqlCommenterContext {
60
+ query: SqlCommenterQueryInfo
61
+ }
62
+
63
+ type SqlCommenterQueryInfo =
64
+ | { type: 'single'; modelName?: string; action: string; query: unknown }
65
+ | { type: 'compacted'; queries: SqlCommenterSingleQueryInfo[] }
66
+ ```
67
+
68
+ - **`type: 'single'`**: A single Prisma query is being executed
69
+ - `modelName`: The model being queried (e.g., `"User"`, `"Post"`). Undefined for raw queries.
70
+ - `action`: The Prisma operation (e.g., `"findMany"`, `"createOne"`, `"queryRaw"`)
71
+ - `query`: The full query object with selection and arguments
72
+
73
+ - **`type: 'compacted'`**: Multiple queries have been batched into a single SQL statement (e.g., automatic `findUnique` batching or explicit `$transaction` batches)
74
+
75
+ ### Example: Custom Application Tags
76
+
77
+ ```typescript
78
+ import type { SqlCommenterPlugin } from '@prisma/sqlcommenter'
79
+
80
+ const applicationTags: SqlCommenterPlugin = (context) => {
81
+ const tags: Record<string, string> = {
82
+ application: 'my-service',
83
+ environment: process.env.NODE_ENV ?? 'development',
84
+ }
85
+
86
+ if (context.query.type === 'single' && context.query.modelName) {
87
+ tags.model = context.query.modelName
88
+ tags.operation = context.query.action
89
+ }
90
+
91
+ return tags
92
+ }
93
+ ```
94
+
95
+ ### Example: Async Context Propagation
96
+
97
+ ```typescript
98
+ import { AsyncLocalStorage } from 'node:async_hooks'
99
+ import type { SqlCommenterPlugin } from '@prisma/sqlcommenter'
100
+
101
+ const traceStorage = new AsyncLocalStorage<{ route: string }>()
102
+
103
+ const traceContext: SqlCommenterPlugin = () => {
104
+ const store = traceStorage.getStore()
105
+ if (store?.route) {
106
+ return { route: store.route }
107
+ }
108
+ return {}
109
+ }
110
+ ```
111
+
112
+ ## Output Format
113
+
114
+ The plugin outputs are merged, sorted by key, URL-encoded, and formatted according to the [sqlcommenter specification](https://google.github.io/sqlcommenter/spec/):
115
+
116
+ ```sql
117
+ SELECT "id", "name" FROM "User" /*application='my-app',environment='production',model='User'*/
118
+ ```
119
+
120
+ ## API Reference
121
+
122
+ ### `SqlCommenterPlugin`
123
+
124
+ ```typescript
125
+ interface SqlCommenterPlugin {
126
+ (context: SqlCommenterContext): Record<string, string>
127
+ }
128
+ ```
129
+
130
+ A function that receives query context and returns key-value pairs. Return an empty object to add no comments for a particular query.
131
+
132
+ ### `SqlCommenterContext`
133
+
134
+ ```typescript
135
+ interface SqlCommenterContext {
136
+ query: SqlCommenterQueryInfo
137
+ }
138
+ ```
139
+
140
+ Context provided to plugins containing information about the query.
141
+
142
+ ### `SqlCommenterQueryInfo`
143
+
144
+ ```typescript
145
+ type SqlCommenterQueryInfo =
146
+ | ({ type: 'single' } & SqlCommenterSingleQueryInfo)
147
+ | { type: 'compacted'; queries: SqlCommenterSingleQueryInfo[] }
148
+ ```
149
+
150
+ Information about the query or queries being executed.
151
+
152
+ ### `SqlCommenterSingleQueryInfo`
153
+
154
+ ```typescript
155
+ interface SqlCommenterSingleQueryInfo {
156
+ modelName?: string
157
+ action: string
158
+ query: unknown
159
+ }
160
+ ```
161
+
162
+ Information about a single Prisma query.
163
+
164
+ ## Notes
165
+
166
+ - Plugins are called synchronously in array order
167
+ - Later plugins override earlier ones if they return the same key
168
+ - Keys and values are URL-encoded per the sqlcommenter spec
169
+ - Single quotes in values are escaped as `\'`
170
+ - Comments are appended to the end of SQL queries
171
+
172
+ ## License
173
+
174
+ Apache-2.0
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Information about a compacted batch query (e.g. multiple independent
3
+ * `findUnique` queries automatically merged into a single `SELECT` SQL
4
+ * statement).
5
+ */
6
+ export declare interface SqlCommenterCompactedQueryInfo {
7
+ /**
8
+ * The model name (e.g., "User", "Post").
9
+ */
10
+ readonly modelName: string;
11
+ /**
12
+ * The Prisma operation (e.g., "findUnique").
13
+ */
14
+ readonly action: SqlCommenterQueryAction;
15
+ /**
16
+ * The full query objects (selections, arguments, etc.).
17
+ * Specifics of the query representation are not part of the public API yet.
18
+ */
19
+ readonly queries: ReadonlyArray<unknown>;
20
+ }
21
+
22
+ /**
23
+ * Context provided to SQL commenter plugins.
24
+ */
25
+ export declare interface SqlCommenterContext {
26
+ /**
27
+ * Information about the Prisma query being executed.
28
+ */
29
+ readonly query: SqlCommenterQueryInfo;
30
+ }
31
+
32
+ /**
33
+ * A SQL commenter plugin that returns key-value pairs to be added as comments.
34
+ * Return an empty object to add no comments.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const myPlugin: SqlCommenterPlugin = (context) => {
39
+ * return {
40
+ * application: 'my-app',
41
+ * model: context.query.modelName ?? 'raw',
42
+ * }
43
+ * }
44
+ * ```
45
+ */
46
+ export declare interface SqlCommenterPlugin {
47
+ (context: SqlCommenterContext): Record<string, string>;
48
+ }
49
+
50
+ /**
51
+ * Prisma query type corresponding to this SQL query.
52
+ */
53
+ export declare type SqlCommenterQueryAction = 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'createOne' | 'createMany' | 'createManyAndReturn' | 'updateOne' | 'updateMany' | 'updateManyAndReturn' | 'deleteOne' | 'deleteMany' | 'upsertOne' | 'aggregate' | 'groupBy' | 'executeRaw' | 'queryRaw' | 'runCommandRaw' | 'findRaw' | 'aggregateRaw';
54
+
55
+ /**
56
+ * Information about the query or queries being executed.
57
+ *
58
+ * - `single`: A single query is being executed
59
+ * - `compacted`: Multiple queries have been compacted into a single SQL statement
60
+ */
61
+ export declare type SqlCommenterQueryInfo = ({
62
+ readonly type: 'single';
63
+ } & SqlCommenterSingleQueryInfo) | ({
64
+ readonly type: 'compacted';
65
+ } & SqlCommenterCompactedQueryInfo);
66
+
67
+ /**
68
+ * Information about a single Prisma query.
69
+ */
70
+ export declare interface SqlCommenterSingleQueryInfo {
71
+ /**
72
+ * The model name (e.g., "User", "Post"). Undefined for raw queries.
73
+ */
74
+ readonly modelName?: string;
75
+ /**
76
+ * The Prisma operation (e.g., "findMany", "createOne", "queryRaw").
77
+ */
78
+ readonly action: SqlCommenterQueryAction;
79
+ /**
80
+ * The full query object (selection, arguments, etc.).
81
+ * Specifics of the query representation are not part of the public API yet.
82
+ */
83
+ readonly query: unknown;
84
+ }
85
+
86
+ export { }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Information about a compacted batch query (e.g. multiple independent
3
+ * `findUnique` queries automatically merged into a single `SELECT` SQL
4
+ * statement).
5
+ */
6
+ export declare interface SqlCommenterCompactedQueryInfo {
7
+ /**
8
+ * The model name (e.g., "User", "Post").
9
+ */
10
+ readonly modelName: string;
11
+ /**
12
+ * The Prisma operation (e.g., "findUnique").
13
+ */
14
+ readonly action: SqlCommenterQueryAction;
15
+ /**
16
+ * The full query objects (selections, arguments, etc.).
17
+ * Specifics of the query representation are not part of the public API yet.
18
+ */
19
+ readonly queries: ReadonlyArray<unknown>;
20
+ }
21
+
22
+ /**
23
+ * Context provided to SQL commenter plugins.
24
+ */
25
+ export declare interface SqlCommenterContext {
26
+ /**
27
+ * Information about the Prisma query being executed.
28
+ */
29
+ readonly query: SqlCommenterQueryInfo;
30
+ }
31
+
32
+ /**
33
+ * A SQL commenter plugin that returns key-value pairs to be added as comments.
34
+ * Return an empty object to add no comments.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const myPlugin: SqlCommenterPlugin = (context) => {
39
+ * return {
40
+ * application: 'my-app',
41
+ * model: context.query.modelName ?? 'raw',
42
+ * }
43
+ * }
44
+ * ```
45
+ */
46
+ export declare interface SqlCommenterPlugin {
47
+ (context: SqlCommenterContext): Record<string, string>;
48
+ }
49
+
50
+ /**
51
+ * Prisma query type corresponding to this SQL query.
52
+ */
53
+ export declare type SqlCommenterQueryAction = 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'createOne' | 'createMany' | 'createManyAndReturn' | 'updateOne' | 'updateMany' | 'updateManyAndReturn' | 'deleteOne' | 'deleteMany' | 'upsertOne' | 'aggregate' | 'groupBy' | 'executeRaw' | 'queryRaw' | 'runCommandRaw' | 'findRaw' | 'aggregateRaw';
54
+
55
+ /**
56
+ * Information about the query or queries being executed.
57
+ *
58
+ * - `single`: A single query is being executed
59
+ * - `compacted`: Multiple queries have been compacted into a single SQL statement
60
+ */
61
+ export declare type SqlCommenterQueryInfo = ({
62
+ readonly type: 'single';
63
+ } & SqlCommenterSingleQueryInfo) | ({
64
+ readonly type: 'compacted';
65
+ } & SqlCommenterCompactedQueryInfo);
66
+
67
+ /**
68
+ * Information about a single Prisma query.
69
+ */
70
+ export declare interface SqlCommenterSingleQueryInfo {
71
+ /**
72
+ * The model name (e.g., "User", "Post"). Undefined for raw queries.
73
+ */
74
+ readonly modelName?: string;
75
+ /**
76
+ * The Prisma operation (e.g., "findMany", "createOne", "queryRaw").
77
+ */
78
+ readonly action: SqlCommenterQueryAction;
79
+ /**
80
+ * The full query object (selection, arguments, etc.).
81
+ * Specifics of the query representation are not part of the public API yet.
82
+ */
83
+ readonly query: unknown;
84
+ }
85
+
86
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/index.ts
17
+ var index_exports = {};
18
+ module.exports = __toCommonJS(index_exports);
package/dist/index.mjs ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@prisma/sqlcommenter",
3
+ "version": "0.0.1",
4
+ "description": "SQL commenter types for Prisma",
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"
26
+ },
27
+ "bugs": "https://github.com/prisma/prisma/issues",
28
+ "scripts": {
29
+ "dev": "DEV=true tsx helpers/build.ts",
30
+ "build": "tsx helpers/build.ts",
31
+ "prepublishOnly": "pnpm run build"
32
+ },
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "sideEffects": false
37
+ }