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