relational-audit-kit 0.0.0-stage → 1.0.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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -2
  3. package/dist/cli/aggregate.d.ts +3 -0
  4. package/dist/cli/aggregate.d.ts.map +1 -0
  5. package/dist/cli/aggregate.js +36 -0
  6. package/dist/cli/aggregate.js.map +1 -0
  7. package/dist/cli/run.d.ts +3 -0
  8. package/dist/cli/run.d.ts.map +1 -0
  9. package/dist/cli/run.js +65 -0
  10. package/dist/cli/run.js.map +1 -0
  11. package/dist/cli/setup.d.ts +3 -0
  12. package/dist/cli/setup.d.ts.map +1 -0
  13. package/dist/cli/setup.js +48 -0
  14. package/dist/cli/setup.js.map +1 -0
  15. package/dist/core/aggregate-audit.d.ts +7 -0
  16. package/dist/core/aggregate-audit.d.ts.map +1 -0
  17. package/dist/core/aggregate-audit.js +155 -0
  18. package/dist/core/aggregate-audit.js.map +1 -0
  19. package/dist/core/schema-loader.d.ts +4 -0
  20. package/dist/core/schema-loader.d.ts.map +1 -0
  21. package/dist/core/schema-loader.js +93 -0
  22. package/dist/core/schema-loader.js.map +1 -0
  23. package/dist/core/setup-audit.d.ts +5 -0
  24. package/dist/core/setup-audit.d.ts.map +1 -0
  25. package/dist/core/setup-audit.js +238 -0
  26. package/dist/core/setup-audit.js.map +1 -0
  27. package/dist/core/sql-template.d.ts +4 -0
  28. package/dist/core/sql-template.d.ts.map +1 -0
  29. package/dist/core/sql-template.js +16 -0
  30. package/dist/core/sql-template.js.map +1 -0
  31. package/dist/core/start-audit.d.ts +6 -0
  32. package/dist/core/start-audit.d.ts.map +1 -0
  33. package/dist/core/start-audit.js +46 -0
  34. package/dist/core/start-audit.js.map +1 -0
  35. package/dist/core/trigger-builder.d.ts +5 -0
  36. package/dist/core/trigger-builder.d.ts.map +1 -0
  37. package/dist/core/trigger-builder.js +158 -0
  38. package/dist/core/trigger-builder.js.map +1 -0
  39. package/dist/core/types.d.ts +55 -0
  40. package/dist/core/types.d.ts.map +1 -0
  41. package/dist/core/types.js +2 -0
  42. package/dist/core/types.js.map +1 -0
  43. package/dist/index.d.ts +5 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +4 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/sql/aggregated-log.sql +195 -0
  48. package/dist/sql/audit-log.sql +51 -0
  49. package/package.json +51 -4
@@ -0,0 +1,195 @@
1
+ -- ============================================================
2
+ -- Aggregated audit log.
3
+ -- One row = one group (transaction_id, cluster_name, cluster_row_key).
4
+ -- Filled by audit.aggregate_audit_log(), called by
5
+ -- src/aggregate-audit.ts on a schedule.
6
+ --
7
+ -- changes format:
8
+ -- {
9
+ -- "<table_name>": [
10
+ -- {
11
+ -- "row_key": {"id": 5},
12
+ -- "fields": [
13
+ -- {"field_name": "email", "old_value": "a", "new_value": "c"},
14
+ -- ...
15
+ -- ]
16
+ -- },
17
+ -- ...
18
+ -- ],
19
+ -- ...
20
+ -- }
21
+ --
22
+ -- Multiple operations on the same row within one transaction are
23
+ -- merged into a single entry. Per field we keep only the FIRST
24
+ -- old_value and the LAST new_value (net effect of the transaction).
25
+ -- Fields that net out to no change (first_old == last_new) are dropped.
26
+ -- ============================================================
27
+
28
+ CREATE SCHEMA IF NOT EXISTS __AUDIT_SCHEMA__;
29
+
30
+ CREATE TABLE IF NOT EXISTS __AUDIT_SCHEMA__.audit_log_aggregated (
31
+ id BIGSERIAL PRIMARY KEY,
32
+ transaction_id BIGINT, -- database transaction id (atomic batch)
33
+ cluster_name TEXT NOT NULL, -- cluster name from audit-config.json
34
+ cluster_row_key JSONB, -- PK of the level-1 entity
35
+ "timestamp" TIMESTAMPTZ NOT NULL, -- moment of change (min per group)
36
+ changes JSONB NOT NULL, -- see format above
37
+ aggregated_at TIMESTAMPTZ NOT NULL DEFAULT now()
38
+ );
39
+
40
+ -- Grouping key: one aggregated row per (tx, cluster, entity).
41
+ CREATE UNIQUE INDEX IF NOT EXISTS uq_audit_log_aggregated_tx_cluster
42
+ ON __AUDIT_SCHEMA__.audit_log_aggregated (transaction_id, cluster_name, cluster_row_key);
43
+
44
+ CREATE INDEX IF NOT EXISTS idx_audit_log_aggregated_tx
45
+ ON __AUDIT_SCHEMA__.audit_log_aggregated (transaction_id);
46
+ CREATE INDEX IF NOT EXISTS idx_audit_log_aggregated_cluster
47
+ ON __AUDIT_SCHEMA__.audit_log_aggregated (cluster_name, cluster_row_key);
48
+ CREATE INDEX IF NOT EXISTS idx_audit_log_aggregated_time
49
+ ON __AUDIT_SCHEMA__.audit_log_aggregated ("timestamp");
50
+ CREATE INDEX IF NOT EXISTS idx_audit_log_aggregated_changes_gin
51
+ ON __AUDIT_SCHEMA__.audit_log_aggregated USING GIN (changes);
52
+
53
+ -- ============================================================
54
+ -- audit.aggregate_audit_log()
55
+ -- Collapses audit_log by (transaction_id, cluster_name, cluster_row_key)
56
+ -- and deletes the processed rows. One call = one transaction.
57
+ -- Returns (aggregated, deleted) for logging.
58
+ -- ============================================================
59
+ CREATE OR REPLACE FUNCTION __AUDIT_SCHEMA__.aggregate_audit_log()
60
+ RETURNS TABLE(aggregated BIGINT, deleted BIGINT)
61
+ LANGUAGE plpgsql
62
+ SET search_path = pg_catalog, pg_temp
63
+ AS $$
64
+ DECLARE
65
+ v_aggregated BIGINT;
66
+ v_deleted BIGINT;
67
+ BEGIN
68
+ -- Serialize workers that aggregate the same audit schema. This also
69
+ -- makes NULL cluster keys safe on PostgreSQL versions before 15.
70
+ PERFORM pg_advisory_xact_lock(
71
+ hashtext('postgres-audit-kit'), hashtext('__AUDIT_SCHEMA__')
72
+ );
73
+
74
+ WITH per_field AS (
75
+ -- One row per (tx, cluster, cluster_key, table, row_key, column).
76
+ -- operation_id grows monotonically per row-change within a
77
+ -- transaction, so ORDER BY operation_id reflects chronological order.
78
+ SELECT
79
+ al.transaction_id,
80
+ al.cluster_name,
81
+ al.cluster_row_key,
82
+ al.table_name,
83
+ al.row_key,
84
+ al.column_name,
85
+ (array_agg(al.old_value ORDER BY al.operation_id ASC))[1] AS first_old_value,
86
+ (array_agg(al.new_value ORDER BY al.operation_id DESC))[1] AS last_new_value
87
+ FROM __AUDIT_SCHEMA__.audit_log al
88
+ WHERE al.transaction_id IS NOT NULL
89
+ GROUP BY
90
+ al.transaction_id, al.cluster_name, al.cluster_row_key,
91
+ al.table_name, al.row_key, al.column_name
92
+ ),
93
+ changed_fields AS (
94
+ -- Drop fields whose net effect is "no change".
95
+ SELECT * FROM per_field
96
+ WHERE first_old_value IS DISTINCT FROM last_new_value
97
+ ),
98
+ per_row AS (
99
+ SELECT
100
+ transaction_id, cluster_name, cluster_row_key, table_name, row_key,
101
+ jsonb_agg(
102
+ jsonb_build_object(
103
+ 'field_name', column_name,
104
+ 'old_value', first_old_value,
105
+ 'new_value', last_new_value
106
+ )
107
+ ORDER BY column_name
108
+ ) AS fields
109
+ FROM changed_fields
110
+ GROUP BY transaction_id, cluster_name, cluster_row_key, table_name, row_key
111
+ ),
112
+ per_table AS (
113
+ SELECT
114
+ transaction_id, cluster_name, cluster_row_key, table_name,
115
+ jsonb_agg(
116
+ jsonb_build_object(
117
+ 'row_key', row_key,
118
+ 'fields', fields
119
+ )
120
+ ORDER BY row_key::text
121
+ ) AS entries
122
+ FROM per_row
123
+ GROUP BY transaction_id, cluster_name, cluster_row_key, table_name
124
+ ),
125
+ changes_by_group AS (
126
+ SELECT
127
+ transaction_id, cluster_name, cluster_row_key,
128
+ jsonb_object_agg(table_name, entries) AS changes
129
+ FROM per_table
130
+ GROUP BY transaction_id, cluster_name, cluster_row_key
131
+ ),
132
+ groups AS (
133
+ SELECT
134
+ transaction_id, cluster_name, cluster_row_key,
135
+ min("timestamp") AS timestamp,
136
+ array_agg(DISTINCT operation_id) AS operation_ids
137
+ FROM __AUDIT_SCHEMA__.audit_log
138
+ WHERE transaction_id IS NOT NULL
139
+ GROUP BY transaction_id, cluster_name, cluster_row_key
140
+ ),
141
+ built AS (
142
+ SELECT
143
+ g.transaction_id,
144
+ g.cluster_name,
145
+ g.cluster_row_key,
146
+ g.timestamp,
147
+ COALESCE(c.changes, '{}'::jsonb) AS changes,
148
+ g.operation_ids
149
+ FROM groups g
150
+ LEFT JOIN changes_by_group c
151
+ ON c.transaction_id = g.transaction_id
152
+ AND c.cluster_name = g.cluster_name
153
+ AND c.cluster_row_key IS NOT DISTINCT FROM g.cluster_row_key
154
+ ),
155
+ inserted AS (
156
+ INSERT INTO __AUDIT_SCHEMA__.audit_log_aggregated
157
+ (transaction_id, cluster_name, cluster_row_key, "timestamp", changes)
158
+ SELECT transaction_id, cluster_name, cluster_row_key, timestamp, changes
159
+ FROM built
160
+ WHERE changes <> '{}'::jsonb
161
+ ON CONFLICT (transaction_id, cluster_name, cluster_row_key) DO NOTHING
162
+ RETURNING transaction_id, cluster_name, cluster_row_key
163
+ ),
164
+ no_op_groups AS (
165
+ SELECT transaction_id, cluster_name, cluster_row_key, operation_ids
166
+ FROM built
167
+ WHERE changes = '{}'::jsonb
168
+ ),
169
+ processed_groups AS (
170
+ SELECT g.transaction_id, g.cluster_name, g.cluster_row_key, g.operation_ids
171
+ FROM groups g
172
+ JOIN inserted i
173
+ ON i.transaction_id = g.transaction_id
174
+ AND i.cluster_name = g.cluster_name
175
+ AND i.cluster_row_key IS NOT DISTINCT FROM g.cluster_row_key
176
+ UNION ALL
177
+ SELECT transaction_id, cluster_name, cluster_row_key, operation_ids
178
+ FROM no_op_groups
179
+ ),
180
+ deleted AS (
181
+ DELETE FROM __AUDIT_SCHEMA__.audit_log
182
+ WHERE operation_id IN (
183
+ SELECT unnest(p.operation_ids)
184
+ FROM processed_groups p
185
+ )
186
+ RETURNING 1
187
+ )
188
+ SELECT
189
+ (SELECT count(*) FROM inserted),
190
+ (SELECT count(*) FROM deleted)
191
+ INTO v_aggregated, v_deleted;
192
+
193
+ RETURN QUERY SELECT v_aggregated, v_deleted;
194
+ END;
195
+ $$;
@@ -0,0 +1,51 @@
1
+ -- ============================================================
2
+ -- Common audit infrastructure.
3
+ -- Trigger functions are generated by setup-audit.ts per table from
4
+ -- audit-config.json (respecting field rules and clusters), so this
5
+ -- file only contains the operation sequence and the audit_log table.
6
+ -- ============================================================
7
+
8
+ CREATE SCHEMA IF NOT EXISTS __AUDIT_SCHEMA__;
9
+
10
+ CREATE SEQUENCE IF NOT EXISTS __AUDIT_SCHEMA__.audit_log_operation_id_seq;
11
+
12
+ CREATE TABLE IF NOT EXISTS __AUDIT_SCHEMA__.audit_log (
13
+ id BIGSERIAL PRIMARY KEY, -- audit row id
14
+ operation_id BIGINT NOT NULL, -- operation id (shared by all fields of one change)
15
+ transaction_id BIGINT, -- database transaction id (pg_current_xact_id)
16
+ row_key JSONB, -- PK of the row itself: {"id": 42} / {"id1": .., "id2": ..}
17
+ cluster_row_key JSONB, -- PK of the level-1 entity (cluster)
18
+ cluster_name TEXT NOT NULL, -- cluster name from audit-config.json
19
+ table_name TEXT NOT NULL,
20
+ "timestamp" TIMESTAMPTZ NOT NULL DEFAULT now(), -- moment of change
21
+ column_name TEXT NOT NULL,
22
+ old_value TEXT,
23
+ new_value TEXT
24
+ );
25
+
26
+ -- Preserve old operation values on upgraded databases while allowing new
27
+ -- trigger functions to omit this legacy column.
28
+ DO $$
29
+ BEGIN
30
+ IF EXISTS (
31
+ SELECT 1 FROM pg_attribute
32
+ WHERE attrelid = '__AUDIT_SCHEMA__.audit_log'::regclass
33
+ AND attname = 'operation'
34
+ AND NOT attisdropped
35
+ ) THEN
36
+ ALTER TABLE __AUDIT_SCHEMA__.audit_log ALTER COLUMN operation DROP NOT NULL;
37
+ END IF;
38
+ END$$;
39
+
40
+ CREATE INDEX IF NOT EXISTS idx_audit_log_table_time
41
+ ON __AUDIT_SCHEMA__.audit_log (table_name, "timestamp");
42
+ CREATE INDEX IF NOT EXISTS idx_audit_log_operation_id
43
+ ON __AUDIT_SCHEMA__.audit_log (operation_id);
44
+ CREATE INDEX IF NOT EXISTS idx_audit_log_transaction_id
45
+ ON __AUDIT_SCHEMA__.audit_log (transaction_id);
46
+ CREATE INDEX IF NOT EXISTS idx_audit_log_row_key
47
+ ON __AUDIT_SCHEMA__.audit_log USING gin (row_key);
48
+ CREATE INDEX IF NOT EXISTS idx_audit_log_cluster_row_key
49
+ ON __AUDIT_SCHEMA__.audit_log USING gin (cluster_row_key);
50
+ CREATE INDEX IF NOT EXISTS idx_audit_log_cluster
51
+ ON __AUDIT_SCHEMA__.audit_log (cluster_name, "timestamp");
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "relational-audit-kit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "PostgreSQL audit logging toolkit for relational database tables",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "bin": {
15
+ "audit-setup": "./dist/cli/setup.js",
16
+ "audit-aggregate": "./dist/cli/aggregate.js",
17
+ "audit-run": "./dist/cli/run.js"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "scripts": {
25
+ "clean": "node scripts/clean.mjs",
26
+ "build": "tsc && node scripts/postbuild.mjs",
27
+ "prepack": "npm run clean && npm run build"
28
+ },
29
+ "keywords": [
30
+ "postgresql",
31
+ "postgres",
32
+ "audit",
33
+ "logging",
34
+ "trigger",
35
+ "database",
36
+ "change-tracking"
37
+ ],
38
+ "author": "Alexundr38",
39
+ "license": "MIT",
40
+ "dependencies": {
41
+ "@types/pg": "^8.11.10",
42
+ "@types/node": "^20.14.0",
43
+ "commander": "^12.1.0",
44
+ "dotenv": "^16.4.5",
45
+ "pg": "^8.13.0"
46
+ },
47
+ "devDependencies": {
48
+ "typescript": "^5.6.0"
49
+ },
50
+ "engines": {
51
+ "node": ">=18.0.0"
52
+ }
53
+ }