@stacknext/prisma 0.0.0-stage → 0.6.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 StackNext contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,22 @@
1
- # Temporary Holding Version
1
+ # @stacknext/prisma
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Prisma Client query instrumentation for [StackNext](https://github.com/marrrkkk/stacknext).
4
+
5
+ ```ts
6
+ import { PrismaClient } from '@prisma/client'
7
+ import { instrumentPrismaClient } from '@stacknext/prisma'
8
+
9
+ const prisma = new PrismaClient({
10
+ log: [{ emit: 'event', level: 'query' }],
11
+ })
12
+
13
+ const stop = instrumentPrismaClient(prisma, { databaseSystem: 'postgresql' })
14
+ ```
15
+
16
+ Query text is redacted and bind parameters are never read. Registration is
17
+ idempotent for the same client and returns a cleanup function.
18
+
19
+ The package is ESM-only and requires Node.js 20.9 or newer. `@prisma/client` is
20
+ an optional peer dependency.
21
+
22
+ License: MIT.
@@ -0,0 +1,34 @@
1
+ import { type StackNextTransport } from '@stacknext/core/transport';
2
+ /** Structural subset of Prisma's `$on('query')` event. */
3
+ export interface PrismaQueryEventLike {
4
+ timestamp: Date;
5
+ query: string;
6
+ /** Prisma passes bind parameters as a JSON string. They are never read. */
7
+ params: string;
8
+ duration: number;
9
+ target: string;
10
+ }
11
+ /** Structural subset of PrismaClient needed for instrumentation. */
12
+ export interface PrismaClientLike {
13
+ $on(event: 'query', callback: (event: PrismaQueryEventLike) => void): void;
14
+ }
15
+ export interface StackNextPrismaOptions {
16
+ /** Database engine reported to StackNext, for example "postgresql". */
17
+ databaseSystem?: string;
18
+ /** Override the process-wide transport (mostly for tests). */
19
+ transport?: Pick<StackNextTransport, 'enqueueDatabaseQuery'>;
20
+ onError?: (error: unknown) => void;
21
+ }
22
+ /**
23
+ * Subscribes to Prisma query events:
24
+ *
25
+ * ```ts
26
+ * const prisma = new PrismaClient()
27
+ * instrumentPrismaClient(prisma, { databaseSystem: 'sqlite' })
28
+ * ```
29
+ *
30
+ * Query text is redacted; bind parameters are never read or transmitted.
31
+ * Returns a cleanup function and is idempotent for the same client instance.
32
+ */
33
+ export declare function instrumentPrismaClient(client: PrismaClientLike, options?: StackNextPrismaOptions): () => void;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,2BAA2B,CAAA;AAElC,0DAA0D;AAC1D,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,IAAI,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,2EAA2E;IAC3E,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,oEAAoE;AACpE,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,GAAG,IAAI,CAAA;CAC3E;AAED,MAAM,WAAW,sBAAsB;IACrC,uEAAuE;IACvE,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,8DAA8D;IAC9D,SAAS,CAAC,EAAE,IAAI,CAAC,kBAAkB,EAAE,sBAAsB,CAAC,CAAA;IAC5D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;CACnC;AAWD;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,gBAAgB,EACxB,OAAO,GAAE,sBAA2B,GACnC,MAAM,IAAI,CAkDZ"}
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { isSpanContextValid, trace } from '@opentelemetry/api';
3
+ import { redactSql } from '@stacknext/core';
4
+ import { getStackNextTransport, } from '@stacknext/core/transport';
5
+ const instrumented = new WeakSet();
6
+ const OPERATION_PATTERN = /^\s*(select|insert|update|delete|with|begin|commit|rollback)\b/i;
7
+ function inferOperation(sql) {
8
+ return OPERATION_PATTERN.exec(sql)?.[1]?.toLowerCase();
9
+ }
10
+ /**
11
+ * Subscribes to Prisma query events:
12
+ *
13
+ * ```ts
14
+ * const prisma = new PrismaClient()
15
+ * instrumentPrismaClient(prisma, { databaseSystem: 'sqlite' })
16
+ * ```
17
+ *
18
+ * Query text is redacted; bind parameters are never read or transmitted.
19
+ * Returns a cleanup function and is idempotent for the same client instance.
20
+ */
21
+ export function instrumentPrismaClient(client, options = {}) {
22
+ if (instrumented.has(client))
23
+ return () => undefined;
24
+ instrumented.add(client);
25
+ const databaseSystem = options.databaseSystem ?? 'prisma';
26
+ let disposed = false;
27
+ const listener = (event) => {
28
+ if (disposed)
29
+ return;
30
+ try {
31
+ const transport = options.transport ?? getStackNextTransport();
32
+ if (!transport)
33
+ return;
34
+ const activeSpan = trace.getActiveSpan();
35
+ const spanContext = activeSpan?.spanContext();
36
+ const operation = inferOperation(event.query);
37
+ const duration = typeof event.duration === 'number' && Number.isFinite(event.duration)
38
+ ? Math.max(0, event.duration)
39
+ : null;
40
+ const input = {
41
+ id: randomUUID(),
42
+ ...(spanContext && isSpanContextValid(spanContext)
43
+ ? { traceId: spanContext.traceId, spanId: spanContext.spanId }
44
+ : {}),
45
+ timestamp: event.timestamp instanceof Date && Number.isFinite(event.timestamp.getTime())
46
+ ? event.timestamp.getTime()
47
+ : Date.now(),
48
+ durationMs: duration,
49
+ sql: redactSql(event.query).slice(0, 20_000),
50
+ databaseSystem,
51
+ ...(operation ? { operation } : {}),
52
+ success: true,
53
+ source: 'prisma',
54
+ attributes: {
55
+ 'db.system.name': databaseSystem,
56
+ 'db.duration.measured': duration !== null,
57
+ ...(event.target ? { 'db.prisma.target': event.target.slice(0, 200) } : {}),
58
+ },
59
+ };
60
+ transport.enqueueDatabaseQuery(input);
61
+ }
62
+ catch (error) {
63
+ options.onError?.(error);
64
+ }
65
+ };
66
+ client.$on('query', listener);
67
+ return () => {
68
+ disposed = true;
69
+ instrumented.delete(client);
70
+ };
71
+ }
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC9D,OAAO,EAAE,SAAS,EAAoC,MAAM,iBAAiB,CAAA;AAC7E,OAAO,EACL,qBAAqB,GAEtB,MAAM,2BAA2B,CAAA;AAyBlC,MAAM,YAAY,GAAG,IAAI,O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,MAAM,CAAC,MAAM,CAAC,CAAA;IAC7B,CAAC,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@stacknext/prisma",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.6.0",
4
+ "description": "Prisma Client query instrumentation adapter for StackNext",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "dependencies": {
21
+ "@stacknext/core": "0.6.0",
22
+ "@opentelemetry/api": "1.9.0"
23
+ },
24
+ "devDependencies": {
25
+ "@opentelemetry/sdk-trace-node": "2.12.0"
26
+ },
27
+ "engines": {
28
+ "node": ">=20.9.0"
29
+ },
30
+ "author": "StackNext contributors",
31
+ "homepage": "https://github.com/marrrkkk/stacknext#readme",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/marrrkkk/stacknext.git",
35
+ "directory": "packages/prisma"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/marrrkkk/stacknext/issues"
39
+ },
40
+ "keywords": [
41
+ "stacknext",
42
+ "nextjs",
43
+ "prisma",
44
+ "sql",
45
+ "observability"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "peerDependencies": {
51
+ "@prisma/client": ">=5.0.0"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "@prisma/client": {
55
+ "optional": true
56
+ }
57
+ },
58
+ "scripts": {
59
+ "build": "tsc -p tsconfig.build.json",
60
+ "typecheck": "tsc -p tsconfig.json --noEmit"
61
+ }
6
62
  }