ezlogs-nextjs 0.3.1 → 0.4.2
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 +149 -30
- package/bin/install-triggers.cjs +7 -2
- package/dist/actors/index.cjs +4 -4
- package/dist/actors/index.d.cts +1 -1
- package/dist/actors/index.d.ts +1 -1
- package/dist/actors/index.js +1 -1
- package/dist/{chunk-C5OSM557.js → chunk-FCFVIRQF.js} +7 -0
- package/dist/chunk-GI6BM4MR.js +443 -0
- package/dist/{chunk-YX2XPLOA.cjs → chunk-GMHOUAKD.cjs} +63 -34
- package/dist/{chunk-CU45I2WR.js → chunk-MHBS5FEP.js} +48 -20
- package/dist/chunk-MMLQXSSD.cjs +461 -0
- package/dist/{chunk-CE3R7DVB.js → chunk-NINIAL7M.js} +60 -384
- package/dist/{chunk-Y5NSHKWQ.cjs → chunk-NX4EL7BY.cjs} +57 -7
- package/dist/chunk-PS4U2GFJ.cjs +52 -0
- package/dist/chunk-R6IWVTX3.cjs +212 -0
- package/dist/{chunk-V5YNKKTG.cjs → chunk-TWTG5ILJ.cjs} +7 -0
- package/dist/chunk-U22T7QYY.js +200 -0
- package/dist/chunk-YMIGZYFH.js +48 -0
- package/dist/{chunk-WASXTDLW.cjs → chunk-YXZZNZG6.cjs} +71 -403
- package/dist/{chunk-DKRNNI4I.js → chunk-Z2TM3LGG.js} +53 -3
- package/dist/{configuration-OHLNivON.d.ts → configuration-CJ5yKVlP.d.cts} +14 -8
- package/dist/{configuration-OHLNivON.d.cts → configuration-CJ5yKVlP.d.ts} +14 -8
- package/dist/db/drizzle.cjs +5 -4
- package/dist/db/drizzle.js +3 -2
- package/dist/db/postgres-js.cjs +98 -0
- package/dist/db/postgres-js.d.cts +11 -0
- package/dist/db/postgres-js.d.ts +11 -0
- package/dist/db/postgres-js.js +95 -0
- package/dist/db/prisma.cjs +21 -20
- package/dist/db/prisma.d.cts +19 -2
- package/dist/db/prisma.d.ts +19 -2
- package/dist/db/prisma.js +6 -5
- package/dist/db/supabase.cjs +19 -12
- package/dist/db/supabase.d.cts +3 -2
- package/dist/db/supabase.d.ts +3 -2
- package/dist/db/supabase.js +11 -4
- package/dist/index.cjs +596 -165
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +492 -62
- package/dist/jobs/bullmq.cjs +63 -18
- package/dist/jobs/bullmq.d.cts +0 -4
- package/dist/jobs/bullmq.d.ts +0 -4
- package/dist/jobs/bullmq.js +59 -14
- package/dist/jobs/inngest.cjs +61 -24
- package/dist/jobs/inngest.d.cts +5 -1
- package/dist/jobs/inngest.d.ts +5 -1
- package/dist/jobs/inngest.js +55 -18
- package/dist/jobs/supabase-queues.cjs +17 -15
- package/dist/jobs/supabase-queues.d.cts +1 -1
- package/dist/jobs/supabase-queues.d.ts +1 -1
- package/dist/jobs/supabase-queues.js +15 -13
- package/dist/jobs/trigger.cjs +11 -12
- package/dist/jobs/trigger.d.cts +4 -3
- package/dist/jobs/trigger.d.ts +4 -3
- package/dist/jobs/trigger.js +7 -8
- package/dist/jobs/upstash-workflow.cjs +43 -35
- package/dist/jobs/upstash-workflow.d.cts +2 -8
- package/dist/jobs/upstash-workflow.d.ts +2 -8
- package/dist/jobs/upstash-workflow.js +38 -30
- package/dist/nextjs-plugin/index.cjs +39 -3
- package/dist/nextjs-plugin/index.d.cts +8 -1
- package/dist/nextjs-plugin/index.d.ts +8 -1
- package/dist/nextjs-plugin/index.js +39 -3
- package/dist/nextjs-plugin/loader.cjs +41 -3
- package/dist/nextjs-plugin/loader.d.cts +1 -0
- package/dist/nextjs-plugin/loader.d.ts +1 -0
- package/dist/nextjs-plugin/loader.js +42 -4
- package/dist/triggers/index.cjs +145 -92
- package/dist/triggers/index.d.cts +20 -5
- package/dist/triggers/index.d.ts +20 -5
- package/dist/triggers/index.js +125 -82
- package/package.json +11 -1
- package/sql/postgres/00_install.sql +1 -1
- package/dist/chunk-UQHEW6ER.js +0 -119
- package/dist/chunk-UWHYJH4M.cjs +0 -126
|
@@ -25,7 +25,7 @@ interface ActorContext {
|
|
|
25
25
|
/** Stable identifier for the user (e.g. `user.id`). REQUIRED. */
|
|
26
26
|
id: string;
|
|
27
27
|
/** Human-readable label (e.g. `user.email`). Optional. */
|
|
28
|
-
label?: string;
|
|
28
|
+
label?: string | undefined;
|
|
29
29
|
/** Category of actor. Optional — the server stamps `human` when
|
|
30
30
|
* absent. Auto-detection from the User-Agent fills this when the
|
|
31
31
|
* request looks like it came from an LLM client. */
|
|
@@ -49,15 +49,20 @@ interface ActorPrincipal {
|
|
|
49
49
|
label?: string;
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
52
|
+
* What `actorFromRequest` receives: the route's `Request` (App Router),
|
|
53
|
+
* or a request-like object whose `headers` is a Web `Headers` (Server
|
|
54
|
+
* Actions, Pages Router). Read headers only. `Request` and
|
|
55
|
+
* `NextRequest` both satisfy it.
|
|
56
|
+
*/
|
|
57
|
+
interface ActorRequest {
|
|
58
|
+
readonly headers: Headers;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
56
61
|
* The hook may return a value or a Promise — Phase 4 actor extractors
|
|
57
62
|
* (Clerk, NextAuth, Auth.js) are async because they call into the
|
|
58
63
|
* framework's auth helpers, which lazy-import the SDK.
|
|
59
64
|
*/
|
|
60
|
-
type ActorFromRequest = (request:
|
|
65
|
+
type ActorFromRequest = (request: ActorRequest) => ActorContext | null | undefined | Promise<ActorContext | null | undefined>;
|
|
61
66
|
/**
|
|
62
67
|
* Resolver for a display name. Either:
|
|
63
68
|
* - a column name on the row (e.g. "name"), or
|
|
@@ -69,7 +74,7 @@ type ActorFromRequest = (request: unknown) => ActorContext | null | undefined |
|
|
|
69
74
|
* try/catch — a thrown resolver collapses to "no display name" so a
|
|
70
75
|
* buggy resolver can never crash the host application.
|
|
71
76
|
*/
|
|
72
|
-
type DisplayNameResolver = string | ((row: Record<string, unknown>) =>
|
|
77
|
+
type DisplayNameResolver = string | ((row: Record<string, unknown>) => unknown);
|
|
73
78
|
/**
|
|
74
79
|
* Maps a model class name → resolver used to derive the human-readable
|
|
75
80
|
* display value, mirroring Ruby's `display_name_for`.
|
|
@@ -97,7 +102,8 @@ type ServerActionErrorClassifier = (result: unknown) => boolean | {
|
|
|
97
102
|
errorMessage: string;
|
|
98
103
|
} | null | undefined;
|
|
99
104
|
interface InitOptions {
|
|
100
|
-
|
|
105
|
+
/** Default: `process.env.EZLOGS_SERVER_URL`, else `"https://app.ezlogs.io"`. Set only if you self-host. */
|
|
106
|
+
serverUrl?: string;
|
|
101
107
|
projectToken: string;
|
|
102
108
|
captureHttp?: boolean;
|
|
103
109
|
captureDb?: boolean;
|
|
@@ -25,7 +25,7 @@ interface ActorContext {
|
|
|
25
25
|
/** Stable identifier for the user (e.g. `user.id`). REQUIRED. */
|
|
26
26
|
id: string;
|
|
27
27
|
/** Human-readable label (e.g. `user.email`). Optional. */
|
|
28
|
-
label?: string;
|
|
28
|
+
label?: string | undefined;
|
|
29
29
|
/** Category of actor. Optional — the server stamps `human` when
|
|
30
30
|
* absent. Auto-detection from the User-Agent fills this when the
|
|
31
31
|
* request looks like it came from an LLM client. */
|
|
@@ -49,15 +49,20 @@ interface ActorPrincipal {
|
|
|
49
49
|
label?: string;
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
52
|
+
* What `actorFromRequest` receives: the route's `Request` (App Router),
|
|
53
|
+
* or a request-like object whose `headers` is a Web `Headers` (Server
|
|
54
|
+
* Actions, Pages Router). Read headers only. `Request` and
|
|
55
|
+
* `NextRequest` both satisfy it.
|
|
56
|
+
*/
|
|
57
|
+
interface ActorRequest {
|
|
58
|
+
readonly headers: Headers;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
56
61
|
* The hook may return a value or a Promise — Phase 4 actor extractors
|
|
57
62
|
* (Clerk, NextAuth, Auth.js) are async because they call into the
|
|
58
63
|
* framework's auth helpers, which lazy-import the SDK.
|
|
59
64
|
*/
|
|
60
|
-
type ActorFromRequest = (request:
|
|
65
|
+
type ActorFromRequest = (request: ActorRequest) => ActorContext | null | undefined | Promise<ActorContext | null | undefined>;
|
|
61
66
|
/**
|
|
62
67
|
* Resolver for a display name. Either:
|
|
63
68
|
* - a column name on the row (e.g. "name"), or
|
|
@@ -69,7 +74,7 @@ type ActorFromRequest = (request: unknown) => ActorContext | null | undefined |
|
|
|
69
74
|
* try/catch — a thrown resolver collapses to "no display name" so a
|
|
70
75
|
* buggy resolver can never crash the host application.
|
|
71
76
|
*/
|
|
72
|
-
type DisplayNameResolver = string | ((row: Record<string, unknown>) =>
|
|
77
|
+
type DisplayNameResolver = string | ((row: Record<string, unknown>) => unknown);
|
|
73
78
|
/**
|
|
74
79
|
* Maps a model class name → resolver used to derive the human-readable
|
|
75
80
|
* display value, mirroring Ruby's `display_name_for`.
|
|
@@ -97,7 +102,8 @@ type ServerActionErrorClassifier = (result: unknown) => boolean | {
|
|
|
97
102
|
errorMessage: string;
|
|
98
103
|
} | null | undefined;
|
|
99
104
|
interface InitOptions {
|
|
100
|
-
|
|
105
|
+
/** Default: `process.env.EZLOGS_SERVER_URL`, else `"https://app.ezlogs.io"`. Set only if you self-host. */
|
|
106
|
+
serverUrl?: string;
|
|
101
107
|
projectToken: string;
|
|
102
108
|
captureHttp?: boolean;
|
|
103
109
|
captureDb?: boolean;
|
package/dist/db/drizzle.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
3
|
+
var chunkYXZZNZG6_cjs = require('../chunk-YXZZNZG6.cjs');
|
|
4
|
+
require('../chunk-MMLQXSSD.cjs');
|
|
5
|
+
var chunkTWTG5ILJ_cjs = require('../chunk-TWTG5ILJ.cjs');
|
|
5
6
|
require('../chunk-ZDSQYCYV.cjs');
|
|
6
7
|
|
|
7
8
|
// src/db/drizzle.ts
|
|
@@ -13,7 +14,7 @@ function ezlogsDrizzleLogger(options = {}) {
|
|
|
13
14
|
if (parsed) {
|
|
14
15
|
const resourceId = parsed.operation === "create" ? null : extractResourceIdFromSimpleWhere(query, params);
|
|
15
16
|
const changes = parsed.operation === "update" ? extractSetChangesFromUpdate(query, params) : null;
|
|
16
|
-
|
|
17
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
17
18
|
modelClass: parsed.table,
|
|
18
19
|
operation: parsed.operation,
|
|
19
20
|
resourceId,
|
|
@@ -21,7 +22,7 @@ function ezlogsDrizzleLogger(options = {}) {
|
|
|
21
22
|
});
|
|
22
23
|
}
|
|
23
24
|
} catch (error) {
|
|
24
|
-
|
|
25
|
+
chunkTWTG5ILJ_cjs.logger.debug(`drizzle capture failed: ${describeError(error)}`);
|
|
25
26
|
} finally {
|
|
26
27
|
if (options.next) {
|
|
27
28
|
try {
|
package/dist/db/drizzle.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { emitDbEvent } from '../chunk-
|
|
2
|
-
import
|
|
1
|
+
import { emitDbEvent } from '../chunk-NINIAL7M.js';
|
|
2
|
+
import '../chunk-GI6BM4MR.js';
|
|
3
|
+
import { logger } from '../chunk-FCFVIRQF.js';
|
|
3
4
|
import '../chunk-SIAA4J6H.js';
|
|
4
5
|
|
|
5
6
|
// src/db/drizzle.ts
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkPS4U2GFJ_cjs = require('../chunk-PS4U2GFJ.cjs');
|
|
4
|
+
var chunkMMLQXSSD_cjs = require('../chunk-MMLQXSSD.cjs');
|
|
5
|
+
require('../chunk-TWTG5ILJ.cjs');
|
|
6
|
+
require('../chunk-ZDSQYCYV.cjs');
|
|
7
|
+
|
|
8
|
+
// src/db/postgres-js.ts
|
|
9
|
+
var SET_CONTEXT_SQL = "select set_config('ezlogs.actor_id', $1, true), set_config('ezlogs.correlation_id', $2, true)";
|
|
10
|
+
var wrappedInstances = /* @__PURE__ */ new WeakSet();
|
|
11
|
+
function wrapPostgresJs(sql) {
|
|
12
|
+
if (typeof sql !== "function" || wrappedInstances.has(sql)) return sql;
|
|
13
|
+
const base = sql;
|
|
14
|
+
if (typeof base.unsafe !== "function" || typeof base.begin !== "function") return sql;
|
|
15
|
+
const unsafe = (...args) => tagIfWrite(base, base.unsafe(...args));
|
|
16
|
+
const begin = (...args) => beginWithContext(base, args);
|
|
17
|
+
const proxy = new Proxy(base, {
|
|
18
|
+
apply: (target, thisArg, args) => tagIfWrite(base, Reflect.apply(target, thisArg, args)),
|
|
19
|
+
get: (target, prop) => prop === "unsafe" ? unsafe : prop === "begin" ? begin : Reflect.get(target, prop)
|
|
20
|
+
});
|
|
21
|
+
wrappedInstances.add(proxy);
|
|
22
|
+
return proxy;
|
|
23
|
+
}
|
|
24
|
+
function tagContext() {
|
|
25
|
+
if (chunkMMLQXSSD_cjs.configuration().databaseReader == null) return null;
|
|
26
|
+
const ctx = chunkPS4U2GFJ_cjs.getDatabaseContext();
|
|
27
|
+
return ctx.correlationId ? ctx : null;
|
|
28
|
+
}
|
|
29
|
+
function setContext(tx, ctx) {
|
|
30
|
+
return tx.unsafe(SET_CONTEXT_SQL, [ctx.actorId ?? "", ctx.correlationId]);
|
|
31
|
+
}
|
|
32
|
+
function tagIfWrite(base, value) {
|
|
33
|
+
if (!isQuery(value) || !isWrite(value.strings)) return value;
|
|
34
|
+
const original = value.handler;
|
|
35
|
+
value.handler = Object.assign((query) => dispatch(base, query, original), {
|
|
36
|
+
debug: original.debug
|
|
37
|
+
});
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
function dispatch(base, query, original) {
|
|
41
|
+
const ctx = tagContext();
|
|
42
|
+
if (!ctx || query.cancelled || query.cursorFn || query.forEachFn || query.onlyDescribe || query.streaming) {
|
|
43
|
+
original(query);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
let tagged = false;
|
|
47
|
+
let copy = null;
|
|
48
|
+
let cancelled = false;
|
|
49
|
+
query.canceller = () => {
|
|
50
|
+
cancelled = true;
|
|
51
|
+
copy?.cancel();
|
|
52
|
+
};
|
|
53
|
+
base.begin(async (tx) => {
|
|
54
|
+
await setContext(tx, ctx);
|
|
55
|
+
if (cancelled) {
|
|
56
|
+
throw Object.assign(new Error("canceling statement due to user request"), { code: "57014" });
|
|
57
|
+
}
|
|
58
|
+
const built = query.tagged ? tx(query.strings, ...query.args) : tx.unsafe(query.strings[0], query.args, query.options);
|
|
59
|
+
Object.assign(built.options, query.options);
|
|
60
|
+
built.isRaw = query.isRaw;
|
|
61
|
+
copy = built;
|
|
62
|
+
tagged = true;
|
|
63
|
+
return await built;
|
|
64
|
+
}).then(
|
|
65
|
+
(result) => query.resolve(result),
|
|
66
|
+
// BEGIN / set_config / the copy failed before the write ran: run it
|
|
67
|
+
// untagged (unless the caller cancelled it).
|
|
68
|
+
(error) => tagged || cancelled ? query.reject(error) : original(query)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
function beginWithContext(base, args) {
|
|
72
|
+
const last = args.length - 1;
|
|
73
|
+
const fn = args[last];
|
|
74
|
+
const ctx = tagContext();
|
|
75
|
+
if (typeof fn !== "function" || !ctx) return base.begin(...args);
|
|
76
|
+
const withContext = async (tx) => {
|
|
77
|
+
await setContext(tx, ctx);
|
|
78
|
+
const result = fn(tx);
|
|
79
|
+
return Array.isArray(result) ? Promise.all(result) : result;
|
|
80
|
+
};
|
|
81
|
+
return base.begin(...args.slice(0, last), withContext);
|
|
82
|
+
}
|
|
83
|
+
function isQuery(value) {
|
|
84
|
+
const q = value;
|
|
85
|
+
return value instanceof Promise && typeof q.handler === "function" && Array.isArray(q.strings) && typeof q.resolve === "function";
|
|
86
|
+
}
|
|
87
|
+
var LEADING_NOISE = /^(?:\s+|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/|\()+/;
|
|
88
|
+
var WRITE_VERB = /^(?:insert|update|delete|merge)\b/i;
|
|
89
|
+
var WRITE_WORD = /\b(?:insert|update|delete|merge)\b/i;
|
|
90
|
+
function isWrite(strings) {
|
|
91
|
+
const text = strings.join(" ");
|
|
92
|
+
const head = text.replace(LEADING_NOISE, "");
|
|
93
|
+
if (WRITE_VERB.test(head)) return true;
|
|
94
|
+
return /^with\b/i.test(head) && WRITE_WORD.test(text);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
exports.isWrite = isWrite;
|
|
98
|
+
exports.wrapPostgresJs = wrapPostgresJs;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap a postgres.js `sql` instance so writes made inside a captured
|
|
3
|
+
* request carry its correlation id and actor. Returns the same API (a
|
|
4
|
+
* Proxy); pass the result to `drizzle()` for Drizzle on postgres-js.
|
|
5
|
+
* Anything that isn't a postgres.js instance is returned unchanged.
|
|
6
|
+
*/
|
|
7
|
+
declare function wrapPostgresJs<T>(sql: T): T;
|
|
8
|
+
/** INSERT / UPDATE / DELETE / MERGE, or a WITH whose body writes. @internal */
|
|
9
|
+
declare function isWrite(strings: ReadonlyArray<string>): boolean;
|
|
10
|
+
|
|
11
|
+
export { isWrite, wrapPostgresJs };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap a postgres.js `sql` instance so writes made inside a captured
|
|
3
|
+
* request carry its correlation id and actor. Returns the same API (a
|
|
4
|
+
* Proxy); pass the result to `drizzle()` for Drizzle on postgres-js.
|
|
5
|
+
* Anything that isn't a postgres.js instance is returned unchanged.
|
|
6
|
+
*/
|
|
7
|
+
declare function wrapPostgresJs<T>(sql: T): T;
|
|
8
|
+
/** INSERT / UPDATE / DELETE / MERGE, or a WITH whose body writes. @internal */
|
|
9
|
+
declare function isWrite(strings: ReadonlyArray<string>): boolean;
|
|
10
|
+
|
|
11
|
+
export { isWrite, wrapPostgresJs };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { getDatabaseContext } from '../chunk-YMIGZYFH.js';
|
|
2
|
+
import { configuration } from '../chunk-GI6BM4MR.js';
|
|
3
|
+
import '../chunk-FCFVIRQF.js';
|
|
4
|
+
import '../chunk-SIAA4J6H.js';
|
|
5
|
+
|
|
6
|
+
// src/db/postgres-js.ts
|
|
7
|
+
var SET_CONTEXT_SQL = "select set_config('ezlogs.actor_id', $1, true), set_config('ezlogs.correlation_id', $2, true)";
|
|
8
|
+
var wrappedInstances = /* @__PURE__ */ new WeakSet();
|
|
9
|
+
function wrapPostgresJs(sql) {
|
|
10
|
+
if (typeof sql !== "function" || wrappedInstances.has(sql)) return sql;
|
|
11
|
+
const base = sql;
|
|
12
|
+
if (typeof base.unsafe !== "function" || typeof base.begin !== "function") return sql;
|
|
13
|
+
const unsafe = (...args) => tagIfWrite(base, base.unsafe(...args));
|
|
14
|
+
const begin = (...args) => beginWithContext(base, args);
|
|
15
|
+
const proxy = new Proxy(base, {
|
|
16
|
+
apply: (target, thisArg, args) => tagIfWrite(base, Reflect.apply(target, thisArg, args)),
|
|
17
|
+
get: (target, prop) => prop === "unsafe" ? unsafe : prop === "begin" ? begin : Reflect.get(target, prop)
|
|
18
|
+
});
|
|
19
|
+
wrappedInstances.add(proxy);
|
|
20
|
+
return proxy;
|
|
21
|
+
}
|
|
22
|
+
function tagContext() {
|
|
23
|
+
if (configuration().databaseReader == null) return null;
|
|
24
|
+
const ctx = getDatabaseContext();
|
|
25
|
+
return ctx.correlationId ? ctx : null;
|
|
26
|
+
}
|
|
27
|
+
function setContext(tx, ctx) {
|
|
28
|
+
return tx.unsafe(SET_CONTEXT_SQL, [ctx.actorId ?? "", ctx.correlationId]);
|
|
29
|
+
}
|
|
30
|
+
function tagIfWrite(base, value) {
|
|
31
|
+
if (!isQuery(value) || !isWrite(value.strings)) return value;
|
|
32
|
+
const original = value.handler;
|
|
33
|
+
value.handler = Object.assign((query) => dispatch(base, query, original), {
|
|
34
|
+
debug: original.debug
|
|
35
|
+
});
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function dispatch(base, query, original) {
|
|
39
|
+
const ctx = tagContext();
|
|
40
|
+
if (!ctx || query.cancelled || query.cursorFn || query.forEachFn || query.onlyDescribe || query.streaming) {
|
|
41
|
+
original(query);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let tagged = false;
|
|
45
|
+
let copy = null;
|
|
46
|
+
let cancelled = false;
|
|
47
|
+
query.canceller = () => {
|
|
48
|
+
cancelled = true;
|
|
49
|
+
copy?.cancel();
|
|
50
|
+
};
|
|
51
|
+
base.begin(async (tx) => {
|
|
52
|
+
await setContext(tx, ctx);
|
|
53
|
+
if (cancelled) {
|
|
54
|
+
throw Object.assign(new Error("canceling statement due to user request"), { code: "57014" });
|
|
55
|
+
}
|
|
56
|
+
const built = query.tagged ? tx(query.strings, ...query.args) : tx.unsafe(query.strings[0], query.args, query.options);
|
|
57
|
+
Object.assign(built.options, query.options);
|
|
58
|
+
built.isRaw = query.isRaw;
|
|
59
|
+
copy = built;
|
|
60
|
+
tagged = true;
|
|
61
|
+
return await built;
|
|
62
|
+
}).then(
|
|
63
|
+
(result) => query.resolve(result),
|
|
64
|
+
// BEGIN / set_config / the copy failed before the write ran: run it
|
|
65
|
+
// untagged (unless the caller cancelled it).
|
|
66
|
+
(error) => tagged || cancelled ? query.reject(error) : original(query)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
function beginWithContext(base, args) {
|
|
70
|
+
const last = args.length - 1;
|
|
71
|
+
const fn = args[last];
|
|
72
|
+
const ctx = tagContext();
|
|
73
|
+
if (typeof fn !== "function" || !ctx) return base.begin(...args);
|
|
74
|
+
const withContext = async (tx) => {
|
|
75
|
+
await setContext(tx, ctx);
|
|
76
|
+
const result = fn(tx);
|
|
77
|
+
return Array.isArray(result) ? Promise.all(result) : result;
|
|
78
|
+
};
|
|
79
|
+
return base.begin(...args.slice(0, last), withContext);
|
|
80
|
+
}
|
|
81
|
+
function isQuery(value) {
|
|
82
|
+
const q = value;
|
|
83
|
+
return value instanceof Promise && typeof q.handler === "function" && Array.isArray(q.strings) && typeof q.resolve === "function";
|
|
84
|
+
}
|
|
85
|
+
var LEADING_NOISE = /^(?:\s+|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/|\()+/;
|
|
86
|
+
var WRITE_VERB = /^(?:insert|update|delete|merge)\b/i;
|
|
87
|
+
var WRITE_WORD = /\b(?:insert|update|delete|merge)\b/i;
|
|
88
|
+
function isWrite(strings) {
|
|
89
|
+
const text = strings.join(" ");
|
|
90
|
+
const head = text.replace(LEADING_NOISE, "");
|
|
91
|
+
if (WRITE_VERB.test(head)) return true;
|
|
92
|
+
return /^with\b/i.test(head) && WRITE_WORD.test(text);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { isWrite, wrapPostgresJs };
|
package/dist/db/prisma.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkYXZZNZG6_cjs = require('../chunk-YXZZNZG6.cjs');
|
|
4
|
+
var chunkMMLQXSSD_cjs = require('../chunk-MMLQXSSD.cjs');
|
|
5
|
+
var chunkTWTG5ILJ_cjs = require('../chunk-TWTG5ILJ.cjs');
|
|
5
6
|
require('../chunk-ZDSQYCYV.cjs');
|
|
6
7
|
|
|
7
8
|
// src/db/prisma.ts
|
|
@@ -26,7 +27,7 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
26
27
|
deleteMany: "destroy"
|
|
27
28
|
};
|
|
28
29
|
function ezlogsPrisma() {
|
|
29
|
-
return (client) => client.$extends(buildPrismaExtension(client));
|
|
30
|
+
return ((client) => client.$extends(buildPrismaExtension(client)));
|
|
30
31
|
}
|
|
31
32
|
function buildPrismaExtension(client, schemaClient = client) {
|
|
32
33
|
return {
|
|
@@ -41,7 +42,7 @@ function buildPrismaExtension(client, schemaClient = client) {
|
|
|
41
42
|
try {
|
|
42
43
|
recordPrismaOperation(ctx, result, tableNameFor(schemaClient, ctx.model));
|
|
43
44
|
} catch (error) {
|
|
44
|
-
|
|
45
|
+
chunkTWTG5ILJ_cjs.logger.debug(`prisma capture failed: ${describeError(error)}`);
|
|
45
46
|
}
|
|
46
47
|
return result;
|
|
47
48
|
}
|
|
@@ -56,19 +57,19 @@ function buildPrismaExtension(client, schemaClient = client) {
|
|
|
56
57
|
}
|
|
57
58
|
};
|
|
58
59
|
}
|
|
59
|
-
var
|
|
60
|
+
var SET_CONTEXT_SQL = "SELECT set_config('ezlogs.correlation_id', $1, TRUE), set_config('ezlogs.actor_id', $2, TRUE)";
|
|
60
61
|
async function runTagged(client, ctx) {
|
|
61
62
|
if (!shouldTag(client, ctx)) return ctx.query(ctx.args);
|
|
62
63
|
try {
|
|
63
64
|
const results = await client.$transaction([
|
|
64
|
-
client.$executeRawUnsafe(
|
|
65
|
+
client.$executeRawUnsafe(SET_CONTEXT_SQL, chunkMMLQXSSD_cjs.current(), chunkMMLQXSSD_cjs.getCurrentActor()?.id ?? ""),
|
|
65
66
|
ctx.query(ctx.args)
|
|
66
67
|
]);
|
|
67
68
|
return results[1];
|
|
68
69
|
} catch (error) {
|
|
69
70
|
if (!isTransactionUnsupported(error)) throw error;
|
|
70
71
|
taggingDisabled.add(client);
|
|
71
|
-
|
|
72
|
+
chunkTWTG5ILJ_cjs.logger.warn(
|
|
72
73
|
`this Prisma client can't run transactions, so DB writes won't be linked to their request (they still show up as their own actions): ${describeError(error)}`
|
|
73
74
|
);
|
|
74
75
|
return ctx.query(ctx.args);
|
|
@@ -80,12 +81,12 @@ function _resetPrismaTaggingForTests() {
|
|
|
80
81
|
}
|
|
81
82
|
function shouldTag(client, ctx) {
|
|
82
83
|
if (!client || taggingDisabled.has(client)) return false;
|
|
83
|
-
if (
|
|
84
|
+
if (chunkMMLQXSSD_cjs.configuration().databaseReader == null) return false;
|
|
84
85
|
if (!isPostgresClient(client)) return false;
|
|
85
86
|
if (typeof client.$transaction !== "function" || typeof client.$executeRawUnsafe !== "function") {
|
|
86
87
|
return false;
|
|
87
88
|
}
|
|
88
|
-
if (!
|
|
89
|
+
if (!chunkMMLQXSSD_cjs.current()) return false;
|
|
89
90
|
const params = ctx.__internalParams;
|
|
90
91
|
return params != null && !params.transaction;
|
|
91
92
|
}
|
|
@@ -111,7 +112,7 @@ function recordPrismaOperation(ctx, result, table) {
|
|
|
111
112
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
112
113
|
if (!dbOperation) return;
|
|
113
114
|
if (isBatchResult(result)) {
|
|
114
|
-
|
|
115
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
115
116
|
modelClass: ctx.model,
|
|
116
117
|
table,
|
|
117
118
|
operation: dbOperation,
|
|
@@ -138,7 +139,7 @@ function recordPrismaOperation(ctx, result, table) {
|
|
|
138
139
|
}
|
|
139
140
|
function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
140
141
|
if (!isPlainObject(row)) {
|
|
141
|
-
|
|
142
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
142
143
|
modelClass: ctx.model,
|
|
143
144
|
table,
|
|
144
145
|
operation: dbOperation,
|
|
@@ -148,9 +149,9 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
148
149
|
}
|
|
149
150
|
const rowObj = row;
|
|
150
151
|
const resourceId = rowObj.id ?? null;
|
|
151
|
-
const displayName =
|
|
152
|
+
const displayName = chunkYXZZNZG6_cjs.resolveDisplayName(ctx.model, rowObj);
|
|
152
153
|
if (dbOperation === "create") {
|
|
153
|
-
|
|
154
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
154
155
|
modelClass: ctx.model,
|
|
155
156
|
table,
|
|
156
157
|
operation: "create",
|
|
@@ -165,7 +166,7 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
165
166
|
return;
|
|
166
167
|
}
|
|
167
168
|
if (dbOperation === "update") {
|
|
168
|
-
|
|
169
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
169
170
|
modelClass: ctx.model,
|
|
170
171
|
table,
|
|
171
172
|
operation: "update",
|
|
@@ -176,7 +177,7 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
176
177
|
});
|
|
177
178
|
return;
|
|
178
179
|
}
|
|
179
|
-
|
|
180
|
+
chunkYXZZNZG6_cjs.emitDbEvent({
|
|
180
181
|
modelClass: ctx.model,
|
|
181
182
|
table,
|
|
182
183
|
operation: "destroy",
|
|
@@ -267,16 +268,16 @@ function patchPrismaClient(prisma, adapter) {
|
|
|
267
268
|
if (prisma[EZLOGS_PRISMA_BRAND] === true) {
|
|
268
269
|
return prisma;
|
|
269
270
|
}
|
|
270
|
-
const captureAndRun =
|
|
271
|
+
const captureAndRun = chunkMMLQXSSD_cjs.makeAsyncContextCapture();
|
|
271
272
|
if (!captureAndRun) {
|
|
272
|
-
|
|
273
|
+
chunkTWTG5ILJ_cjs.logger.debug(
|
|
273
274
|
"patchPrismaClient: AsyncLocalStorage.snapshot() unavailable; skipping ALS bind, applying ezlogsPrisma() only."
|
|
274
275
|
);
|
|
275
276
|
return applyEzlogsExtension(prisma);
|
|
276
277
|
}
|
|
277
278
|
const patchedAdapter = adapter && typeof adapter === "object" && adapter[EZLOGS_ADAPTER_BRAND] === true ? adapter : null;
|
|
278
279
|
if (adapter && !patchedAdapter) {
|
|
279
|
-
|
|
280
|
+
chunkTWTG5ILJ_cjs.logger.debug(
|
|
280
281
|
"patchPrismaClient: adapter is not the output of patchPrismaPgAdapter(). Correlation_id will be NULL on concurrent requests. Wrap the adapter first."
|
|
281
282
|
);
|
|
282
283
|
}
|
|
@@ -287,7 +288,7 @@ function patchPrismaClient(prisma, adapter) {
|
|
|
287
288
|
function applyEzlogsExtension(prisma) {
|
|
288
289
|
const client = prisma;
|
|
289
290
|
if (typeof client.$extends !== "function") {
|
|
290
|
-
|
|
291
|
+
chunkTWTG5ILJ_cjs.logger.debug(
|
|
291
292
|
"patchPrismaClient: client has no $extends; skipping ezlogsPrisma extension."
|
|
292
293
|
);
|
|
293
294
|
return prisma;
|
|
@@ -295,7 +296,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
295
296
|
try {
|
|
296
297
|
return client.$extends(buildPrismaExtension(null, prisma));
|
|
297
298
|
} catch (error) {
|
|
298
|
-
|
|
299
|
+
chunkTWTG5ILJ_cjs.logger.debug(`patchPrismaClient: $extends failed: ${describeError(error)}`);
|
|
299
300
|
return prisma;
|
|
300
301
|
}
|
|
301
302
|
}
|
package/dist/db/prisma.d.cts
CHANGED
|
@@ -26,7 +26,24 @@ interface TaggingClient {
|
|
|
26
26
|
* writes inside a user `$transaction` (batch or interactive) — Prisma
|
|
27
27
|
* can't nest a transaction there.
|
|
28
28
|
*/
|
|
29
|
-
declare function ezlogsPrisma():
|
|
29
|
+
declare function ezlogsPrisma(): EzlogsPrismaExtension;
|
|
30
|
+
/**
|
|
31
|
+
* The type `Prisma.defineExtension((client) => …)` gives a query-only
|
|
32
|
+
* extension (no added result / model / client members), spelled out so
|
|
33
|
+
* `$extends(ezlogsPrisma())` type-checks on Prisma 5, 6 and 7 without
|
|
34
|
+
* importing `@prisma/client` types, and the extended client keeps
|
|
35
|
+
* every model type unchanged.
|
|
36
|
+
*/
|
|
37
|
+
type EzlogsPrismaExtension = (client: any) => {
|
|
38
|
+
$extends: {
|
|
39
|
+
extArgs: {
|
|
40
|
+
result: {};
|
|
41
|
+
model: {};
|
|
42
|
+
query: {};
|
|
43
|
+
client: {};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
30
47
|
/**
|
|
31
48
|
* The extension descriptor. `client` null = capture only, no tagging
|
|
32
49
|
* (the adapter-pg path tags via the patched `pg` driver instead).
|
|
@@ -75,4 +92,4 @@ declare function patchPrismaPgAdapter<A extends object>(adapter: A): A;
|
|
|
75
92
|
*/
|
|
76
93
|
declare function patchPrismaClient<T extends object>(prisma: T, adapter?: object): T;
|
|
77
94
|
|
|
78
|
-
export { _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
|
95
|
+
export { type EzlogsPrismaExtension, _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
package/dist/db/prisma.d.ts
CHANGED
|
@@ -26,7 +26,24 @@ interface TaggingClient {
|
|
|
26
26
|
* writes inside a user `$transaction` (batch or interactive) — Prisma
|
|
27
27
|
* can't nest a transaction there.
|
|
28
28
|
*/
|
|
29
|
-
declare function ezlogsPrisma():
|
|
29
|
+
declare function ezlogsPrisma(): EzlogsPrismaExtension;
|
|
30
|
+
/**
|
|
31
|
+
* The type `Prisma.defineExtension((client) => …)` gives a query-only
|
|
32
|
+
* extension (no added result / model / client members), spelled out so
|
|
33
|
+
* `$extends(ezlogsPrisma())` type-checks on Prisma 5, 6 and 7 without
|
|
34
|
+
* importing `@prisma/client` types, and the extended client keeps
|
|
35
|
+
* every model type unchanged.
|
|
36
|
+
*/
|
|
37
|
+
type EzlogsPrismaExtension = (client: any) => {
|
|
38
|
+
$extends: {
|
|
39
|
+
extArgs: {
|
|
40
|
+
result: {};
|
|
41
|
+
model: {};
|
|
42
|
+
query: {};
|
|
43
|
+
client: {};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
30
47
|
/**
|
|
31
48
|
* The extension descriptor. `client` null = capture only, no tagging
|
|
32
49
|
* (the adapter-pg path tags via the patched `pg` driver instead).
|
|
@@ -75,4 +92,4 @@ declare function patchPrismaPgAdapter<A extends object>(adapter: A): A;
|
|
|
75
92
|
*/
|
|
76
93
|
declare function patchPrismaClient<T extends object>(prisma: T, adapter?: object): T;
|
|
77
94
|
|
|
78
|
-
export { _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
|
95
|
+
export { type EzlogsPrismaExtension, _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
package/dist/db/prisma.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { emitDbEvent, resolveDisplayName } from '../chunk-NINIAL7M.js';
|
|
2
|
+
import { current, getCurrentActor, configuration, makeAsyncContextCapture } from '../chunk-GI6BM4MR.js';
|
|
3
|
+
import { logger } from '../chunk-FCFVIRQF.js';
|
|
3
4
|
import '../chunk-SIAA4J6H.js';
|
|
4
5
|
|
|
5
6
|
// src/db/prisma.ts
|
|
@@ -24,7 +25,7 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
24
25
|
deleteMany: "destroy"
|
|
25
26
|
};
|
|
26
27
|
function ezlogsPrisma() {
|
|
27
|
-
return (client) => client.$extends(buildPrismaExtension(client));
|
|
28
|
+
return ((client) => client.$extends(buildPrismaExtension(client)));
|
|
28
29
|
}
|
|
29
30
|
function buildPrismaExtension(client, schemaClient = client) {
|
|
30
31
|
return {
|
|
@@ -54,12 +55,12 @@ function buildPrismaExtension(client, schemaClient = client) {
|
|
|
54
55
|
}
|
|
55
56
|
};
|
|
56
57
|
}
|
|
57
|
-
var
|
|
58
|
+
var SET_CONTEXT_SQL = "SELECT set_config('ezlogs.correlation_id', $1, TRUE), set_config('ezlogs.actor_id', $2, TRUE)";
|
|
58
59
|
async function runTagged(client, ctx) {
|
|
59
60
|
if (!shouldTag(client, ctx)) return ctx.query(ctx.args);
|
|
60
61
|
try {
|
|
61
62
|
const results = await client.$transaction([
|
|
62
|
-
client.$executeRawUnsafe(
|
|
63
|
+
client.$executeRawUnsafe(SET_CONTEXT_SQL, current(), getCurrentActor()?.id ?? ""),
|
|
63
64
|
ctx.query(ctx.args)
|
|
64
65
|
]);
|
|
65
66
|
return results[1];
|