@geekmidas/audit 0.0.1

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 (47) hide show
  1. package/TECHNICAL.md +937 -0
  2. package/dist/Auditor-CZ8lkASv.d.cts +260 -0
  3. package/dist/Auditor-ii62d_pF.d.mts +260 -0
  4. package/dist/Auditor.cjs +0 -0
  5. package/dist/Auditor.d.cts +2 -0
  6. package/dist/Auditor.d.mts +2 -0
  7. package/dist/Auditor.mjs +0 -0
  8. package/dist/DefaultAuditor-1HDUGMub.cjs +124 -0
  9. package/dist/DefaultAuditor-1HDUGMub.cjs.map +1 -0
  10. package/dist/DefaultAuditor-B9Unin1g.d.mts +59 -0
  11. package/dist/DefaultAuditor-BAVnNmRh.mjs +96 -0
  12. package/dist/DefaultAuditor-BAVnNmRh.mjs.map +1 -0
  13. package/dist/DefaultAuditor-Dqc4UZA1.d.cts +59 -0
  14. package/dist/DefaultAuditor.cjs +3 -0
  15. package/dist/DefaultAuditor.d.cts +4 -0
  16. package/dist/DefaultAuditor.d.mts +4 -0
  17. package/dist/DefaultAuditor.mjs +3 -0
  18. package/dist/index.cjs +3 -0
  19. package/dist/index.d.cts +4 -0
  20. package/dist/index.d.mts +4 -0
  21. package/dist/index.mjs +3 -0
  22. package/dist/kysely.cjs +193 -0
  23. package/dist/kysely.cjs.map +1 -0
  24. package/dist/kysely.d.cts +165 -0
  25. package/dist/kysely.d.mts +165 -0
  26. package/dist/kysely.mjs +191 -0
  27. package/dist/kysely.mjs.map +1 -0
  28. package/dist/storage-BtFY7Rha.d.cts +102 -0
  29. package/dist/storage-DnAMfOA7.d.mts +102 -0
  30. package/dist/storage.cjs +0 -0
  31. package/dist/storage.d.cts +3 -0
  32. package/dist/storage.d.mts +3 -0
  33. package/dist/storage.mjs +0 -0
  34. package/dist/types.cjs +0 -0
  35. package/dist/types.d.cts +2 -0
  36. package/dist/types.d.mts +2 -0
  37. package/dist/types.mjs +0 -0
  38. package/package.json +34 -0
  39. package/src/Auditor.ts +157 -0
  40. package/src/DefaultAuditor.ts +141 -0
  41. package/src/__tests__/DefaultAuditor.spec.ts +420 -0
  42. package/src/__tests__/KyselyAuditStorage.integration.spec.ts +517 -0
  43. package/src/__tests__/KyselyAuditStorage.spec.ts +359 -0
  44. package/src/index.ts +23 -0
  45. package/src/kysely.ts +391 -0
  46. package/src/storage.ts +101 -0
  47. package/src/types.ts +147 -0
@@ -0,0 +1,359 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { KyselyAuditStorage, type AuditLogTable } from '../kysely';
3
+ import type { AuditRecord } from '../types';
4
+
5
+ // Mock Kysely database
6
+ function createMockDb() {
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
+ }
40
+
41
+ describe('KyselyAuditStorage', () => {
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: JSON.stringify({ email: 'test@example.com' }),
80
+ actorId: 'admin-1',
81
+ actorType: 'admin',
82
+ metadata: JSON.stringify({ 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: JSON.stringify({ email: 'user@example.com', role: 'admin' }),
159
+ }),
160
+ ]);
161
+ });
162
+ });
163
+
164
+ describe('query', () => {
165
+ it('should return records from database', async () => {
166
+ mockDb.addRow({
167
+ id: 'audit-1',
168
+ type: 'user.created',
169
+ operation: 'INSERT',
170
+ table: 'users',
171
+ entityId: 'user-123',
172
+ oldValues: null,
173
+ newValues: JSON.stringify({ name: 'Test' }),
174
+ payload: JSON.stringify({ email: 'test@example.com' }),
175
+ timestamp: new Date('2024-01-01T00:00:00Z'),
176
+ actorId: 'admin-1',
177
+ actorType: 'admin',
178
+ actorData: null,
179
+ metadata: JSON.stringify({ requestId: 'req-123' }),
180
+ });
181
+
182
+ const results = await storage.query({});
183
+
184
+ expect(results).toHaveLength(1);
185
+ expect(results[0]).toMatchObject({
186
+ id: 'audit-1',
187
+ type: 'user.created',
188
+ operation: 'INSERT',
189
+ table: 'users',
190
+ entityId: 'user-123',
191
+ newValues: { name: 'Test' },
192
+ payload: { email: 'test@example.com' },
193
+ actor: { id: 'admin-1', type: 'admin' },
194
+ metadata: { requestId: 'req-123' },
195
+ });
196
+ });
197
+
198
+ it('should apply type filter', async () => {
199
+ await storage.query({ type: 'user.created' });
200
+
201
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
202
+ 'type',
203
+ '=',
204
+ 'user.created',
205
+ );
206
+ });
207
+
208
+ it('should apply type array filter', async () => {
209
+ await storage.query({ type: ['user.created', 'user.updated'] });
210
+
211
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith('type', 'in', [
212
+ 'user.created',
213
+ 'user.updated',
214
+ ]);
215
+ });
216
+
217
+ it('should apply entity ID filter', async () => {
218
+ await storage.query({ entityId: 'user-123' });
219
+
220
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
221
+ 'entityId',
222
+ '=',
223
+ 'user-123',
224
+ );
225
+ });
226
+
227
+ it('should apply table filter', async () => {
228
+ await storage.query({ table: 'users' });
229
+
230
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
231
+ 'table',
232
+ '=',
233
+ 'users',
234
+ );
235
+ });
236
+
237
+ it('should apply actor ID filter', async () => {
238
+ await storage.query({ actorId: 'admin-1' });
239
+
240
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
241
+ 'actorId',
242
+ '=',
243
+ 'admin-1',
244
+ );
245
+ });
246
+
247
+ it('should apply date range filters', async () => {
248
+ const from = new Date('2024-01-01');
249
+ const to = new Date('2024-12-31');
250
+
251
+ await storage.query({ from, to });
252
+
253
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
254
+ 'timestamp',
255
+ '>=',
256
+ from,
257
+ );
258
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
259
+ 'timestamp',
260
+ '<=',
261
+ to,
262
+ );
263
+ });
264
+
265
+ it('should apply pagination', async () => {
266
+ await storage.query({ limit: 10, offset: 20 });
267
+
268
+ expect(mockDb.selectBuilder.limit).toHaveBeenCalledWith(10);
269
+ expect(mockDb.selectBuilder.offset).toHaveBeenCalledWith(20);
270
+ });
271
+
272
+ it('should apply ordering', async () => {
273
+ await storage.query({ orderBy: 'type', orderDirection: 'asc' });
274
+
275
+ expect(mockDb.selectBuilder.orderBy).toHaveBeenCalledWith('type', 'asc');
276
+ });
277
+
278
+ it('should default to timestamp desc ordering', async () => {
279
+ await storage.query({});
280
+
281
+ expect(mockDb.selectBuilder.orderBy).toHaveBeenCalledWith(
282
+ 'timestamp',
283
+ 'desc',
284
+ );
285
+ });
286
+
287
+ it('should parse complex entity IDs', async () => {
288
+ mockDb.addRow({
289
+ id: 'audit-1',
290
+ type: 'relation.created',
291
+ operation: 'INSERT',
292
+ table: null,
293
+ entityId: JSON.stringify({ userId: 'u1', roleId: 'r1' }),
294
+ oldValues: null,
295
+ newValues: null,
296
+ payload: null,
297
+ timestamp: new Date(),
298
+ actorId: null,
299
+ actorType: null,
300
+ actorData: null,
301
+ metadata: null,
302
+ });
303
+
304
+ const results = await storage.query({});
305
+
306
+ expect(results[0].entityId).toEqual({ userId: 'u1', roleId: 'r1' });
307
+ });
308
+
309
+ it('should merge actor data', async () => {
310
+ mockDb.addRow({
311
+ id: 'audit-1',
312
+ type: 'user.created',
313
+ operation: 'CUSTOM',
314
+ table: null,
315
+ entityId: null,
316
+ oldValues: null,
317
+ newValues: null,
318
+ payload: null,
319
+ timestamp: new Date(),
320
+ actorId: 'user-1',
321
+ actorType: 'user',
322
+ actorData: JSON.stringify({ email: 'user@example.com' }),
323
+ metadata: null,
324
+ });
325
+
326
+ const results = await storage.query({});
327
+
328
+ expect(results[0].actor).toEqual({
329
+ id: 'user-1',
330
+ type: 'user',
331
+ email: 'user@example.com',
332
+ });
333
+ });
334
+ });
335
+
336
+ describe('count', () => {
337
+ it('should return count from database', async () => {
338
+ const count = await storage.count({});
339
+
340
+ expect(count).toBe(0);
341
+ expect(mockDb.selectBuilder.select).toHaveBeenCalled();
342
+ });
343
+
344
+ it('should apply filters to count', async () => {
345
+ await storage.count({ type: 'user.created', actorId: 'admin-1' });
346
+
347
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
348
+ 'type',
349
+ '=',
350
+ 'user.created',
351
+ );
352
+ expect(mockDb.selectBuilder.where).toHaveBeenCalledWith(
353
+ 'actorId',
354
+ '=',
355
+ 'admin-1',
356
+ );
357
+ });
358
+ });
359
+ });
package/src/index.ts ADDED
@@ -0,0 +1,23 @@
1
+ // Core types
2
+ export type {
3
+ AuditableAction,
4
+ AuditActor,
5
+ AuditMetadata,
6
+ AuditOperation,
7
+ AuditOptions,
8
+ AuditRecord,
9
+ ExtractAuditPayload,
10
+ ExtractAuditType,
11
+ ExtractAuditorAction,
12
+ MappedAudit,
13
+ } from './types';
14
+
15
+ // Auditor interface
16
+ export type { Auditor } from './Auditor';
17
+
18
+ // Default implementation
19
+ export { DefaultAuditor } from './DefaultAuditor';
20
+ export type { DefaultAuditorConfig } from './DefaultAuditor';
21
+
22
+ // Storage interface
23
+ export type { AuditQueryOptions, AuditStorage } from './storage';