@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.
- package/README.md +397 -0
- package/dist/{Auditor-D3me-qKX.d.mts → Auditor-_Dn2dp8d.d.mts} +46 -1
- package/dist/Auditor-_Dn2dp8d.d.mts.map +1 -0
- package/dist/{Auditor-QYUMGJCH.d.cts → Auditor-sW7YAJIA.d.cts} +46 -1
- package/dist/Auditor-sW7YAJIA.d.cts.map +1 -0
- package/dist/Auditor.d.cts +1 -1
- package/dist/Auditor.d.mts +1 -1
- package/dist/DefaultAuditor-BAVnNmRh.mjs.map +1 -1
- package/dist/{DefaultAuditor-BTuMMiWh.d.cts → DefaultAuditor-BSYMwojG.d.cts} +3 -2
- package/dist/DefaultAuditor-BSYMwojG.d.cts.map +1 -0
- package/dist/{DefaultAuditor-C1FWrJg6.d.mts → DefaultAuditor-CYE-uAry.d.mts} +3 -2
- package/dist/DefaultAuditor-CYE-uAry.d.mts.map +1 -0
- package/dist/DefaultAuditor-JbZ_BQfh.cjs.map +1 -1
- package/dist/DefaultAuditor.d.cts +2 -2
- package/dist/DefaultAuditor.d.mts +2 -2
- package/dist/cache-2VI80Vao.d.mts +96 -0
- package/dist/cache-2VI80Vao.d.mts.map +1 -0
- package/dist/cache-BbIl31RL.mjs +167 -0
- package/dist/cache-BbIl31RL.mjs.map +1 -0
- package/dist/cache-CDHqXqcl.d.cts +96 -0
- package/dist/cache-CDHqXqcl.d.cts.map +1 -0
- package/dist/cache-l7jAptBl.cjs +173 -0
- package/dist/cache-l7jAptBl.cjs.map +1 -0
- package/dist/cache.cjs +3 -0
- package/dist/cache.d.cts +3 -0
- package/dist/cache.d.mts +3 -0
- package/dist/cache.mjs +3 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/kysely.cjs +24 -0
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +11 -1
- package/dist/kysely.d.cts.map +1 -0
- package/dist/kysely.d.mts +11 -1
- package/dist/kysely.d.mts.map +1 -0
- package/dist/kysely.mjs +24 -0
- package/dist/kysely.mjs.map +1 -1
- package/dist/memory.cjs +49 -0
- package/dist/memory.cjs.map +1 -0
- package/dist/memory.d.cts +44 -0
- package/dist/memory.d.cts.map +1 -0
- package/dist/memory.d.mts +44 -0
- package/dist/memory.d.mts.map +1 -0
- package/dist/memory.mjs +48 -0
- package/dist/memory.mjs.map +1 -0
- package/dist/storage.d.cts +1 -1
- package/dist/storage.d.mts +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +33 -5
- package/src/Auditor.ts +121 -121
- package/src/DefaultAuditor.ts +91 -91
- package/src/__tests__/CacheAuditStorage.spec.ts +382 -0
- package/src/__tests__/DefaultAuditor.spec.ts +520 -462
- package/src/__tests__/InMemoryAuditStorage.spec.ts +317 -0
- package/src/__tests__/KyselyAuditStorage.integration.spec.ts +500 -496
- package/src/__tests__/KyselyAuditStorage.spec.ts +433 -433
- package/src/cache.ts +263 -0
- package/src/index.ts +14 -15
- package/src/kysely.ts +292 -259
- package/src/memory.ts +50 -0
- package/src/storage.ts +132 -85
- package/src/types.ts +73 -74
- package/tsconfig.json +9 -0
|
@@ -4,440 +4,440 @@ import type { AuditRecord } from '../types';
|
|
|
4
4
|
|
|
5
5
|
// Mock Kysely database
|
|
6
6
|
function createMockDb() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
7
|
+
const rows: AuditLogTable[] = [];
|
|
8
|
+
|
|
9
|
+
const insertBuilder = {
|
|
10
|
+
values: vi.fn().mockReturnThis(),
|
|
11
|
+
execute: vi.fn(async () => {
|
|
12
|
+
return [];
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const selectBuilder = {
|
|
17
|
+
selectAll: vi.fn().mockReturnThis(),
|
|
18
|
+
select: vi.fn().mockReturnThis(),
|
|
19
|
+
where: vi.fn().mockReturnThis(),
|
|
20
|
+
orderBy: vi.fn().mockReturnThis(),
|
|
21
|
+
limit: vi.fn().mockReturnThis(),
|
|
22
|
+
offset: vi.fn().mockReturnThis(),
|
|
23
|
+
execute: vi.fn(async () => rows),
|
|
24
|
+
executeTakeFirst: vi.fn(async () => ({ count: rows.length })),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const db = {
|
|
28
|
+
insertInto: vi.fn(() => insertBuilder),
|
|
29
|
+
selectFrom: vi.fn(() => selectBuilder),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
db,
|
|
34
|
+
insertBuilder,
|
|
35
|
+
selectBuilder,
|
|
36
|
+
rows,
|
|
37
|
+
addRow: (row: AuditLogTable) => rows.push(row),
|
|
38
|
+
};
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
describe('KyselyAuditStorage', () => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
42
|
+
let mockDb: ReturnType<typeof createMockDb>;
|
|
43
|
+
let storage: KyselyAuditStorage<{ audit_logs: AuditLogTable }>;
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
mockDb = createMockDb();
|
|
47
|
+
storage = new KyselyAuditStorage({
|
|
48
|
+
db: mockDb.db as any,
|
|
49
|
+
tableName: 'audit_logs',
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('write', () => {
|
|
54
|
+
it('should write records to database', async () => {
|
|
55
|
+
const records: AuditRecord[] = [
|
|
56
|
+
{
|
|
57
|
+
id: 'audit-1',
|
|
58
|
+
type: 'user.created',
|
|
59
|
+
operation: 'INSERT',
|
|
60
|
+
table: 'users',
|
|
61
|
+
entityId: 'user-123',
|
|
62
|
+
payload: { email: 'test@example.com' },
|
|
63
|
+
timestamp: new Date('2024-01-01T00:00:00Z'),
|
|
64
|
+
actor: { id: 'admin-1', type: 'admin' },
|
|
65
|
+
metadata: { requestId: 'req-123' },
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
await storage.write(records);
|
|
70
|
+
|
|
71
|
+
expect(mockDb.db.insertInto).toHaveBeenCalledWith('audit_logs');
|
|
72
|
+
expect(mockDb.insertBuilder.values).toHaveBeenCalledWith([
|
|
73
|
+
expect.objectContaining({
|
|
74
|
+
id: 'audit-1',
|
|
75
|
+
type: 'user.created',
|
|
76
|
+
operation: 'INSERT',
|
|
77
|
+
table: 'users',
|
|
78
|
+
entityId: 'user-123',
|
|
79
|
+
payload: { email: 'test@example.com' },
|
|
80
|
+
actorId: 'admin-1',
|
|
81
|
+
actorType: 'admin',
|
|
82
|
+
metadata: { requestId: 'req-123' },
|
|
83
|
+
}),
|
|
84
|
+
]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should use transaction when provided', async () => {
|
|
88
|
+
const mockTrx = {
|
|
89
|
+
insertInto: vi.fn(() => ({
|
|
90
|
+
values: vi.fn().mockReturnThis(),
|
|
91
|
+
execute: vi.fn(),
|
|
92
|
+
})),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const records: AuditRecord[] = [
|
|
96
|
+
{
|
|
97
|
+
id: 'audit-1',
|
|
98
|
+
type: 'user.created',
|
|
99
|
+
operation: 'CUSTOM',
|
|
100
|
+
timestamp: new Date(),
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
await storage.write(records, mockTrx);
|
|
105
|
+
|
|
106
|
+
expect(mockTrx.insertInto).toHaveBeenCalledWith('audit_logs');
|
|
107
|
+
expect(mockDb.db.insertInto).not.toHaveBeenCalled();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should do nothing for empty records', async () => {
|
|
111
|
+
await storage.write([]);
|
|
112
|
+
|
|
113
|
+
expect(mockDb.db.insertInto).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should handle complex entity IDs', async () => {
|
|
117
|
+
const records: AuditRecord[] = [
|
|
118
|
+
{
|
|
119
|
+
id: 'audit-1',
|
|
120
|
+
type: 'relation.created',
|
|
121
|
+
operation: 'INSERT',
|
|
122
|
+
entityId: { userId: 'u1', roleId: 'r1' },
|
|
123
|
+
timestamp: new Date(),
|
|
124
|
+
},
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
await storage.write(records);
|
|
128
|
+
|
|
129
|
+
expect(mockDb.insertBuilder.values).toHaveBeenCalledWith([
|
|
130
|
+
expect.objectContaining({
|
|
131
|
+
entityId: JSON.stringify({ userId: 'u1', roleId: 'r1' }),
|
|
132
|
+
}),
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should store extra actor properties', async () => {
|
|
137
|
+
const records: AuditRecord[] = [
|
|
138
|
+
{
|
|
139
|
+
id: 'audit-1',
|
|
140
|
+
type: 'user.created',
|
|
141
|
+
operation: 'CUSTOM',
|
|
142
|
+
timestamp: new Date(),
|
|
143
|
+
actor: {
|
|
144
|
+
id: 'user-1',
|
|
145
|
+
type: 'user',
|
|
146
|
+
email: 'user@example.com',
|
|
147
|
+
role: 'admin',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
await storage.write(records);
|
|
153
|
+
|
|
154
|
+
expect(mockDb.insertBuilder.values).toHaveBeenCalledWith([
|
|
155
|
+
expect.objectContaining({
|
|
156
|
+
actorId: 'user-1',
|
|
157
|
+
actorType: 'user',
|
|
158
|
+
actorData: {
|
|
159
|
+
email: 'user@example.com',
|
|
160
|
+
role: 'admin',
|
|
161
|
+
},
|
|
162
|
+
}),
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should generate ID with nanoid when autoId is false (default)', async () => {
|
|
167
|
+
const records: AuditRecord[] = [
|
|
168
|
+
{
|
|
169
|
+
id: '', // Empty ID
|
|
170
|
+
type: 'user.created',
|
|
171
|
+
operation: 'CUSTOM',
|
|
172
|
+
timestamp: new Date(),
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
await storage.write(records);
|
|
177
|
+
|
|
178
|
+
const calledValues = mockDb.insertBuilder.values.mock.calls[0][0];
|
|
179
|
+
expect(calledValues[0].id).toBeDefined();
|
|
180
|
+
expect(calledValues[0].id.length).toBeGreaterThan(0);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should use provided ID when autoId is false (default)', async () => {
|
|
184
|
+
const records: AuditRecord[] = [
|
|
185
|
+
{
|
|
186
|
+
id: 'my-custom-id',
|
|
187
|
+
type: 'user.created',
|
|
188
|
+
operation: 'CUSTOM',
|
|
189
|
+
timestamp: new Date(),
|
|
190
|
+
},
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
await storage.write(records);
|
|
194
|
+
|
|
195
|
+
expect(mockDb.insertBuilder.values).toHaveBeenCalledWith([
|
|
196
|
+
expect.objectContaining({
|
|
197
|
+
id: 'my-custom-id',
|
|
198
|
+
}),
|
|
199
|
+
]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should omit ID when autoId is true and no ID provided', async () => {
|
|
203
|
+
const storageAutoId = new KyselyAuditStorage({
|
|
204
|
+
db: mockDb.db as any,
|
|
205
|
+
tableName: 'audit_logs',
|
|
206
|
+
autoId: true,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const records: AuditRecord[] = [
|
|
210
|
+
{
|
|
211
|
+
id: '', // Empty ID - let database generate
|
|
212
|
+
type: 'user.created',
|
|
213
|
+
operation: 'CUSTOM',
|
|
214
|
+
timestamp: new Date(),
|
|
215
|
+
},
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
await storageAutoId.write(records);
|
|
219
|
+
|
|
220
|
+
const calledValues = mockDb.insertBuilder.values.mock.calls[0][0];
|
|
221
|
+
expect(calledValues[0].id).toBeUndefined();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should omit ID even if provided when autoId is true', async () => {
|
|
225
|
+
const storageAutoId = new KyselyAuditStorage({
|
|
226
|
+
db: mockDb.db as any,
|
|
227
|
+
tableName: 'audit_logs',
|
|
228
|
+
autoId: true,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
const records: AuditRecord[] = [
|
|
232
|
+
{
|
|
233
|
+
id: 'explicit-id', // This should be ignored when autoId is true
|
|
234
|
+
type: 'user.created',
|
|
235
|
+
operation: 'CUSTOM',
|
|
236
|
+
timestamp: new Date(),
|
|
237
|
+
},
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
await storageAutoId.write(records);
|
|
241
|
+
|
|
242
|
+
// When autoId is true, ID should always be omitted (let database generate)
|
|
243
|
+
const calledValues = mockDb.insertBuilder.values.mock.calls[0][0];
|
|
244
|
+
expect(calledValues[0].id).toBeUndefined();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('query', () => {
|
|
249
|
+
it('should return records from database', async () => {
|
|
250
|
+
mockDb.addRow({
|
|
251
|
+
id: 'audit-1',
|
|
252
|
+
type: 'user.created',
|
|
253
|
+
operation: 'INSERT',
|
|
254
|
+
table: 'users',
|
|
255
|
+
entityId: 'user-123',
|
|
256
|
+
oldValues: null,
|
|
257
|
+
newValues: JSON.stringify({ name: 'Test' }),
|
|
258
|
+
payload: JSON.stringify({ email: 'test@example.com' }),
|
|
259
|
+
timestamp: new Date('2024-01-01T00:00:00Z'),
|
|
260
|
+
actorId: 'admin-1',
|
|
261
|
+
actorType: 'admin',
|
|
262
|
+
actorData: null,
|
|
263
|
+
metadata: JSON.stringify({ requestId: 'req-123' }),
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const results = await storage.query({});
|
|
267
|
+
|
|
268
|
+
expect(results).toHaveLength(1);
|
|
269
|
+
expect(results[0]).toMatchObject({
|
|
270
|
+
id: 'audit-1',
|
|
271
|
+
type: 'user.created',
|
|
272
|
+
operation: 'INSERT',
|
|
273
|
+
table: 'users',
|
|
274
|
+
entityId: 'user-123',
|
|
275
|
+
newValues: { name: 'Test' },
|
|
276
|
+
payload: { email: 'test@example.com' },
|
|
277
|
+
actor: { id: 'admin-1', type: 'admin' },
|
|
278
|
+
metadata: { requestId: 'req-123' },
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should apply type filter', async () => {
|
|
283
|
+
await storage.query({ type: 'user.created' });
|
|
284
|
+
|
|
285
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
286
|
+
'type',
|
|
287
|
+
'=',
|
|
288
|
+
'user.created',
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('should apply type array filter', async () => {
|
|
293
|
+
await storage.query({ type: ['user.created', 'user.updated'] });
|
|
294
|
+
|
|
295
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith('type', 'in', [
|
|
296
|
+
'user.created',
|
|
297
|
+
'user.updated',
|
|
298
|
+
]);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('should apply entity ID filter', async () => {
|
|
302
|
+
await storage.query({ entityId: 'user-123' });
|
|
303
|
+
|
|
304
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
305
|
+
'entityId',
|
|
306
|
+
'=',
|
|
307
|
+
'user-123',
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('should apply table filter', async () => {
|
|
312
|
+
await storage.query({ table: 'users' });
|
|
313
|
+
|
|
314
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
315
|
+
'table',
|
|
316
|
+
'=',
|
|
317
|
+
'users',
|
|
318
|
+
);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('should apply actor ID filter', async () => {
|
|
322
|
+
await storage.query({ actorId: 'admin-1' });
|
|
323
|
+
|
|
324
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
325
|
+
'actorId',
|
|
326
|
+
'=',
|
|
327
|
+
'admin-1',
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should apply date range filters', async () => {
|
|
332
|
+
const from = new Date('2024-01-01');
|
|
333
|
+
const to = new Date('2024-12-31');
|
|
334
|
+
|
|
335
|
+
await storage.query({ from, to });
|
|
336
|
+
|
|
337
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
338
|
+
'timestamp',
|
|
339
|
+
'>=',
|
|
340
|
+
from,
|
|
341
|
+
);
|
|
342
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
343
|
+
'timestamp',
|
|
344
|
+
'<=',
|
|
345
|
+
to,
|
|
346
|
+
);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should apply pagination', async () => {
|
|
350
|
+
await storage.query({ limit: 10, offset: 20 });
|
|
351
|
+
|
|
352
|
+
expect(mockDb.selectBuilder.limit).toHaveBeenCalledWith(10);
|
|
353
|
+
expect(mockDb.selectBuilder.offset).toHaveBeenCalledWith(20);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('should apply ordering', async () => {
|
|
357
|
+
await storage.query({ orderBy: 'type', orderDirection: 'asc' });
|
|
358
|
+
|
|
359
|
+
expect(mockDb.selectBuilder.orderBy).toHaveBeenCalledWith('type', 'asc');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('should default to timestamp desc ordering', async () => {
|
|
363
|
+
await storage.query({});
|
|
364
|
+
|
|
365
|
+
expect(mockDb.selectBuilder.orderBy).toHaveBeenCalledWith(
|
|
366
|
+
'timestamp',
|
|
367
|
+
'desc',
|
|
368
|
+
);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('should parse complex entity IDs', async () => {
|
|
372
|
+
mockDb.addRow({
|
|
373
|
+
id: 'audit-1',
|
|
374
|
+
type: 'relation.created',
|
|
375
|
+
operation: 'INSERT',
|
|
376
|
+
table: null,
|
|
377
|
+
entityId: JSON.stringify({ userId: 'u1', roleId: 'r1' }),
|
|
378
|
+
oldValues: null,
|
|
379
|
+
newValues: null,
|
|
380
|
+
payload: null,
|
|
381
|
+
timestamp: new Date(),
|
|
382
|
+
actorId: null,
|
|
383
|
+
actorType: null,
|
|
384
|
+
actorData: null,
|
|
385
|
+
metadata: null,
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
const results = await storage.query({});
|
|
389
|
+
|
|
390
|
+
expect(results[0].entityId).toEqual({ userId: 'u1', roleId: 'r1' });
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('should merge actor data', async () => {
|
|
394
|
+
mockDb.addRow({
|
|
395
|
+
id: 'audit-1',
|
|
396
|
+
type: 'user.created',
|
|
397
|
+
operation: 'CUSTOM',
|
|
398
|
+
table: null,
|
|
399
|
+
entityId: null,
|
|
400
|
+
oldValues: null,
|
|
401
|
+
newValues: null,
|
|
402
|
+
payload: null,
|
|
403
|
+
timestamp: new Date(),
|
|
404
|
+
actorId: 'user-1',
|
|
405
|
+
actorType: 'user',
|
|
406
|
+
actorData: JSON.stringify({ email: 'user@example.com' }),
|
|
407
|
+
metadata: null,
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
const results = await storage.query({});
|
|
411
|
+
|
|
412
|
+
expect(results[0].actor).toEqual({
|
|
413
|
+
id: 'user-1',
|
|
414
|
+
type: 'user',
|
|
415
|
+
email: 'user@example.com',
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe('count', () => {
|
|
421
|
+
it('should return count from database', async () => {
|
|
422
|
+
const count = await storage.count({});
|
|
423
|
+
|
|
424
|
+
expect(count).toBe(0);
|
|
425
|
+
expect(mockDb.selectBuilder.select).toHaveBeenCalled();
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('should apply filters to count', async () => {
|
|
429
|
+
await storage.count({ type: 'user.created', actorId: 'admin-1' });
|
|
430
|
+
|
|
431
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
432
|
+
'type',
|
|
433
|
+
'=',
|
|
434
|
+
'user.created',
|
|
435
|
+
);
|
|
436
|
+
expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
|
|
437
|
+
'actorId',
|
|
438
|
+
'=',
|
|
439
|
+
'admin-1',
|
|
440
|
+
);
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
443
|
});
|