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.
Files changed (40) hide show
  1. package/README.md +106 -11
  2. package/bin/install-triggers.cjs +49 -11
  3. package/dist/actors/index.d.cts +1 -1
  4. package/dist/actors/index.d.ts +1 -1
  5. package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
  6. package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
  7. package/dist/db/drizzle.cjs +154 -18
  8. package/dist/db/drizzle.js +154 -18
  9. package/dist/db/prisma.cjs +242 -45
  10. package/dist/db/prisma.d.cts +39 -1
  11. package/dist/db/prisma.d.ts +39 -1
  12. package/dist/db/prisma.js +241 -46
  13. package/dist/db/supabase.cjs +154 -18
  14. package/dist/db/supabase.js +154 -18
  15. package/dist/index.cjs +761 -245
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.js +761 -245
  19. package/dist/jobs/bullmq.cjs +973 -16
  20. package/dist/jobs/bullmq.d.cts +8 -3
  21. package/dist/jobs/bullmq.d.ts +8 -3
  22. package/dist/jobs/bullmq.js +973 -16
  23. package/dist/jobs/inngest.cjs +880 -14
  24. package/dist/jobs/inngest.d.cts +7 -5
  25. package/dist/jobs/inngest.d.ts +7 -5
  26. package/dist/jobs/inngest.js +880 -14
  27. package/dist/jobs/supabase-queues.cjs +878 -16
  28. package/dist/jobs/supabase-queues.js +878 -16
  29. package/dist/jobs/trigger.cjs +914 -42
  30. package/dist/jobs/trigger.js +914 -42
  31. package/dist/jobs/upstash-workflow.cjs +914 -14
  32. package/dist/jobs/upstash-workflow.d.cts +16 -2
  33. package/dist/jobs/upstash-workflow.d.ts +16 -2
  34. package/dist/jobs/upstash-workflow.js +914 -15
  35. package/dist/triggers/index.cjs +405 -76
  36. package/dist/triggers/index.d.cts +36 -41
  37. package/dist/triggers/index.d.ts +36 -41
  38. package/dist/triggers/index.js +404 -74
  39. package/package.json +5 -1
  40. package/sql/postgres/00_install.sql +244 -21
@@ -116,7 +116,6 @@ var Configuration = class {
116
116
  displayNameFor = {};
117
117
  isServerActionError = null;
118
118
  databaseReader = null;
119
- databaseSnapshotMode = false;
120
119
  databaseUrl = null;
121
120
  /**
122
121
  * Tables that have the `ezlogs_audit` trigger attached. Detected once
@@ -168,9 +167,6 @@ var Configuration = class {
168
167
  if (options.databaseReader !== void 0) {
169
168
  this.databaseReader = options.databaseReader;
170
169
  }
171
- if (options.databaseSnapshotMode !== void 0) {
172
- this.databaseSnapshotMode = options.databaseSnapshotMode;
173
- }
174
170
  if (options.databaseUrl !== void 0) {
175
171
  this.databaseUrl = options.databaseUrl;
176
172
  }
@@ -359,11 +355,11 @@ async function runInstall(args, options = {}) {
359
355
  "\n-- --tables=auto: trigger attachment SQL is generated at install time from information_schema. Re-run without --print to apply, or pass --tables=<a,b,c> to use an explicit list."
360
356
  );
361
357
  } else {
362
- const trackingSql2 = buildTrackingSql(args.tables, "postgres");
363
- const fullSql2 = trackingSql2 ? `${installSql}
364
- ${trackingSql2}
358
+ const trackingSql = buildTrackingSql(args.tables, "postgres");
359
+ const fullSql = trackingSql ? `${installSql}
360
+ ${trackingSql}
365
361
  ` : installSql;
366
- out(fullSql2);
362
+ out(fullSql);
367
363
  }
368
364
  return 0;
369
365
  }
@@ -404,12 +400,11 @@ ${trackingSql2}
404
400
  return 1;
405
401
  }
406
402
  }
407
- const trackingSql = buildTrackingSql(tablesToTrack, "postgres");
408
- const fullSql = trackingSql ? `${installSql}
409
- ${trackingSql}
410
- ` : installSql;
411
403
  try {
412
- await options.exec(fullSql);
404
+ await options.exec(installSql);
405
+ for (const table of tablesToTrack) {
406
+ await options.exec(buildTrackingSql([table], "postgres"));
407
+ }
413
408
  if (tablesToTrack.length === 0) {
414
409
  out(
415
410
  "[ezlogs] Postgres triggers installed. No tables tracked yet \u2014 run `SELECT ezlogs_track('foo')` to attach the trigger, or re-run with --tables=auto to track every business table."
@@ -510,14 +505,34 @@ var storage = createScopedStorage(
510
505
  function getCurrentActor() {
511
506
  return storage.read();
512
507
  }
508
+ function runWithActorScope(callback) {
509
+ return storage.run(null, callback);
510
+ }
513
511
 
514
512
  // src/correlation.ts
515
513
  var storage2 = createScopedStorage(
516
514
  /* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
517
515
  );
516
+ function generate() {
517
+ const ts = Date.now();
518
+ const hex = randomHexBytes(4);
519
+ return `ezl_${ts}_${hex}`;
520
+ }
521
+ function randomHexBytes(byteCount) {
522
+ const buf = new Uint8Array(byteCount);
523
+ globalThis.crypto.getRandomValues(buf);
524
+ let out = "";
525
+ for (let i = 0; i < buf.length; i++) {
526
+ out += buf[i].toString(16).padStart(2, "0");
527
+ }
528
+ return out;
529
+ }
518
530
  function current() {
519
531
  return storage2.read();
520
532
  }
533
+ function runWithCorrelation(correlationId, callback) {
534
+ return storage2.run(correlationId, callback);
535
+ }
521
536
 
522
537
  // src/triggers/context.ts
523
538
  function getDatabaseContext() {
@@ -777,6 +792,57 @@ function describe2(error) {
777
792
  if (error instanceof Error) return `${error.name}: ${error.message}`;
778
793
  return String(error);
779
794
  }
795
+ var SENSITIVE_WORD_PATTERNS = Object.freeze([
796
+ "pin",
797
+ "otp",
798
+ "totp",
799
+ "iban",
800
+ "mfa",
801
+ "passphrase"
802
+ ]);
803
+ function snakeLower(key) {
804
+ return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
805
+ }
806
+ function matchesSensitiveKey(key, substringPatterns) {
807
+ const lower = key.toLowerCase();
808
+ const snake = snakeLower(key);
809
+ if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
810
+ return true;
811
+ }
812
+ const words = snake.split(/[^a-z0-9]+/);
813
+ return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
814
+ }
815
+ function passesLuhn(digits) {
816
+ let sum = 0;
817
+ for (let i = 0; i < digits.length; i += 1) {
818
+ let d = Number(digits[digits.length - 1 - i]);
819
+ if (i % 2 === 1) {
820
+ d *= 2;
821
+ if (d > 9) d -= 9;
822
+ }
823
+ sum += d;
824
+ }
825
+ return sum % 10 === 0;
826
+ }
827
+ var SECRET_TEXT_PATTERNS = [
828
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
829
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
830
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
831
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
832
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
833
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
834
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
835
+ ];
836
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
837
+ function scrubSecretsInText(text, options = {}) {
838
+ let out = text;
839
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
840
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
841
+ const digits = match.replace(/[ -]/g, "");
842
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
843
+ });
844
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
845
+ }
780
846
 
781
847
  // src/db/shared.ts
782
848
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -785,8 +851,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
785
851
  "created_at",
786
852
  "updatedAt",
787
853
  "updated_at",
788
- "deletedAt",
789
- "deleted_at",
790
854
  // Auth.js / NextAuth standard columns
791
855
  "emailVerified",
792
856
  "expires"
@@ -809,7 +873,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
809
873
  "salt",
810
874
  "digest",
811
875
  "signature",
812
- "hmac"
876
+ "hmac",
877
+ // 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
878
+ // the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
879
+ // `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
880
+ // SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
881
+ "credential",
882
+ "passwd",
883
+ "passcode",
884
+ "apikey",
885
+ "card_number",
886
+ "cardnumber",
887
+ "cvv",
888
+ "cvc"
813
889
  ]);
814
890
  function emitDbEvent(input) {
815
891
  try {
@@ -818,14 +894,17 @@ function emitDbEvent(input) {
818
894
  if (config.allExcludedTables().includes(input.modelClass)) return;
819
895
  if (config.databaseReader && !input._fromTrigger) {
820
896
  const tracked = config.triggerTrackedTables;
821
- if (tracked === null || tracked.has(input.modelClass)) return;
897
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
822
898
  }
823
899
  if (input.operation === "update") {
824
900
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
825
901
  if (meaningful.length === 0) return;
826
902
  }
827
903
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
828
- const context = buildContext(input);
904
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
905
+ const context = buildContext(
906
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
907
+ );
829
908
  const event = build({
830
909
  source_type: "database_callback",
831
910
  source_data: {
@@ -834,9 +913,10 @@ function emitDbEvent(input) {
834
913
  },
835
914
  outcome: "success",
836
915
  correlation_id: current(),
837
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
916
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
838
917
  context,
839
- duration_ms: null
918
+ duration_ms: null,
919
+ timestamp: input.timestamp ?? null
840
920
  });
841
921
  pushEvent(event);
842
922
  } catch (error) {
@@ -880,7 +960,7 @@ function filterMeaningfulAttributes(attrs) {
880
960
  return Object.keys(out).length === 0 ? null : out;
881
961
  }
882
962
  function filterMeaningfulChanges(changes) {
883
- return changes.filter((c) => {
963
+ return flattenNestedChanges(changes).filter((c) => {
884
964
  if (!isMeaningfulAttribute(c.attribute)) return false;
885
965
  if (!isScalar(c.to)) return false;
886
966
  if (c.from === void 0) return true;
@@ -888,14 +968,67 @@ function filterMeaningfulChanges(changes) {
888
968
  return valuesActuallyChanged(c.from, c.to);
889
969
  });
890
970
  }
971
+ var MAX_NESTED_DEPTH = 3;
972
+ var MAX_NESTED_LEAVES = 20;
973
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
974
+ function flattenNestedChanges(changes) {
975
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
976
+ const out = [];
977
+ for (const c of changes) {
978
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
979
+ out.push(c);
980
+ continue;
981
+ }
982
+ if (!isMeaningfulAttribute(c.attribute)) continue;
983
+ const leaves = [];
984
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
985
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
986
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
987
+ }
988
+ return out;
989
+ }
990
+ function collectLeafChanges(path, from, to, depth, out) {
991
+ if (out.length > MAX_NESTED_LEAVES) return;
992
+ const fromObj = isPlainRecord(from);
993
+ const toObj = isPlainRecord(to);
994
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
995
+ const f = fromObj ? from : {};
996
+ const t = toObj ? to : {};
997
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
998
+ const before = from === void 0 ? void 0 : f[key] ?? null;
999
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
1000
+ }
1001
+ return;
1002
+ }
1003
+ if (from !== void 0 && sameJson(from, to)) return;
1004
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
1005
+ }
1006
+ function scrubLeaf(change) {
1007
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
1008
+ const out = { attribute: change.attribute, to: scrub(change.to) };
1009
+ if (change.from !== void 0) out.from = scrub(change.from);
1010
+ return out;
1011
+ }
1012
+ function isPlainRecord(value) {
1013
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
1014
+ const proto = Object.getPrototypeOf(value);
1015
+ return proto === Object.prototype || proto === null;
1016
+ }
1017
+ function sameJson(a, b) {
1018
+ if (a === b) return true;
1019
+ try {
1020
+ return JSON.stringify(a) === JSON.stringify(b);
1021
+ } catch {
1022
+ return false;
1023
+ }
1024
+ }
891
1025
  function isMeaningfulAttribute(name) {
892
1026
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
893
1027
  if (isDbSensitive(name)) return false;
894
1028
  return true;
895
1029
  }
896
1030
  function isDbSensitive(name) {
897
- const lower = name.toLowerCase();
898
- return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
1031
+ return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
899
1032
  }
900
1033
  function isScalar(value) {
901
1034
  if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
@@ -926,22 +1059,44 @@ function isPrismaDecimal(value) {
926
1059
  const obj = value;
927
1060
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
928
1061
  }
1062
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
1063
+ var MAX_DISPLAY_NAME_LENGTH = 120;
929
1064
  function resolveDisplayName(modelClass, instance) {
930
1065
  try {
931
1066
  const config = configuration();
932
1067
  const resolver = config.displayNameFor[modelClass];
933
1068
  const resolved = applyResolver(resolver, instance);
934
1069
  if (resolved !== null) return resolved;
935
- for (const field of ["name", "title", "number"]) {
936
- const value = instance[field];
937
- if (value !== null && value !== void 0 && String(value).length > 0) {
938
- return String(value);
1070
+ const columns = /* @__PURE__ */ new Map();
1071
+ for (const key of Object.keys(instance)) {
1072
+ if (isDbSensitive(key)) continue;
1073
+ const norm = key.toLowerCase().replace(/_/g, "");
1074
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
1075
+ }
1076
+ const text = (norm) => {
1077
+ const value = columns.get(norm);
1078
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
1079
+ return null;
939
1080
  }
1081
+ const str = String(value).trim();
1082
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
1083
+ };
1084
+ for (const column of DISPLAY_NAME_COLUMNS) {
1085
+ const value = text(column);
1086
+ if (value !== null) return value;
940
1087
  }
1088
+ return text(`${singularNorm(modelClass)}number`);
941
1089
  } catch {
942
1090
  }
943
1091
  return null;
944
1092
  }
1093
+ function singularNorm(modelClass) {
1094
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
1095
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
1096
+ if (/(ss|us|is)$/.test(norm)) return norm;
1097
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
1098
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
1099
+ }
945
1100
  function applyResolver(resolver, instance) {
946
1101
  if (typeof resolver === "string") {
947
1102
  const value = instance[resolver];
@@ -967,43 +1122,53 @@ function describeError(error) {
967
1122
  return String(error);
968
1123
  }
969
1124
 
970
- // src/triggers/audit-snapshot.ts
971
- var storage3 = createScopedStorage(
972
- /* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-snapshot-storage")
973
- );
974
- function currentAuditSnapshot() {
975
- return storage3.read();
976
- }
977
- function setAuditSnapshot(value) {
978
- storage3.update(value);
979
- }
980
- function runWithAuditSnapshotScope(callback) {
981
- return storage3.run(0n, callback);
982
- }
983
-
984
1125
  // src/triggers/emit-dedup.ts
985
- var storage4 = createScopedStorage(
1126
+ var storage3 = createScopedStorage(
986
1127
  /* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-emitted-ids-storage")
987
1128
  );
988
1129
  function currentEmittedAuditIds() {
989
- return storage4.read();
1130
+ return storage3.read();
990
1131
  }
991
1132
 
992
1133
  // src/triggers/reader.ts
993
- async function readAuditRows(exec, correlationId, snapshotMaxId = null) {
1134
+ var COLUMNS = "id, table_name, op, old_row, new_row, resource_id, correlation_id, txid, captured_at";
1135
+ var ORPHAN_AGE = "5 minutes";
1136
+ var PRUNE_AGE = "24 hours";
1137
+ var SWEEP_BATCH = 100;
1138
+ var PRUNE_BATCH = 2e3;
1139
+ var SWEEP_INTERVAL_MS = 1e4;
1140
+ var PRUNE_INTERVAL_MS = 10 * 6e4;
1141
+ var lastSweepAt = 0;
1142
+ var lastPruneAt = 0;
1143
+ var deleteDenied = false;
1144
+ var txCorrelation = /* @__PURE__ */ new Map();
1145
+ async function readAuditRows(exec, correlationId, since = null) {
1146
+ const where = since ? "correlation_id = $1 AND captured_at >= $2" : "correlation_id = $1";
1147
+ const params = since ? [correlationId, since.toISOString()] : [correlationId];
994
1148
  try {
995
- const sql = snapshotMaxId === null ? `SELECT id, table_name, op, old_row, new_row, resource_id
996
- FROM ezlogs_audit_log
997
- WHERE correlation_id = $1
998
- ORDER BY id ASC` : `SELECT id, table_name, op, old_row, new_row, resource_id
999
- FROM ezlogs_audit_log
1000
- WHERE correlation_id = $1
1001
- OR (correlation_id IS NULL AND id > $2)
1002
- ORDER BY id ASC`;
1003
- const params = snapshotMaxId === null ? [correlationId] : [correlationId, snapshotMaxId.toString()];
1004
- const result = await exec(sql, params);
1005
- const rows = extractRows(result);
1006
- return rows.map((r) => normalizeAuditRow(r));
1149
+ if (!deleteDenied) {
1150
+ try {
1151
+ return sortById(
1152
+ extractRows(
1153
+ await exec(
1154
+ `DELETE FROM ezlogs_audit_log WHERE ${where} RETURNING ${COLUMNS}`,
1155
+ params
1156
+ )
1157
+ ).map(normalizeAuditRow)
1158
+ );
1159
+ } catch (err) {
1160
+ if (!isPermissionDenied(err)) throw err;
1161
+ deleteDenied = true;
1162
+ logger.warn(
1163
+ "the database role used by the agent cannot DELETE from ezlogs_audit_log. Rows will not be cleaned up after they are sent; grant DELETE or schedule `DELETE FROM ezlogs_audit_log WHERE captured_at < now() - interval '24 hours'`."
1164
+ );
1165
+ }
1166
+ }
1167
+ const result = await exec(
1168
+ `SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE ${where} ORDER BY id ASC`,
1169
+ params
1170
+ );
1171
+ return extractRows(result).map(normalizeAuditRow);
1007
1172
  } catch (err) {
1008
1173
  logger.debug(
1009
1174
  `readAuditRows failed: ${err instanceof Error ? err.message : String(err)}`
@@ -1011,21 +1176,58 @@ async function readAuditRows(exec, correlationId, snapshotMaxId = null) {
1011
1176
  return [];
1012
1177
  }
1013
1178
  }
1014
- async function takeAuditSnapshot(exec) {
1179
+ async function sweepAuditLog(exec) {
1180
+ if (deleteDenied) return 0;
1015
1181
  try {
1016
- const result = await exec(
1017
- `SELECT COALESCE(MAX(id), 0) AS max_id FROM ezlogs_audit_log`,
1182
+ const rows = sortById(
1183
+ extractRows(
1184
+ await exec(
1185
+ `DELETE FROM ezlogs_audit_log
1186
+ WHERE id IN (
1187
+ SELECT id FROM ezlogs_audit_log
1188
+ WHERE (correlation_id IS NULL
1189
+ OR captured_at < now() - interval '${ORPHAN_AGE}')
1190
+ -- Never resurface history: past the retention cap a
1191
+ -- row is pruned, not sent as new activity.
1192
+ AND captured_at > now() - interval '${PRUNE_AGE}'
1193
+ ORDER BY id
1194
+ LIMIT ${SWEEP_BATCH}
1195
+ FOR UPDATE SKIP LOCKED)
1196
+ RETURNING ${COLUMNS}`,
1197
+ []
1198
+ )
1199
+ ).map(normalizeAuditRow)
1200
+ );
1201
+ await loadTableMeta(exec, rows);
1202
+ for (const row of rows) {
1203
+ const correlationId = row.correlation_id ?? correlationForTx(row.txid);
1204
+ runWithCorrelation(
1205
+ correlationId,
1206
+ () => runWithActorScope(() => emitOne(row))
1207
+ );
1208
+ }
1209
+ return rows.length;
1210
+ } catch (err) {
1211
+ logger.debug(
1212
+ `sweepAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
1213
+ );
1214
+ return 0;
1215
+ }
1216
+ }
1217
+ async function pruneAuditLog(exec) {
1218
+ if (deleteDenied) return;
1219
+ try {
1220
+ await exec(
1221
+ `DELETE FROM ezlogs_audit_log
1222
+ WHERE id IN (
1223
+ SELECT id FROM ezlogs_audit_log
1224
+ WHERE captured_at < now() - interval '${PRUNE_AGE}'
1225
+ LIMIT ${PRUNE_BATCH})`,
1018
1226
  []
1019
1227
  );
1020
- const rows = extractRows(result);
1021
- if (rows.length === 0) return;
1022
- const raw = rows[0] ?? {};
1023
- const value = typeof raw.max_id === "bigint" ? raw.max_id : typeof raw.max_id === "number" ? BigInt(raw.max_id) : typeof raw.max_id === "string" && /^\d+$/.test(raw.max_id) ? BigInt(raw.max_id) : null;
1024
- if (value === null) return;
1025
- setAuditSnapshot(value);
1026
1228
  } catch (err) {
1027
1229
  logger.debug(
1028
- `takeAuditSnapshot failed: ${err instanceof Error ? err.message : String(err)}`
1230
+ `pruneAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
1029
1231
  );
1030
1232
  }
1031
1233
  }
@@ -1059,12 +1261,97 @@ function emitAuditRows(rows) {
1059
1261
  emitOne(row);
1060
1262
  }
1061
1263
  }
1062
- async function flushAuditLog(exec) {
1264
+ async function flushAuditLog(exec, options = {}) {
1063
1265
  if (!exec) return;
1064
1266
  const correlationId = current();
1065
1267
  if (!correlationId) return;
1066
- const snapshot = currentAuditSnapshot();
1067
- const rows = await readAuditRows(exec, correlationId, snapshot);
1268
+ const rows = await readAuditRows(exec, correlationId, options.since ?? null);
1269
+ await loadTableMeta(exec, rows);
1270
+ emitFresh(rows);
1271
+ const now = Date.now();
1272
+ if (now - lastPruneAt >= PRUNE_INTERVAL_MS) {
1273
+ lastPruneAt = now;
1274
+ await pruneAuditLog(exec);
1275
+ }
1276
+ if (now - lastSweepAt >= SWEEP_INTERVAL_MS) {
1277
+ lastSweepAt = now;
1278
+ await sweepAuditLog(exec);
1279
+ }
1280
+ }
1281
+ var tableMeta = /* @__PURE__ */ new Map();
1282
+ var META_RETRY_MS = 6e4;
1283
+ var metaRetryAt = 0;
1284
+ async function loadTableMeta(exec, rows) {
1285
+ if (Date.now() < metaRetryAt) return;
1286
+ const tables = [
1287
+ ...new Set(rows.map((r) => r.table_name).filter((t) => t !== "" && !t.includes(",") && !tableMeta.has(t)))
1288
+ ];
1289
+ if (tables.length === 0) return;
1290
+ const list = tables.join(",");
1291
+ try {
1292
+ const fkRows = extractRows(
1293
+ await exec(
1294
+ `SELECT cl.relname AS table_name, a.attname AS column_name, rf.relname AS ref_table
1295
+ FROM pg_constraint c
1296
+ JOIN pg_class cl ON cl.oid = c.conrelid
1297
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1298
+ JOIN pg_class rf ON rf.oid = c.confrelid
1299
+ JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
1300
+ WHERE c.contype = 'f'
1301
+ AND n.nspname = 'public'
1302
+ AND array_length(c.conkey, 1) = 1
1303
+ AND cl.relname = ANY(string_to_array($1, ','))
1304
+ ORDER BY cl.relname, a.attnum`,
1305
+ [list]
1306
+ )
1307
+ );
1308
+ const tsRows = extractRows(
1309
+ await exec(
1310
+ `SELECT cl.relname AS table_name, a.attname AS column_name
1311
+ FROM pg_attribute a
1312
+ JOIN pg_class cl ON cl.oid = a.attrelid
1313
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1314
+ WHERE n.nspname = 'public'
1315
+ AND cl.relname = ANY(string_to_array($1, ','))
1316
+ AND a.attnum > 0
1317
+ AND NOT a.attisdropped
1318
+ AND a.atttypid = 'timestamp'::regtype`,
1319
+ [list]
1320
+ )
1321
+ );
1322
+ const fks = /* @__PURE__ */ new Map();
1323
+ for (const raw of fkRows) {
1324
+ const r = raw ?? {};
1325
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string" || typeof r.ref_table !== "string") {
1326
+ continue;
1327
+ }
1328
+ fks.set(r.table_name, [...fks.get(r.table_name) ?? [], { column: r.column_name, refTable: r.ref_table }]);
1329
+ }
1330
+ const timestamps = /* @__PURE__ */ new Map();
1331
+ for (const raw of tsRows) {
1332
+ const r = raw ?? {};
1333
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string") continue;
1334
+ timestamps.set(r.table_name, (timestamps.get(r.table_name) ?? /* @__PURE__ */ new Set()).add(r.column_name));
1335
+ }
1336
+ for (const table of tables) {
1337
+ tableMeta.set(table, { fks: fks.get(table) ?? [], naiveTimestamps: timestamps.get(table) ?? /* @__PURE__ */ new Set() });
1338
+ }
1339
+ } catch (err) {
1340
+ metaRetryAt = Date.now() + META_RETRY_MS;
1341
+ logger.debug(`table metadata lookup failed: ${err instanceof Error ? err.message : String(err)}`);
1342
+ }
1343
+ }
1344
+ function relatedResourceIds(table, row) {
1345
+ const out = [];
1346
+ for (const fk of tableMeta.get(table)?.fks ?? []) {
1347
+ const value = row[fk.column];
1348
+ if (typeof value === "string" || typeof value === "number") {
1349
+ out.push({ resource_type: fk.refTable, resource_id: String(value) });
1350
+ }
1351
+ }
1352
+ return out;
1353
+ }
1354
+ function emitFresh(rows) {
1068
1355
  if (rows.length === 0) return;
1069
1356
  const emitted = currentEmittedAuditIds();
1070
1357
  if (emitted === null) {
@@ -1077,14 +1364,33 @@ async function flushAuditLog(exec) {
1077
1364
  emitted.add(row.id);
1078
1365
  fresh.push(row);
1079
1366
  }
1080
- if (fresh.length === 0) return;
1081
1367
  emitAuditRows(fresh);
1082
1368
  }
1369
+ function correlationForTx(txid) {
1370
+ if (!txid) return generate();
1371
+ let id = txCorrelation.get(txid);
1372
+ if (!id) {
1373
+ if (txCorrelation.size >= 1e3) txCorrelation.clear();
1374
+ id = generate();
1375
+ txCorrelation.set(txid, id);
1376
+ }
1377
+ return id;
1378
+ }
1379
+ function isPermissionDenied(err) {
1380
+ return err?.code === "42501";
1381
+ }
1382
+ function sortById(rows) {
1383
+ return rows.sort(
1384
+ (a, b) => a.id.length !== b.id.length ? a.id.length - b.id.length : a.id < b.id ? -1 : a.id > b.id ? 1 : 0
1385
+ );
1386
+ }
1083
1387
  function emitOne(row) {
1084
1388
  const op = mapOp(row.op);
1085
1389
  if (!op) return;
1086
- const oldRow = coerceJsonb(row.old_row);
1087
- const newRow = coerceJsonb(row.new_row);
1390
+ const naive = tableMeta.get(row.table_name)?.naiveTimestamps ?? null;
1391
+ const oldRow = withUtcTimestamps(coerceJsonb(row.old_row), naive);
1392
+ const newRow = withUtcTimestamps(coerceJsonb(row.new_row), naive);
1393
+ const related = row.resource_id === null ? relatedResourceIds(row.table_name, newRow ?? oldRow ?? {}) : null;
1088
1394
  if (op === "create" && newRow) {
1089
1395
  emitDbEvent({
1090
1396
  modelClass: row.table_name,
@@ -1093,6 +1399,8 @@ function emitOne(row) {
1093
1399
  initialAttributes: newRow,
1094
1400
  modelInstance: newRow,
1095
1401
  displayName: null,
1402
+ timestamp: row.captured_at ?? null,
1403
+ relatedResourceIds: related,
1096
1404
  _fromTrigger: true
1097
1405
  });
1098
1406
  return;
@@ -1111,6 +1419,8 @@ function emitOne(row) {
1111
1419
  // user just told the system to call this row.
1112
1420
  modelInstance: newRow,
1113
1421
  displayName: null,
1422
+ timestamp: row.captured_at ?? null,
1423
+ relatedResourceIds: related,
1114
1424
  _fromTrigger: true
1115
1425
  });
1116
1426
  return;
@@ -1123,11 +1433,23 @@ function emitOne(row) {
1123
1433
  initialAttributes: oldRow,
1124
1434
  modelInstance: oldRow,
1125
1435
  displayName: null,
1436
+ timestamp: row.captured_at ?? null,
1437
+ relatedResourceIds: related,
1126
1438
  _fromTrigger: true
1127
1439
  });
1128
1440
  return;
1129
1441
  }
1130
1442
  }
1443
+ var NAIVE_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?$/;
1444
+ function withUtcTimestamps(row, naiveColumns) {
1445
+ if (!row) return row;
1446
+ const out = {};
1447
+ for (const [key, value] of Object.entries(row)) {
1448
+ const isNaiveColumn = naiveColumns ? naiveColumns.has(key) : /(_at|At)$/.test(key);
1449
+ out[key] = isNaiveColumn && typeof value === "string" && NAIVE_TIMESTAMP.test(value) ? `${value}Z` : value;
1450
+ }
1451
+ return out;
1452
+ }
1131
1453
  function mapOp(op) {
1132
1454
  if (op === "INSERT") return "create";
1133
1455
  if (op === "UPDATE") return "update";
@@ -1171,6 +1493,11 @@ function extractRows(result) {
1171
1493
  }
1172
1494
  return [];
1173
1495
  }
1496
+ function toDate(value) {
1497
+ if (value == null) return null;
1498
+ const d = value instanceof Date ? value : new Date(String(value));
1499
+ return Number.isNaN(d.getTime()) ? null : d;
1500
+ }
1174
1501
  function normalizeAuditRow(raw) {
1175
1502
  const r = raw ?? {};
1176
1503
  return {
@@ -1179,7 +1506,10 @@ function normalizeAuditRow(raw) {
1179
1506
  op: r.op,
1180
1507
  old_row: r.old_row ?? null,
1181
1508
  new_row: r.new_row ?? null,
1182
- resource_id: r.resource_id == null ? null : String(r.resource_id)
1509
+ resource_id: r.resource_id == null ? null : String(r.resource_id),
1510
+ correlation_id: r.correlation_id == null ? null : String(r.correlation_id),
1511
+ txid: r.txid == null ? null : String(r.txid),
1512
+ captured_at: toDate(r.captured_at)
1183
1513
  };
1184
1514
  }
1185
1515
  function coerceJsonb(value) {
@@ -1374,7 +1704,6 @@ function leadingVerb(text) {
1374
1704
 
1375
1705
  exports.applyDatabaseContext = applyDatabaseContext;
1376
1706
  exports.buildTrackingSql = buildTrackingSql;
1377
- exports.currentAuditSnapshot = currentAuditSnapshot;
1378
1707
  exports.detectEngine = detectEngine;
1379
1708
  exports.detectTriggerTrackedTables = detectTriggerTrackedTables;
1380
1709
  exports.emitAuditRows = emitAuditRows;
@@ -1382,8 +1711,8 @@ exports.flushAuditLog = flushAuditLog;
1382
1711
  exports.getDatabaseContext = getDatabaseContext;
1383
1712
  exports.parseArgs = parseArgs;
1384
1713
  exports.patchPgClient = patchPgClient;
1714
+ exports.pruneAuditLog = pruneAuditLog;
1385
1715
  exports.readAuditRows = readAuditRows;
1386
1716
  exports.runInstall = runInstall;
1387
- exports.runWithAuditSnapshotScope = runWithAuditSnapshotScope;
1388
- exports.takeAuditSnapshot = takeAuditSnapshot;
1717
+ exports.sweepAuditLog = sweepAuditLog;
1389
1718
  exports.withDatabaseContext = withDatabaseContext;