@geekmidas/audit 0.0.8 → 0.2.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 (64) hide show
  1. package/README.md +397 -0
  2. package/dist/{Auditor-D3me-qKX.d.mts → Auditor-_Dn2dp8d.d.mts} +46 -1
  3. package/dist/Auditor-_Dn2dp8d.d.mts.map +1 -0
  4. package/dist/{Auditor-QYUMGJCH.d.cts → Auditor-sW7YAJIA.d.cts} +46 -1
  5. package/dist/Auditor-sW7YAJIA.d.cts.map +1 -0
  6. package/dist/Auditor.d.cts +1 -1
  7. package/dist/Auditor.d.mts +1 -1
  8. package/dist/DefaultAuditor-BAVnNmRh.mjs.map +1 -1
  9. package/dist/{DefaultAuditor-BTuMMiWh.d.cts → DefaultAuditor-BSYMwojG.d.cts} +3 -2
  10. package/dist/DefaultAuditor-BSYMwojG.d.cts.map +1 -0
  11. package/dist/{DefaultAuditor-C1FWrJg6.d.mts → DefaultAuditor-CYE-uAry.d.mts} +3 -2
  12. package/dist/DefaultAuditor-CYE-uAry.d.mts.map +1 -0
  13. package/dist/DefaultAuditor-JbZ_BQfh.cjs.map +1 -1
  14. package/dist/DefaultAuditor.d.cts +2 -2
  15. package/dist/DefaultAuditor.d.mts +2 -2
  16. package/dist/cache-2VI80Vao.d.mts +96 -0
  17. package/dist/cache-2VI80Vao.d.mts.map +1 -0
  18. package/dist/cache-BbIl31RL.mjs +167 -0
  19. package/dist/cache-BbIl31RL.mjs.map +1 -0
  20. package/dist/cache-CDHqXqcl.d.cts +96 -0
  21. package/dist/cache-CDHqXqcl.d.cts.map +1 -0
  22. package/dist/cache-l7jAptBl.cjs +173 -0
  23. package/dist/cache-l7jAptBl.cjs.map +1 -0
  24. package/dist/cache.cjs +3 -0
  25. package/dist/cache.d.cts +3 -0
  26. package/dist/cache.d.mts +3 -0
  27. package/dist/cache.mjs +3 -0
  28. package/dist/index.d.cts +2 -2
  29. package/dist/index.d.mts +2 -2
  30. package/dist/kysely.cjs +24 -0
  31. package/dist/kysely.cjs.map +1 -1
  32. package/dist/kysely.d.cts +11 -1
  33. package/dist/kysely.d.cts.map +1 -0
  34. package/dist/kysely.d.mts +11 -1
  35. package/dist/kysely.d.mts.map +1 -0
  36. package/dist/kysely.mjs +24 -0
  37. package/dist/kysely.mjs.map +1 -1
  38. package/dist/memory.cjs +49 -0
  39. package/dist/memory.cjs.map +1 -0
  40. package/dist/memory.d.cts +44 -0
  41. package/dist/memory.d.cts.map +1 -0
  42. package/dist/memory.d.mts +44 -0
  43. package/dist/memory.d.mts.map +1 -0
  44. package/dist/memory.mjs +48 -0
  45. package/dist/memory.mjs.map +1 -0
  46. package/dist/storage.d.cts +1 -1
  47. package/dist/storage.d.mts +1 -1
  48. package/dist/types.d.cts +1 -1
  49. package/dist/types.d.mts +1 -1
  50. package/package.json +33 -5
  51. package/src/Auditor.ts +121 -121
  52. package/src/DefaultAuditor.ts +91 -91
  53. package/src/__tests__/CacheAuditStorage.spec.ts +382 -0
  54. package/src/__tests__/DefaultAuditor.spec.ts +520 -462
  55. package/src/__tests__/InMemoryAuditStorage.spec.ts +317 -0
  56. package/src/__tests__/KyselyAuditStorage.integration.spec.ts +500 -496
  57. package/src/__tests__/KyselyAuditStorage.spec.ts +433 -433
  58. package/src/cache.ts +263 -0
  59. package/src/index.ts +14 -15
  60. package/src/kysely.ts +292 -259
  61. package/src/memory.ts +50 -0
  62. package/src/storage.ts +132 -85
  63. package/src/types.ts +73 -74
  64. package/tsconfig.json +9 -0
@@ -0,0 +1,317 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+ import { InMemoryAuditStorage } from '../memory';
3
+ import type { AuditRecord } from '../types';
4
+
5
+ describe('InMemoryAuditStorage', () => {
6
+ let storage: InMemoryAuditStorage;
7
+
8
+ beforeEach(() => {
9
+ storage = new InMemoryAuditStorage();
10
+ });
11
+
12
+ describe('write', () => {
13
+ it('should write records to memory', async () => {
14
+ const records: AuditRecord[] = [
15
+ {
16
+ id: 'audit-1',
17
+ type: 'user.created',
18
+ operation: 'INSERT',
19
+ table: 'users',
20
+ entityId: 'user-123',
21
+ payload: { email: 'test@example.com' },
22
+ timestamp: new Date('2024-01-01T00:00:00Z'),
23
+ actor: { id: 'admin-1', type: 'admin' },
24
+ metadata: { requestId: 'req-123' },
25
+ },
26
+ ];
27
+
28
+ await storage.write(records);
29
+
30
+ const stored = await storage.getRecords();
31
+ expect(stored).toHaveLength(1);
32
+ expect(stored[0]).toMatchObject({
33
+ id: 'audit-1',
34
+ type: 'user.created',
35
+ entityId: 'user-123',
36
+ });
37
+ });
38
+
39
+ it('should append multiple records', async () => {
40
+ const record1: AuditRecord = {
41
+ id: 'audit-1',
42
+ type: 'user.created',
43
+ operation: 'INSERT',
44
+ timestamp: new Date('2024-01-01T00:00:00Z'),
45
+ };
46
+ const record2: AuditRecord = {
47
+ id: 'audit-2',
48
+ type: 'user.updated',
49
+ operation: 'UPDATE',
50
+ timestamp: new Date('2024-01-02T00:00:00Z'),
51
+ };
52
+
53
+ await storage.write([record1]);
54
+ await storage.write([record2]);
55
+
56
+ const stored = await storage.getRecords();
57
+ expect(stored).toHaveLength(2);
58
+ });
59
+
60
+ it('should do nothing for empty records', async () => {
61
+ await storage.write([]);
62
+
63
+ const records = await storage.getRecords();
64
+ expect(records).toHaveLength(0);
65
+ });
66
+ });
67
+
68
+ describe('query', () => {
69
+ beforeEach(async () => {
70
+ await storage.write([
71
+ {
72
+ id: 'audit-1',
73
+ type: 'user.created',
74
+ operation: 'INSERT',
75
+ table: 'users',
76
+ entityId: 'user-123',
77
+ payload: { email: 'test@example.com' },
78
+ timestamp: new Date('2024-01-01T00:00:00Z'),
79
+ actor: { id: 'admin-1', type: 'admin' },
80
+ },
81
+ {
82
+ id: 'audit-2',
83
+ type: 'user.updated',
84
+ operation: 'UPDATE',
85
+ table: 'users',
86
+ entityId: 'user-123',
87
+ timestamp: new Date('2024-01-02T00:00:00Z'),
88
+ actor: { id: 'admin-1', type: 'admin' },
89
+ },
90
+ {
91
+ id: 'audit-3',
92
+ type: 'order.placed',
93
+ operation: 'INSERT',
94
+ table: 'orders',
95
+ entityId: 'order-456',
96
+ timestamp: new Date('2024-01-03T00:00:00Z'),
97
+ actor: { id: 'user-1', type: 'user' },
98
+ },
99
+ ]);
100
+ });
101
+
102
+ it('should return all records when no filters', async () => {
103
+ const results = await storage.query({});
104
+ expect(results).toHaveLength(3);
105
+ });
106
+
107
+ it('should filter by type (string)', async () => {
108
+ const results = await storage.query({ type: 'user.created' });
109
+ expect(results).toHaveLength(1);
110
+ expect(results[0].id).toBe('audit-1');
111
+ });
112
+
113
+ it('should filter by type (array)', async () => {
114
+ const results = await storage.query({
115
+ type: ['user.created', 'user.updated'],
116
+ });
117
+ expect(results).toHaveLength(2);
118
+ });
119
+
120
+ it('should filter by entityId (string)', async () => {
121
+ const results = await storage.query({ entityId: 'user-123' });
122
+ expect(results).toHaveLength(2);
123
+ });
124
+
125
+ it('should filter by entityId (object)', async () => {
126
+ await storage.write([
127
+ {
128
+ id: 'audit-4',
129
+ type: 'relation.created',
130
+ operation: 'INSERT',
131
+ entityId: { userId: 'u1', roleId: 'r1' },
132
+ timestamp: new Date('2024-01-04T00:00:00Z'),
133
+ },
134
+ ]);
135
+
136
+ const results = await storage.query({
137
+ entityId: { userId: 'u1', roleId: 'r1' },
138
+ });
139
+ expect(results).toHaveLength(1);
140
+ expect(results[0].id).toBe('audit-4');
141
+ });
142
+
143
+ it('should filter by table', async () => {
144
+ const results = await storage.query({ table: 'users' });
145
+ expect(results).toHaveLength(2);
146
+ });
147
+
148
+ it('should filter by actorId', async () => {
149
+ const results = await storage.query({ actorId: 'user-1' });
150
+ expect(results).toHaveLength(1);
151
+ expect(results[0].id).toBe('audit-3');
152
+ });
153
+
154
+ it('should filter by date range (from)', async () => {
155
+ const results = await storage.query({
156
+ from: new Date('2024-01-02T00:00:00Z'),
157
+ });
158
+ expect(results).toHaveLength(2);
159
+ });
160
+
161
+ it('should filter by date range (to)', async () => {
162
+ const results = await storage.query({
163
+ to: new Date('2024-01-02T00:00:00Z'),
164
+ });
165
+ expect(results).toHaveLength(2);
166
+ });
167
+
168
+ it('should filter by date range (from and to)', async () => {
169
+ const results = await storage.query({
170
+ from: new Date('2024-01-02T00:00:00Z'),
171
+ to: new Date('2024-01-02T00:00:00Z'),
172
+ });
173
+ expect(results).toHaveLength(1);
174
+ expect(results[0].id).toBe('audit-2');
175
+ });
176
+
177
+ it('should apply pagination (limit)', async () => {
178
+ const results = await storage.query({ limit: 2 });
179
+ expect(results).toHaveLength(2);
180
+ });
181
+
182
+ it('should apply pagination (offset)', async () => {
183
+ const results = await storage.query({ offset: 1 });
184
+ expect(results).toHaveLength(2);
185
+ });
186
+
187
+ it('should apply pagination (limit and offset)', async () => {
188
+ const results = await storage.query({ limit: 1, offset: 1 });
189
+ expect(results).toHaveLength(1);
190
+ });
191
+
192
+ it('should order by timestamp desc by default', async () => {
193
+ const results = await storage.query({});
194
+ expect(results[0].id).toBe('audit-3'); // Most recent first
195
+ expect(results[2].id).toBe('audit-1'); // Oldest last
196
+ });
197
+
198
+ it('should order by timestamp asc', async () => {
199
+ const results = await storage.query({
200
+ orderBy: 'timestamp',
201
+ orderDirection: 'asc',
202
+ });
203
+ expect(results[0].id).toBe('audit-1'); // Oldest first
204
+ expect(results[2].id).toBe('audit-3'); // Most recent last
205
+ });
206
+
207
+ it('should order by type', async () => {
208
+ const results = await storage.query({
209
+ orderBy: 'type',
210
+ orderDirection: 'asc',
211
+ });
212
+ expect(results[0].type).toBe('order.placed');
213
+ expect(results[2].type).toBe('user.updated');
214
+ });
215
+
216
+ it('should combine multiple filters', async () => {
217
+ const results = await storage.query({
218
+ table: 'users',
219
+ actorId: 'admin-1',
220
+ });
221
+ expect(results).toHaveLength(2);
222
+ });
223
+ });
224
+
225
+ describe('count', () => {
226
+ beforeEach(async () => {
227
+ await storage.write([
228
+ {
229
+ id: 'audit-1',
230
+ type: 'user.created',
231
+ operation: 'INSERT',
232
+ timestamp: new Date(),
233
+ actor: { id: 'admin-1', type: 'admin' },
234
+ },
235
+ {
236
+ id: 'audit-2',
237
+ type: 'user.updated',
238
+ operation: 'UPDATE',
239
+ timestamp: new Date(),
240
+ actor: { id: 'admin-1', type: 'admin' },
241
+ },
242
+ {
243
+ id: 'audit-3',
244
+ type: 'order.placed',
245
+ operation: 'INSERT',
246
+ timestamp: new Date(),
247
+ actor: { id: 'user-1', type: 'user' },
248
+ },
249
+ ]);
250
+ });
251
+
252
+ it('should count all records', async () => {
253
+ const count = await storage.count({});
254
+ expect(count).toBe(3);
255
+ });
256
+
257
+ it('should count with type filter', async () => {
258
+ const count = await storage.count({ type: 'user.created' });
259
+ expect(count).toBe(1);
260
+ });
261
+
262
+ it('should count with actor filter', async () => {
263
+ const count = await storage.count({ actorId: 'admin-1' });
264
+ expect(count).toBe(2);
265
+ });
266
+ });
267
+
268
+ describe('clear', () => {
269
+ it('should clear all records', async () => {
270
+ await storage.write([
271
+ {
272
+ id: 'audit-1',
273
+ type: 'test',
274
+ operation: 'CUSTOM',
275
+ timestamp: new Date(),
276
+ },
277
+ {
278
+ id: 'audit-2',
279
+ type: 'test',
280
+ operation: 'CUSTOM',
281
+ timestamp: new Date(),
282
+ },
283
+ ]);
284
+
285
+ let records = await storage.getRecords();
286
+ expect(records).toHaveLength(2);
287
+
288
+ await storage.clear();
289
+
290
+ records = await storage.getRecords();
291
+ expect(records).toHaveLength(0);
292
+ });
293
+ });
294
+
295
+ describe('type safety', () => {
296
+ it('should support generic audit action types', async () => {
297
+ type AppAuditAction =
298
+ | { type: 'user.created'; payload: { userId: string } }
299
+ | { type: 'order.placed'; payload: { orderId: string } };
300
+
301
+ const typedStorage = new InMemoryAuditStorage<AppAuditAction>();
302
+
303
+ await typedStorage.write([
304
+ {
305
+ id: 'audit-1',
306
+ type: 'user.created',
307
+ operation: 'CUSTOM',
308
+ timestamp: new Date(),
309
+ payload: { userId: '123' },
310
+ },
311
+ ]);
312
+
313
+ const records = await typedStorage.getRecords();
314
+ expect(records).toHaveLength(1);
315
+ });
316
+ });
317
+ });