ezlogs-nextjs 0.1.4 → 0.3.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/README.md +106 -11
- package/bin/install-triggers.cjs +49 -11
- package/dist/actors/index.d.cts +1 -1
- package/dist/actors/index.d.ts +1 -1
- package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
- package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
- package/dist/db/drizzle.cjs +154 -18
- package/dist/db/drizzle.js +154 -18
- package/dist/db/prisma.cjs +242 -45
- package/dist/db/prisma.d.cts +39 -1
- package/dist/db/prisma.d.ts +39 -1
- package/dist/db/prisma.js +241 -46
- package/dist/db/supabase.cjs +154 -18
- package/dist/db/supabase.js +154 -18
- package/dist/index.cjs +761 -245
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +761 -245
- package/dist/jobs/bullmq.cjs +973 -16
- package/dist/jobs/bullmq.d.cts +8 -3
- package/dist/jobs/bullmq.d.ts +8 -3
- package/dist/jobs/bullmq.js +973 -16
- package/dist/jobs/inngest.cjs +880 -14
- package/dist/jobs/inngest.d.cts +7 -5
- package/dist/jobs/inngest.d.ts +7 -5
- package/dist/jobs/inngest.js +880 -14
- package/dist/jobs/supabase-queues.cjs +878 -16
- package/dist/jobs/supabase-queues.js +878 -16
- package/dist/jobs/trigger.cjs +914 -42
- package/dist/jobs/trigger.js +914 -42
- package/dist/jobs/upstash-workflow.cjs +914 -14
- package/dist/jobs/upstash-workflow.d.cts +16 -2
- package/dist/jobs/upstash-workflow.d.ts +16 -2
- package/dist/jobs/upstash-workflow.js +914 -15
- package/dist/triggers/index.cjs +405 -76
- package/dist/triggers/index.d.cts +36 -41
- package/dist/triggers/index.d.ts +36 -41
- package/dist/triggers/index.js +404 -74
- package/package.json +5 -1
- package/sql/postgres/00_install.sql +244 -21
package/dist/db/prisma.js
CHANGED
|
@@ -120,6 +120,14 @@ function createScopedStorage(key = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:def
|
|
|
120
120
|
};
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
// src/correlation.ts
|
|
124
|
+
var storage = createScopedStorage(
|
|
125
|
+
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
126
|
+
);
|
|
127
|
+
function current() {
|
|
128
|
+
return storage.read();
|
|
129
|
+
}
|
|
130
|
+
|
|
123
131
|
// src/configuration.ts
|
|
124
132
|
var DEFAULT_EXCLUDED_PATHS = Object.freeze([
|
|
125
133
|
// Next.js / Vercel infrastructure noise
|
|
@@ -189,7 +197,6 @@ var Configuration = class {
|
|
|
189
197
|
displayNameFor = {};
|
|
190
198
|
isServerActionError = null;
|
|
191
199
|
databaseReader = null;
|
|
192
|
-
databaseSnapshotMode = false;
|
|
193
200
|
databaseUrl = null;
|
|
194
201
|
/**
|
|
195
202
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -241,9 +248,6 @@ var Configuration = class {
|
|
|
241
248
|
if (options.databaseReader !== void 0) {
|
|
242
249
|
this.databaseReader = options.databaseReader;
|
|
243
250
|
}
|
|
244
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
245
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
246
|
-
}
|
|
247
251
|
if (options.databaseUrl !== void 0) {
|
|
248
252
|
this.databaseUrl = options.databaseUrl;
|
|
249
253
|
}
|
|
@@ -486,14 +490,6 @@ function describe2(error) {
|
|
|
486
490
|
return String(error);
|
|
487
491
|
}
|
|
488
492
|
|
|
489
|
-
// src/correlation.ts
|
|
490
|
-
var storage = createScopedStorage(
|
|
491
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
492
|
-
);
|
|
493
|
-
function current() {
|
|
494
|
-
return storage.read();
|
|
495
|
-
}
|
|
496
|
-
|
|
497
493
|
// src/actor.ts
|
|
498
494
|
var storage2 = createScopedStorage(
|
|
499
495
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:actor-storage")
|
|
@@ -501,6 +497,57 @@ var storage2 = createScopedStorage(
|
|
|
501
497
|
function getCurrentActor() {
|
|
502
498
|
return storage2.read();
|
|
503
499
|
}
|
|
500
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
501
|
+
"pin",
|
|
502
|
+
"otp",
|
|
503
|
+
"totp",
|
|
504
|
+
"iban",
|
|
505
|
+
"mfa",
|
|
506
|
+
"passphrase"
|
|
507
|
+
]);
|
|
508
|
+
function snakeLower(key) {
|
|
509
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
510
|
+
}
|
|
511
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
512
|
+
const lower = key.toLowerCase();
|
|
513
|
+
const snake = snakeLower(key);
|
|
514
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
518
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
519
|
+
}
|
|
520
|
+
function passesLuhn(digits) {
|
|
521
|
+
let sum = 0;
|
|
522
|
+
for (let i = 0; i < digits.length; i += 1) {
|
|
523
|
+
let d = Number(digits[digits.length - 1 - i]);
|
|
524
|
+
if (i % 2 === 1) {
|
|
525
|
+
d *= 2;
|
|
526
|
+
if (d > 9) d -= 9;
|
|
527
|
+
}
|
|
528
|
+
sum += d;
|
|
529
|
+
}
|
|
530
|
+
return sum % 10 === 0;
|
|
531
|
+
}
|
|
532
|
+
var SECRET_TEXT_PATTERNS = [
|
|
533
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
|
|
534
|
+
/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
|
|
535
|
+
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
|
|
536
|
+
/\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
537
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}/g,
|
|
538
|
+
/\bxox[abposr]-[A-Za-z0-9-]+/g,
|
|
539
|
+
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
|
|
540
|
+
];
|
|
541
|
+
var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
|
|
542
|
+
function scrubSecretsInText(text, options = {}) {
|
|
543
|
+
let out = text;
|
|
544
|
+
for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
|
|
545
|
+
out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
|
|
546
|
+
const digits = match.replace(/[ -]/g, "");
|
|
547
|
+
return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
|
|
548
|
+
});
|
|
549
|
+
return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
|
|
550
|
+
}
|
|
504
551
|
|
|
505
552
|
// src/db/shared.ts
|
|
506
553
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -509,8 +556,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
|
|
|
509
556
|
"created_at",
|
|
510
557
|
"updatedAt",
|
|
511
558
|
"updated_at",
|
|
512
|
-
"deletedAt",
|
|
513
|
-
"deleted_at",
|
|
514
559
|
// Auth.js / NextAuth standard columns
|
|
515
560
|
"emailVerified",
|
|
516
561
|
"expires"
|
|
@@ -533,7 +578,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
533
578
|
"salt",
|
|
534
579
|
"digest",
|
|
535
580
|
"signature",
|
|
536
|
-
"hmac"
|
|
581
|
+
"hmac",
|
|
582
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
583
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
584
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
585
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
586
|
+
"credential",
|
|
587
|
+
"passwd",
|
|
588
|
+
"passcode",
|
|
589
|
+
"apikey",
|
|
590
|
+
"card_number",
|
|
591
|
+
"cardnumber",
|
|
592
|
+
"cvv",
|
|
593
|
+
"cvc"
|
|
537
594
|
]);
|
|
538
595
|
function emitDbEvent(input) {
|
|
539
596
|
try {
|
|
@@ -542,14 +599,17 @@ function emitDbEvent(input) {
|
|
|
542
599
|
if (config.allExcludedTables().includes(input.modelClass)) return;
|
|
543
600
|
if (config.databaseReader && !input._fromTrigger) {
|
|
544
601
|
const tracked = config.triggerTrackedTables;
|
|
545
|
-
if (tracked === null || tracked.has(input.modelClass)) return;
|
|
602
|
+
if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
|
|
546
603
|
}
|
|
547
604
|
if (input.operation === "update") {
|
|
548
605
|
const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
|
|
549
606
|
if (meaningful.length === 0) return;
|
|
550
607
|
}
|
|
551
608
|
const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
|
|
552
|
-
const
|
|
609
|
+
const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
|
|
610
|
+
const context = buildContext(
|
|
611
|
+
related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
|
|
612
|
+
);
|
|
553
613
|
const event = build({
|
|
554
614
|
source_type: "database_callback",
|
|
555
615
|
source_data: {
|
|
@@ -558,9 +618,10 @@ function emitDbEvent(input) {
|
|
|
558
618
|
},
|
|
559
619
|
outcome: "success",
|
|
560
620
|
correlation_id: current(),
|
|
561
|
-
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
621
|
+
resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
562
622
|
context,
|
|
563
|
-
duration_ms: null
|
|
623
|
+
duration_ms: null,
|
|
624
|
+
timestamp: input.timestamp ?? null
|
|
564
625
|
});
|
|
565
626
|
pushEvent(event);
|
|
566
627
|
} catch (error) {
|
|
@@ -604,7 +665,7 @@ function filterMeaningfulAttributes(attrs) {
|
|
|
604
665
|
return Object.keys(out).length === 0 ? null : out;
|
|
605
666
|
}
|
|
606
667
|
function filterMeaningfulChanges(changes) {
|
|
607
|
-
return changes.filter((c) => {
|
|
668
|
+
return flattenNestedChanges(changes).filter((c) => {
|
|
608
669
|
if (!isMeaningfulAttribute(c.attribute)) return false;
|
|
609
670
|
if (!isScalar(c.to)) return false;
|
|
610
671
|
if (c.from === void 0) return true;
|
|
@@ -612,14 +673,67 @@ function filterMeaningfulChanges(changes) {
|
|
|
612
673
|
return valuesActuallyChanged(c.from, c.to);
|
|
613
674
|
});
|
|
614
675
|
}
|
|
676
|
+
var MAX_NESTED_DEPTH = 3;
|
|
677
|
+
var MAX_NESTED_LEAVES = 20;
|
|
678
|
+
var MAX_UNKNOWN_FROM_LEAVES = 5;
|
|
679
|
+
function flattenNestedChanges(changes) {
|
|
680
|
+
if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
|
|
681
|
+
const out = [];
|
|
682
|
+
for (const c of changes) {
|
|
683
|
+
if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
|
|
684
|
+
out.push(c);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
if (!isMeaningfulAttribute(c.attribute)) continue;
|
|
688
|
+
const leaves = [];
|
|
689
|
+
collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
|
|
690
|
+
const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
|
|
691
|
+
out.push(...leaves.slice(0, cap).map(scrubLeaf));
|
|
692
|
+
}
|
|
693
|
+
return out;
|
|
694
|
+
}
|
|
695
|
+
function collectLeafChanges(path, from, to, depth, out) {
|
|
696
|
+
if (out.length > MAX_NESTED_LEAVES) return;
|
|
697
|
+
const fromObj = isPlainRecord(from);
|
|
698
|
+
const toObj = isPlainRecord(to);
|
|
699
|
+
if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
|
|
700
|
+
const f = fromObj ? from : {};
|
|
701
|
+
const t = toObj ? to : {};
|
|
702
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
|
|
703
|
+
const before = from === void 0 ? void 0 : f[key] ?? null;
|
|
704
|
+
collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
|
|
705
|
+
}
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
if (from !== void 0 && sameJson(from, to)) return;
|
|
709
|
+
out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
|
|
710
|
+
}
|
|
711
|
+
function scrubLeaf(change) {
|
|
712
|
+
const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
|
|
713
|
+
const out = { attribute: change.attribute, to: scrub(change.to) };
|
|
714
|
+
if (change.from !== void 0) out.from = scrub(change.from);
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
function isPlainRecord(value) {
|
|
718
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
719
|
+
const proto = Object.getPrototypeOf(value);
|
|
720
|
+
return proto === Object.prototype || proto === null;
|
|
721
|
+
}
|
|
722
|
+
function sameJson(a, b) {
|
|
723
|
+
if (a === b) return true;
|
|
724
|
+
try {
|
|
725
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
726
|
+
} catch {
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
615
730
|
function isMeaningfulAttribute(name) {
|
|
616
731
|
if (IGNORED_ATTRIBUTES.includes(name)) return false;
|
|
617
732
|
if (isDbSensitive(name)) return false;
|
|
618
733
|
return true;
|
|
619
734
|
}
|
|
620
735
|
function isDbSensitive(name) {
|
|
621
|
-
|
|
622
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
736
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
623
737
|
}
|
|
624
738
|
function isScalar(value) {
|
|
625
739
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
|
@@ -650,22 +764,44 @@ function isPrismaDecimal(value) {
|
|
|
650
764
|
const obj = value;
|
|
651
765
|
return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
|
|
652
766
|
}
|
|
767
|
+
var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
|
|
768
|
+
var MAX_DISPLAY_NAME_LENGTH = 120;
|
|
653
769
|
function resolveDisplayName(modelClass, instance) {
|
|
654
770
|
try {
|
|
655
771
|
const config = configuration();
|
|
656
772
|
const resolver = config.displayNameFor[modelClass];
|
|
657
773
|
const resolved = applyResolver(resolver, instance);
|
|
658
774
|
if (resolved !== null) return resolved;
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
if (
|
|
662
|
-
|
|
775
|
+
const columns = /* @__PURE__ */ new Map();
|
|
776
|
+
for (const key of Object.keys(instance)) {
|
|
777
|
+
if (isDbSensitive(key)) continue;
|
|
778
|
+
const norm = key.toLowerCase().replace(/_/g, "");
|
|
779
|
+
if (!columns.has(norm)) columns.set(norm, instance[key]);
|
|
780
|
+
}
|
|
781
|
+
const text = (norm) => {
|
|
782
|
+
const value = columns.get(norm);
|
|
783
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
|
|
784
|
+
return null;
|
|
663
785
|
}
|
|
786
|
+
const str = String(value).trim();
|
|
787
|
+
return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
|
|
788
|
+
};
|
|
789
|
+
for (const column of DISPLAY_NAME_COLUMNS) {
|
|
790
|
+
const value = text(column);
|
|
791
|
+
if (value !== null) return value;
|
|
664
792
|
}
|
|
793
|
+
return text(`${singularNorm(modelClass)}number`);
|
|
665
794
|
} catch {
|
|
666
795
|
}
|
|
667
796
|
return null;
|
|
668
797
|
}
|
|
798
|
+
function singularNorm(modelClass) {
|
|
799
|
+
const norm = modelClass.toLowerCase().replace(/_/g, "");
|
|
800
|
+
if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
|
|
801
|
+
if (/(ss|us|is)$/.test(norm)) return norm;
|
|
802
|
+
if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
|
|
803
|
+
return norm.endsWith("s") ? norm.slice(0, -1) : norm;
|
|
804
|
+
}
|
|
669
805
|
function applyResolver(resolver, instance) {
|
|
670
806
|
if (typeof resolver === "string") {
|
|
671
807
|
const value = instance[resolver];
|
|
@@ -713,6 +849,9 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
713
849
|
deleteMany: "destroy"
|
|
714
850
|
};
|
|
715
851
|
function ezlogsPrisma() {
|
|
852
|
+
return (client) => client.$extends(buildPrismaExtension(client));
|
|
853
|
+
}
|
|
854
|
+
function buildPrismaExtension(client, schemaClient = client) {
|
|
716
855
|
return {
|
|
717
856
|
name: "ezlogs",
|
|
718
857
|
query: {
|
|
@@ -721,24 +860,83 @@ function ezlogsPrisma() {
|
|
|
721
860
|
if (!CAPTURED_OPERATIONS.has(ctx.operation)) {
|
|
722
861
|
return ctx.query(ctx.args);
|
|
723
862
|
}
|
|
724
|
-
const result = await
|
|
863
|
+
const result = await runTagged(client, ctx);
|
|
725
864
|
try {
|
|
726
|
-
recordPrismaOperation(ctx, result);
|
|
865
|
+
recordPrismaOperation(ctx, result, tableNameFor(schemaClient, ctx.model));
|
|
727
866
|
} catch (error) {
|
|
728
867
|
logger.debug(`prisma capture failed: ${describeError2(error)}`);
|
|
729
868
|
}
|
|
730
869
|
return result;
|
|
731
870
|
}
|
|
871
|
+
},
|
|
872
|
+
// Raw writes (`$executeRaw\`UPDATE …\``) aren't model operations.
|
|
873
|
+
async $allOperations(ctx) {
|
|
874
|
+
if (ctx.operation !== "$executeRaw" && ctx.operation !== "$executeRawUnsafe") {
|
|
875
|
+
return ctx.query(ctx.args);
|
|
876
|
+
}
|
|
877
|
+
return runTagged(client, ctx);
|
|
732
878
|
}
|
|
733
879
|
}
|
|
734
880
|
};
|
|
735
881
|
}
|
|
736
|
-
|
|
882
|
+
var SET_CORRELATION_SQL = "SELECT set_config('ezlogs.correlation_id', $1, TRUE)";
|
|
883
|
+
async function runTagged(client, ctx) {
|
|
884
|
+
if (!shouldTag(client, ctx)) return ctx.query(ctx.args);
|
|
885
|
+
try {
|
|
886
|
+
const results = await client.$transaction([
|
|
887
|
+
client.$executeRawUnsafe(SET_CORRELATION_SQL, current()),
|
|
888
|
+
ctx.query(ctx.args)
|
|
889
|
+
]);
|
|
890
|
+
return results[1];
|
|
891
|
+
} catch (error) {
|
|
892
|
+
if (!isTransactionUnsupported(error)) throw error;
|
|
893
|
+
taggingDisabled.add(client);
|
|
894
|
+
logger.warn(
|
|
895
|
+
`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): ${describeError2(error)}`
|
|
896
|
+
);
|
|
897
|
+
return ctx.query(ctx.args);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
var taggingDisabled = /* @__PURE__ */ new WeakSet();
|
|
901
|
+
function _resetPrismaTaggingForTests() {
|
|
902
|
+
taggingDisabled = /* @__PURE__ */ new WeakSet();
|
|
903
|
+
}
|
|
904
|
+
function shouldTag(client, ctx) {
|
|
905
|
+
if (!client || taggingDisabled.has(client)) return false;
|
|
906
|
+
if (configuration().databaseReader == null) return false;
|
|
907
|
+
if (!isPostgresClient(client)) return false;
|
|
908
|
+
if (typeof client.$transaction !== "function" || typeof client.$executeRawUnsafe !== "function") {
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
if (!current()) return false;
|
|
912
|
+
const params = ctx.__internalParams;
|
|
913
|
+
return params != null && !params.transaction;
|
|
914
|
+
}
|
|
915
|
+
function isPostgresClient(client) {
|
|
916
|
+
const c = client;
|
|
917
|
+
const provider = c._activeProvider ?? c._engineConfig?.activeProvider;
|
|
918
|
+
if (typeof provider !== "string") return true;
|
|
919
|
+
return provider === "postgresql" || provider === "postgres";
|
|
920
|
+
}
|
|
921
|
+
function isTransactionUnsupported(error) {
|
|
922
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
923
|
+
return /transaction/i.test(message) && /not (?:be )?supported|unsupported|not available/i.test(message);
|
|
924
|
+
}
|
|
925
|
+
function tableNameFor(schemaClient, model) {
|
|
926
|
+
try {
|
|
927
|
+
const dbName = schemaClient?._runtimeDataModel?.models?.[model]?.dbName;
|
|
928
|
+
return typeof dbName === "string" && dbName.length > 0 ? dbName : model;
|
|
929
|
+
} catch {
|
|
930
|
+
return model;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
function recordPrismaOperation(ctx, result, table) {
|
|
737
934
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
738
935
|
if (!dbOperation) return;
|
|
739
936
|
if (isBatchResult(result)) {
|
|
740
937
|
emitDbEvent({
|
|
741
938
|
modelClass: ctx.model,
|
|
939
|
+
table,
|
|
742
940
|
operation: dbOperation,
|
|
743
941
|
resourceId: null,
|
|
744
942
|
// For batch updates we still capture the `args.data` shape so
|
|
@@ -755,16 +953,17 @@ function recordPrismaOperation(ctx, result) {
|
|
|
755
953
|
}
|
|
756
954
|
if (Array.isArray(result)) {
|
|
757
955
|
for (const row of result) {
|
|
758
|
-
emitSingleRowEvent(ctx, dbOperation, row);
|
|
956
|
+
emitSingleRowEvent(ctx, dbOperation, row, table);
|
|
759
957
|
}
|
|
760
958
|
return;
|
|
761
959
|
}
|
|
762
|
-
emitSingleRowEvent(ctx, dbOperation, result);
|
|
960
|
+
emitSingleRowEvent(ctx, dbOperation, result, table);
|
|
763
961
|
}
|
|
764
|
-
function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
962
|
+
function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
765
963
|
if (!isPlainObject2(row)) {
|
|
766
964
|
emitDbEvent({
|
|
767
965
|
modelClass: ctx.model,
|
|
966
|
+
table,
|
|
768
967
|
operation: dbOperation,
|
|
769
968
|
resourceId: null
|
|
770
969
|
});
|
|
@@ -776,9 +975,13 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
776
975
|
if (dbOperation === "create") {
|
|
777
976
|
emitDbEvent({
|
|
778
977
|
modelClass: ctx.model,
|
|
978
|
+
table,
|
|
779
979
|
operation: "create",
|
|
780
980
|
resourceId,
|
|
781
|
-
initialAttributes:
|
|
981
|
+
initialAttributes: {
|
|
982
|
+
...isPlainObject2(ctx.args.data) ? ctx.args.data : {},
|
|
983
|
+
...rowObj
|
|
984
|
+
},
|
|
782
985
|
displayName,
|
|
783
986
|
modelInstance: rowObj
|
|
784
987
|
});
|
|
@@ -787,6 +990,7 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
787
990
|
if (dbOperation === "update") {
|
|
788
991
|
emitDbEvent({
|
|
789
992
|
modelClass: ctx.model,
|
|
993
|
+
table,
|
|
790
994
|
operation: "update",
|
|
791
995
|
resourceId,
|
|
792
996
|
changes: extractUpdateChangesFromArgs(ctx, rowObj),
|
|
@@ -797,34 +1001,25 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
797
1001
|
}
|
|
798
1002
|
emitDbEvent({
|
|
799
1003
|
modelClass: ctx.model,
|
|
1004
|
+
table,
|
|
800
1005
|
operation: "destroy",
|
|
801
1006
|
resourceId,
|
|
1007
|
+
initialAttributes: rowObj,
|
|
802
1008
|
displayName,
|
|
803
1009
|
modelInstance: rowObj
|
|
804
1010
|
});
|
|
805
1011
|
}
|
|
806
|
-
function extractCreateAttributes(ctx, resultRow) {
|
|
807
|
-
if (isPlainObject2(ctx.args.data)) {
|
|
808
|
-
return ctx.args.data;
|
|
809
|
-
}
|
|
810
|
-
if (ctx.operation === "upsert" && isPlainObject2(ctx.args.create)) {
|
|
811
|
-
return ctx.args.create;
|
|
812
|
-
}
|
|
813
|
-
return resultRow;
|
|
814
|
-
}
|
|
815
1012
|
function extractUpdateChangesFromArgs(ctx, resultRow) {
|
|
816
1013
|
const data = isPlainObject2(ctx.args.data) ? ctx.args.data : ctx.operation === "upsert" && isPlainObject2(ctx.args.update) ? ctx.args.update : null;
|
|
817
1014
|
if (!data) return [];
|
|
818
1015
|
return Object.keys(data).map((attribute) => ({
|
|
819
1016
|
attribute,
|
|
820
|
-
from: null,
|
|
821
1017
|
to: resultRow[attribute] ?? data[attribute]
|
|
822
1018
|
}));
|
|
823
1019
|
}
|
|
824
1020
|
function extractChangesFromUpdateData(data) {
|
|
825
1021
|
return Object.keys(data).map((attribute) => ({
|
|
826
1022
|
attribute,
|
|
827
|
-
from: null,
|
|
828
1023
|
to: data[attribute]
|
|
829
1024
|
}));
|
|
830
1025
|
}
|
|
@@ -921,7 +1116,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
921
1116
|
return prisma;
|
|
922
1117
|
}
|
|
923
1118
|
try {
|
|
924
|
-
return client.$extends(
|
|
1119
|
+
return client.$extends(buildPrismaExtension(null, prisma));
|
|
925
1120
|
} catch (error) {
|
|
926
1121
|
logger.debug(`patchPrismaClient: $extends failed: ${describeError2(error)}`);
|
|
927
1122
|
return prisma;
|
|
@@ -1014,4 +1209,4 @@ function isLikelyModelDelegate(value) {
|
|
|
1014
1209
|
return typeof candidate.create === "function" && typeof candidate.findMany === "function";
|
|
1015
1210
|
}
|
|
1016
1211
|
|
|
1017
|
-
export { ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
|
1212
|
+
export { _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|