@auditstream/prisma 0.1.0 → 0.2.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 +107 -0
- package/dist/index.d.mts +107 -2
- package/dist/index.mjs +86 -2
- package/package.json +13 -3
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @auditstream/prisma
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-badge]][npm-url]
|
|
4
|
+
[![CI][ci-badge]][ci-url]
|
|
5
|
+
[![SonarCloud Quality Gate][sonar-badge]][sonar-url]
|
|
6
|
+
[![OpenSSF Scorecard][scorecard-badge]][scorecard-url]
|
|
7
|
+
|
|
8
|
+
Prisma 7 SQL comments plugin and transaction-local `set_config` fallback for [Auditstream](https://auditstream.dev).
|
|
9
|
+
|
|
10
|
+
`@auditstream/prisma` reads the active actor from `@auditstream/node` and sends it to database capture triggers without altering your Prisma schema. Full guides and API reference live at **[auditstream.dev/docs/typescript/prisma](https://auditstream.dev/docs/typescript/prisma/)**.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @auditstream/prisma @auditstream/node @prisma/client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Install the `prisma` CLI as a dev dependency if needed. The `prisma` CLI is never a peer dependency.
|
|
19
|
+
|
|
20
|
+
> **Prerequisite:** Run `auditstream init` from the [`auditstream` CLI](https://auditstream.dev/docs/getting-started/) to create the audit schema and database capture triggers.
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
### SQL comments plugin (`auditstream`)
|
|
25
|
+
|
|
26
|
+
Register `auditstream()` in your Prisma 7 client `comments` array. Every write inside `audit.as()` receives one trailing `auditstream` tag:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { audit } from "@auditstream/node";
|
|
30
|
+
import { auditstream } from "@auditstream/prisma";
|
|
31
|
+
import { PrismaPg } from "@prisma/adapter-pg";
|
|
32
|
+
import { PrismaClient } from "./generated/prisma/client.js";
|
|
33
|
+
|
|
34
|
+
const adapter = new PrismaPg({ connectionString: "postgres://localhost/app" });
|
|
35
|
+
const prisma = new PrismaClient({
|
|
36
|
+
adapter,
|
|
37
|
+
comments: [auditstream()],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await audit.as({ type: "user", id: "usr_42" }, () =>
|
|
41
|
+
prisma.invoice.update({
|
|
42
|
+
where: { id: 1 },
|
|
43
|
+
data: { status: "paid" },
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Transaction fallback (`auditTransaction`)
|
|
49
|
+
|
|
50
|
+
Use `auditTransaction()` when SQL comments are unavailable, such as under Prisma Accelerate or proxies that strip comments. It sets `auditstream.actor` with `set_config(..., true)` inside the transaction:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { audit } from "@auditstream/node";
|
|
54
|
+
import { auditTransaction } from "@auditstream/prisma";
|
|
55
|
+
import { PrismaClient } from "./generated/prisma/client.js";
|
|
56
|
+
|
|
57
|
+
const prisma = new PrismaClient();
|
|
58
|
+
|
|
59
|
+
await audit.as({ type: "user", id: "usr_42" }, () =>
|
|
60
|
+
auditTransaction(
|
|
61
|
+
prisma,
|
|
62
|
+
async (tx) => {
|
|
63
|
+
await tx.invoice.update({
|
|
64
|
+
where: { id: 1 },
|
|
65
|
+
data: { status: "paid" },
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
{ timeout: 5000 },
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Supported versions
|
|
74
|
+
|
|
75
|
+
| Dependency | Supported versions |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| Node.js | `22.12` or newer (CI tests `22`, `24`, and `26`) |
|
|
78
|
+
| PostgreSQL | `15` to `19` |
|
|
79
|
+
| Prisma | `@prisma/client` `7.1` or newer within `7.x` (tested on `7.10.1`) |
|
|
80
|
+
| Runtimes | Node.js (reference), Bun and Deno (tested in CI), Cloudflare Workers (`nodejs_als` flag or compatibility date `2026-08-04` or later) |
|
|
81
|
+
|
|
82
|
+
Prisma 6 is not supported. Prisma 8 support follows its stable release. The `prisma` CLI is never a peer dependency.
|
|
83
|
+
|
|
84
|
+
## Context and limits
|
|
85
|
+
|
|
86
|
+
- **Unawaited lazy queries:** Prisma queries execute only when awaited. Always await them inside `audit.as()`, which awaits your callback before closing the scope.
|
|
87
|
+
- **No `statementNameGenerator`:** Never configure `PrismaPg`'s optional `statementNameGenerator` alongside SQL comments; per-actor comments with named prepared statements grow caches unboundedly.
|
|
88
|
+
- **Next.js `proxy`:** `AsyncLocalStorage` does not cross Next.js `proxy`. Pass actor data through headers, cookies, rewrites, redirects, or URLs, and wrap handlers with `@auditstream/node/next`.
|
|
89
|
+
- **Background jobs:** Code running outside a request has no active actor. Open a scope with a `service` or `system` actor.
|
|
90
|
+
- **Prisma Accelerate:** Comment plugins do not receive the SQL under Accelerate. Use `auditTransaction()` there instead.
|
|
91
|
+
- **AWS RDS Proxy:** Trailing comments are the default transport because `set_config(..., true)` pins RDS Proxy sessions.
|
|
92
|
+
|
|
93
|
+
## Links
|
|
94
|
+
|
|
95
|
+
- [Prisma guide (auditstream.dev)](https://auditstream.dev/docs/typescript/prisma/)
|
|
96
|
+
- [Monorepo (auditstream-js)](https://github.com/auditstream/auditstream-js)
|
|
97
|
+
- [Rust Engine & CLI](https://github.com/auditstream/auditstream)
|
|
98
|
+
- [Security policy](https://github.com/auditstream/auditstream-js/security/policy)
|
|
99
|
+
|
|
100
|
+
[npm-badge]: https://img.shields.io/npm/v/%40auditstream%2Fprisma?style=flat&label=%40auditstream%2Fprisma
|
|
101
|
+
[npm-url]: https://www.npmjs.com/package/@auditstream/prisma
|
|
102
|
+
[ci-badge]: https://img.shields.io/github/actions/workflow/status/auditstream/auditstream-js/ci.yml?branch=main&style=flat&label=CI
|
|
103
|
+
[ci-url]: https://github.com/auditstream/auditstream-js/actions/workflows/ci.yml
|
|
104
|
+
[sonar-badge]: https://img.shields.io/sonar/quality_gate/auditstream_auditstream-js?server=https%3A%2F%2Fsonarcloud.io&style=flat&label=SonarCloud
|
|
105
|
+
[sonar-url]: https://sonarcloud.io/project/overview?id=auditstream_auditstream-js
|
|
106
|
+
[scorecard-badge]: https://img.shields.io/ossf-scorecard/github.com/auditstream/auditstream-js?style=flat&label=OpenSSF%20Scorecard
|
|
107
|
+
[scorecard-url]: https://scorecard.dev/viewer/?uri=github.com/auditstream/auditstream-js
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,107 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Interactive transaction client capable of executing parameterized tagged SQL via `$executeRaw`.
|
|
3
|
+
*/
|
|
4
|
+
interface PrismaRawExecutor {
|
|
5
|
+
/** Executes a parameterized SQL template string on the active transaction connection. */
|
|
6
|
+
$executeRaw(query: TemplateStringsArray, ...values: unknown[]): Promise<number>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Structural shape of a Prisma client supporting interactive `$transaction(fn, options?)`.
|
|
10
|
+
*/
|
|
11
|
+
interface PrismaTransactionRunner<TTx extends PrismaRawExecutor, TOptions = unknown> {
|
|
12
|
+
/** Runs `fn` inside an interactive database transaction with optional Prisma transaction options. */
|
|
13
|
+
$transaction<TResult>(fn: (tx: TTx) => Promise<TResult>, options?: TOptions): Promise<TResult>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Runs `fn` inside a Prisma interactive transaction (`prisma.$transaction`) and, when an
|
|
17
|
+
* `audit.as()` scope is active at call time, executes
|
|
18
|
+
* `SELECT set_config('auditstream.actor', $1, true)` as the transaction's first statement
|
|
19
|
+
* using a bound parameter from `setConfigValue()`.
|
|
20
|
+
*
|
|
21
|
+
* - **When to use:** Use `auditTransaction()` when SQL comments cannot reach PostgreSQL
|
|
22
|
+
* (notably under Prisma Accelerate, where comment plugins do not receive SQL and comment
|
|
23
|
+
* delivery is undocumented).
|
|
24
|
+
* - **Precedence & isolation:** `set_config('auditstream.actor', $1, true)` is transaction-local
|
|
25
|
+
* (`is_local = true`), wins over any SQL comment on the same statement, and resets to an empty
|
|
26
|
+
* string once the transaction commits or rolls back so actors cannot leak across pooled
|
|
27
|
+
* connections.
|
|
28
|
+
* - **RDS Proxy note:** `set_config` pins connections on AWS RDS Proxy; keep the `auditstream()`
|
|
29
|
+
* SQL comments plugin as the default transport there.
|
|
30
|
+
* - **Outside a scope:** When `audit.current()` is `undefined`, `auditTransaction()` runs `fn`
|
|
31
|
+
* inside `prisma.$transaction` without issuing `set_config`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { audit } from "@auditstream/node";
|
|
36
|
+
* import { auditTransaction } from "@auditstream/prisma";
|
|
37
|
+
*
|
|
38
|
+
* const updated = await audit.as({ type: "user", id: "usr_1" }, () =>
|
|
39
|
+
* auditTransaction(
|
|
40
|
+
* prisma,
|
|
41
|
+
* async (tx) => tx.invoice.update({ where: { id: 1 }, data: { status: "paid" } }),
|
|
42
|
+
* { timeout: 10_000 },
|
|
43
|
+
* ),
|
|
44
|
+
* );
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function auditTransaction<TTx extends PrismaRawExecutor, TResult, TOptions = unknown>(prisma: PrismaTransactionRunner<TTx, TOptions>, fn: (tx: TTx) => Promise<TResult>, options?: TOptions): Promise<TResult>;
|
|
48
|
+
/** Package identifier exported by `@auditstream/prisma`. */
|
|
49
|
+
export declare const PACKAGE_NAME: "@auditstream/prisma";
|
|
50
|
+
/**
|
|
51
|
+
* Key-value map returned by a Prisma SQL comments plugin.
|
|
52
|
+
* Keys with an `undefined` value are omitted by Prisma when building the trailing comment.
|
|
53
|
+
*/
|
|
54
|
+
export type SqlCommenterTags = {
|
|
55
|
+
readonly [key: string]: string | undefined;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Query metadata passed by Prisma to SQL comments plugins.
|
|
59
|
+
*
|
|
60
|
+
* Declared structurally so `@auditstream/prisma` carries no dependency on `@prisma/sqlcommenter`
|
|
61
|
+
* and stays assignable across `@prisma/client` 7.1.0 through 7.x.
|
|
62
|
+
*/
|
|
63
|
+
export interface SqlCommenterContext {
|
|
64
|
+
/** Metadata about the single or compacted Prisma query being executed. */
|
|
65
|
+
readonly query?: unknown;
|
|
66
|
+
/** Rendered SQL statement when available (omitted under Prisma Accelerate). */
|
|
67
|
+
readonly sql?: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Prisma 7 SQL comments plugin compatible with `new PrismaClient({ comments: [auditstream()] })`.
|
|
71
|
+
*/
|
|
72
|
+
export type SqlCommenterPlugin = (context?: SqlCommenterContext) => SqlCommenterTags;
|
|
73
|
+
/**
|
|
74
|
+
* Creates a Prisma 7 SQL comments plugin that attaches the active `audit.as()` actor and
|
|
75
|
+
* context as an `auditstream` sqlcommenter tag.
|
|
76
|
+
*
|
|
77
|
+
* - **Supported versions:** Requires `@prisma/client` 7.1.0 or newer within Prisma 7.x
|
|
78
|
+
* (`>=7.1.0 <8`). Prisma 6 is not supported.
|
|
79
|
+
* - **Empty outside scope:** Outside an active `audit.as()` scope, the plugin returns `{}`
|
|
80
|
+
* so no `auditstream` key is emitted.
|
|
81
|
+
* - **Prisma Accelerate limit:** Under Prisma Accelerate, SQL comments plugins do not receive
|
|
82
|
+
* rendered SQL and comment delivery to PostgreSQL is undocumented. Use
|
|
83
|
+
* {@link auditTransaction} (`set_config` fallback) for Accelerate workloads.
|
|
84
|
+
* - **`statementNameGenerator` rule:** Never configure `PrismaPg`'s optional
|
|
85
|
+
* `statementNameGenerator` alongside SQL comments. Per-actor comments paired with named
|
|
86
|
+
* prepared statements grow `node-postgres`'s prepared-statement cache unboundedly and
|
|
87
|
+
* violate `spec/transport.md`.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { PrismaPg } from "@prisma/adapter-pg";
|
|
92
|
+
* import { audit } from "@auditstream/node";
|
|
93
|
+
* import { auditstream } from "@auditstream/prisma";
|
|
94
|
+
* import { PrismaClient } from "./generated/prisma/client";
|
|
95
|
+
*
|
|
96
|
+
* const prisma = new PrismaClient({
|
|
97
|
+
* adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),
|
|
98
|
+
* comments: [auditstream()],
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* await audit.as({ type: "user", id: "usr_1" }, () =>
|
|
102
|
+
* prisma.invoice.update({ where: { id: 1 }, data: { status: "paid" } }),
|
|
103
|
+
* );
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare function auditstream(): SqlCommenterPlugin;
|
|
107
|
+
export type { PrismaRawExecutor, PrismaTransactionRunner };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,88 @@
|
|
|
1
|
+
import { audit, encodeScope, setConfigValue } from "@auditstream/node";
|
|
2
|
+
// src/fallback.ts
|
|
3
|
+
/**
|
|
4
|
+
* Runs `fn` inside a Prisma interactive transaction (`prisma.$transaction`) and, when an
|
|
5
|
+
* `audit.as()` scope is active at call time, executes
|
|
6
|
+
* `SELECT set_config('auditstream.actor', $1, true)` as the transaction's first statement
|
|
7
|
+
* using a bound parameter from `setConfigValue()`.
|
|
8
|
+
*
|
|
9
|
+
* - **When to use:** Use `auditTransaction()` when SQL comments cannot reach PostgreSQL
|
|
10
|
+
* (notably under Prisma Accelerate, where comment plugins do not receive SQL and comment
|
|
11
|
+
* delivery is undocumented).
|
|
12
|
+
* - **Precedence & isolation:** `set_config('auditstream.actor', $1, true)` is transaction-local
|
|
13
|
+
* (`is_local = true`), wins over any SQL comment on the same statement, and resets to an empty
|
|
14
|
+
* string once the transaction commits or rolls back so actors cannot leak across pooled
|
|
15
|
+
* connections.
|
|
16
|
+
* - **RDS Proxy note:** `set_config` pins connections on AWS RDS Proxy; keep the `auditstream()`
|
|
17
|
+
* SQL comments plugin as the default transport there.
|
|
18
|
+
* - **Outside a scope:** When `audit.current()` is `undefined`, `auditTransaction()` runs `fn`
|
|
19
|
+
* inside `prisma.$transaction` without issuing `set_config`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { audit } from "@auditstream/node";
|
|
24
|
+
* import { auditTransaction } from "@auditstream/prisma";
|
|
25
|
+
*
|
|
26
|
+
* const updated = await audit.as({ type: "user", id: "usr_1" }, () =>
|
|
27
|
+
* auditTransaction(
|
|
28
|
+
* prisma,
|
|
29
|
+
* async (tx) => tx.invoice.update({ where: { id: 1 }, data: { status: "paid" } }),
|
|
30
|
+
* { timeout: 10_000 },
|
|
31
|
+
* ),
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async function auditTransaction(prisma, fn, options) {
|
|
36
|
+
const scope = audit.current();
|
|
37
|
+
const payload = scope ? setConfigValue(scope) : void 0;
|
|
38
|
+
const callback = async (tx) => {
|
|
39
|
+
if (payload !== void 0) await tx.$executeRaw`SELECT set_config('auditstream.actor', ${payload}, true)`;
|
|
40
|
+
return await fn(tx);
|
|
41
|
+
};
|
|
42
|
+
if (options !== void 0) return await prisma.$transaction(callback, options);
|
|
43
|
+
return await prisma.$transaction(callback);
|
|
44
|
+
}
|
|
1
45
|
// src/index.ts
|
|
2
|
-
/** Package identifier exported by
|
|
46
|
+
/** Package identifier exported by `@auditstream/prisma`. */
|
|
3
47
|
const PACKAGE_NAME = "@auditstream/prisma";
|
|
4
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Creates a Prisma 7 SQL comments plugin that attaches the active `audit.as()` actor and
|
|
50
|
+
* context as an `auditstream` sqlcommenter tag.
|
|
51
|
+
*
|
|
52
|
+
* - **Supported versions:** Requires `@prisma/client` 7.1.0 or newer within Prisma 7.x
|
|
53
|
+
* (`>=7.1.0 <8`). Prisma 6 is not supported.
|
|
54
|
+
* - **Empty outside scope:** Outside an active `audit.as()` scope, the plugin returns `{}`
|
|
55
|
+
* so no `auditstream` key is emitted.
|
|
56
|
+
* - **Prisma Accelerate limit:** Under Prisma Accelerate, SQL comments plugins do not receive
|
|
57
|
+
* rendered SQL and comment delivery to PostgreSQL is undocumented. Use
|
|
58
|
+
* {@link auditTransaction} (`set_config` fallback) for Accelerate workloads.
|
|
59
|
+
* - **`statementNameGenerator` rule:** Never configure `PrismaPg`'s optional
|
|
60
|
+
* `statementNameGenerator` alongside SQL comments. Per-actor comments paired with named
|
|
61
|
+
* prepared statements grow `node-postgres`'s prepared-statement cache unboundedly and
|
|
62
|
+
* violate `spec/transport.md`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { PrismaPg } from "@prisma/adapter-pg";
|
|
67
|
+
* import { audit } from "@auditstream/node";
|
|
68
|
+
* import { auditstream } from "@auditstream/prisma";
|
|
69
|
+
* import { PrismaClient } from "./generated/prisma/client";
|
|
70
|
+
*
|
|
71
|
+
* const prisma = new PrismaClient({
|
|
72
|
+
* adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),
|
|
73
|
+
* comments: [auditstream()],
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* await audit.as({ type: "user", id: "usr_1" }, () =>
|
|
77
|
+
* prisma.invoice.update({ where: { id: 1 }, data: { status: "paid" } }),
|
|
78
|
+
* );
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function auditstream() {
|
|
82
|
+
return () => {
|
|
83
|
+
const scope = audit.current();
|
|
84
|
+
if (!scope) return {};
|
|
85
|
+
return { auditstream: encodeScope(scope) };
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export { PACKAGE_NAME, auditTransaction, auditstream };
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auditstream/prisma",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Prisma 7 SQL comments plugin and set_config fallback for Auditstream",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"homepage": "https://auditstream.dev/docs/typescript/prisma/",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
10
|
"url": "https://github.com/auditstream/auditstream-js.git",
|
|
@@ -30,9 +31,17 @@
|
|
|
30
31
|
"@prisma/client": ">=7.1.0 <8"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
|
-
"@auditstream/node": "0.1
|
|
34
|
+
"@auditstream/node": "0.2.1",
|
|
35
|
+
"@auditstream/test-utils": "0.1.0",
|
|
36
|
+
"@neondatabase/serverless": "1.2.1",
|
|
37
|
+
"@prisma/adapter-neon": "7.10.1",
|
|
38
|
+
"@prisma/adapter-pg": "7.10.1",
|
|
34
39
|
"@prisma/client": "7.10.1",
|
|
40
|
+
"@prisma/sqlcommenter": "7.10.1",
|
|
35
41
|
"@types/node": "^22.20.5",
|
|
42
|
+
"@types/pg": "8.23.1",
|
|
43
|
+
"pg": "8.23.1",
|
|
44
|
+
"prisma": "7.10.1",
|
|
36
45
|
"tsdown": "0.23.7",
|
|
37
46
|
"typescript": "~6.0.3",
|
|
38
47
|
"vitest": "^5.1.1"
|
|
@@ -40,6 +49,7 @@
|
|
|
40
49
|
"scripts": {
|
|
41
50
|
"build": "tsdown",
|
|
42
51
|
"typecheck": "tsc --noEmit",
|
|
43
|
-
"test": "vitest run --config ../../vitest.config.ts"
|
|
52
|
+
"test": "vitest run --config ../../vitest.config.ts",
|
|
53
|
+
"test:integration": "prisma generate --config test/integration/prisma.config.ts && vitest run --config vitest.integration.config.ts"
|
|
44
54
|
}
|
|
45
55
|
}
|