@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
package/src/Auditor.ts ADDED
@@ -0,0 +1,157 @@
1
+ import type {
2
+ AuditableAction,
3
+ AuditActor,
4
+ AuditMetadata,
5
+ AuditOptions,
6
+ AuditRecord,
7
+ ExtractAuditPayload,
8
+ ExtractAuditType,
9
+ } from './types';
10
+
11
+ /**
12
+ * Interface for audit collection and flushing.
13
+ * Generic over audit action types for full type safety.
14
+ *
15
+ * @template TAuditAction - Union of all allowed audit action types
16
+ * @template TTransaction - Transaction type (e.g., Kysely Transaction)
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * type AppAuditAction =
21
+ * | AuditableAction<'user.created', { userId: string; email: string }>
22
+ * | AuditableAction<'user.updated', { userId: string; changes: string[] }>;
23
+ *
24
+ * // In endpoint handler
25
+ * const auditor: Auditor<AppAuditAction> = ...;
26
+ *
27
+ * // Type-safe audit calls
28
+ * auditor.audit('user.created', { userId: '123', email: 'test@example.com' }); // ✅
29
+ * auditor.audit('user.created', { orderId: '123' }); // ❌ Type error
30
+ * auditor.audit('unknown.type', {}); // ❌ Type error
31
+ * ```
32
+ */
33
+ export interface Auditor<
34
+ TAuditAction extends AuditableAction<string, unknown> = AuditableAction<
35
+ string,
36
+ unknown
37
+ >,
38
+ TTransaction = unknown,
39
+ > {
40
+ /**
41
+ * The actor for all audits in this context.
42
+ * Set at construction time, immutable throughout the request.
43
+ */
44
+ readonly actor: AuditActor;
45
+
46
+ /**
47
+ * Record a type-safe audit entry.
48
+ * The payload type is inferred from the audit type.
49
+ *
50
+ * @param type - The audit type (must be a valid type from TAuditAction)
51
+ * @param payload - The audit payload (shape enforced by type)
52
+ * @param options - Optional audit metadata
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * auditor.audit('user.created', {
57
+ * userId: '123',
58
+ * email: 'test@example.com',
59
+ * });
60
+ *
61
+ * auditor.audit('order.placed', {
62
+ * orderId: 'order-456',
63
+ * total: 99.99,
64
+ * }, {
65
+ * entityId: 'order-456',
66
+ * table: 'orders',
67
+ * });
68
+ * ```
69
+ */
70
+ audit<TType extends ExtractAuditType<TAuditAction>>(
71
+ type: TType,
72
+ payload: ExtractAuditPayload<TAuditAction, TType>,
73
+ options?: AuditOptions,
74
+ ): void;
75
+
76
+ /**
77
+ * Record a raw audit record.
78
+ * Use this when you need full control over the audit structure,
79
+ * bypassing type safety.
80
+ *
81
+ * @param record - The audit record (id, timestamp, actor added automatically)
82
+ */
83
+ record(record: Omit<AuditRecord, 'id' | 'timestamp' | 'actor'>): void;
84
+
85
+ /**
86
+ * Get all collected audit records.
87
+ * Useful for inspection or custom processing.
88
+ */
89
+ getRecords(): AuditRecord[];
90
+
91
+ /**
92
+ * Flush all collected audits to storage.
93
+ * Called automatically by the endpoint adaptor inside the transaction.
94
+ *
95
+ * @param trx - Optional transaction context for atomic writes
96
+ */
97
+ flush(trx?: TTransaction): Promise<void>;
98
+
99
+ /**
100
+ * Clear all collected audit records without flushing.
101
+ * Use with caution - collected audits will be lost.
102
+ */
103
+ clear(): void;
104
+
105
+ /**
106
+ * Add metadata to all future audit records.
107
+ * Merges with existing metadata (new values override existing).
108
+ * Typically called by adaptors to add request context.
109
+ *
110
+ * @param metadata - Metadata to add (requestId, endpoint, method, ip, etc.)
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * // In endpoint adaptor
115
+ * auditor.addMetadata({
116
+ * requestId: 'req-123',
117
+ * endpoint: '/users',
118
+ * method: 'POST',
119
+ * ip: '192.168.1.1',
120
+ * });
121
+ * ```
122
+ */
123
+ addMetadata(metadata: AuditMetadata): void;
124
+
125
+ /**
126
+ * Set the transaction context for audit flushing.
127
+ * When set, flush() will use this transaction instead of requiring
128
+ * it to be passed explicitly. This enables declarative audits to
129
+ * participate in the same transaction as the handler's database operations.
130
+ *
131
+ * @param trx - The transaction context (e.g., Kysely Transaction)
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * // In handler with explicit transaction management
136
+ * const result = await withTransaction(services.database.raw, async (trx) => {
137
+ * // Register transaction with auditor so declarative audits use it
138
+ * auditor.setTransaction(trx);
139
+ *
140
+ * const user = await trx.insertInto('users').values(data).returningAll().executeTakeFirstOrThrow();
141
+ *
142
+ * // Manual audits will also use this transaction when flush() is called
143
+ * auditor.audit('user.created', { userId: user.id });
144
+ *
145
+ * return user;
146
+ * });
147
+ * // After handler, adaptor calls auditor.flush() which uses the stored transaction
148
+ * ```
149
+ */
150
+ setTransaction(trx: TTransaction): void;
151
+
152
+ /**
153
+ * Get the currently set transaction context.
154
+ * Returns undefined if no transaction has been set.
155
+ */
156
+ getTransaction(): TTransaction | undefined;
157
+ }
@@ -0,0 +1,141 @@
1
+ import { nanoid } from 'nanoid';
2
+ import type { Auditor } from './Auditor';
3
+ import type { AuditStorage } from './storage';
4
+ import type {
5
+ AuditableAction,
6
+ AuditActor,
7
+ AuditMetadata,
8
+ AuditOptions,
9
+ AuditRecord,
10
+ ExtractAuditPayload,
11
+ ExtractAuditType,
12
+ } from './types';
13
+
14
+ /**
15
+ * Configuration for DefaultAuditor.
16
+ */
17
+ export interface DefaultAuditorConfig {
18
+ /** The actor performing audits (set at construction, immutable) */
19
+ actor: AuditActor;
20
+ /** Storage backend for persisting audits */
21
+ storage: AuditStorage;
22
+ /** Optional metadata to attach to all audits */
23
+ metadata?: AuditMetadata;
24
+ /** Optional custom ID generator (defaults to nanoid) */
25
+ generateId?: () => string;
26
+ }
27
+
28
+ /**
29
+ * Default implementation of the Auditor interface.
30
+ * Collects audit records in memory and flushes to storage.
31
+ *
32
+ * @template TAuditAction - Union of all allowed audit action types
33
+ * @template TTransaction - Transaction type (e.g., Kysely Transaction)
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const auditor = new DefaultAuditor<AppAuditAction>({
38
+ * actor: { id: 'user-123', type: 'user' },
39
+ * storage: auditStorage,
40
+ * metadata: { requestId: 'req-456', endpoint: '/users' },
41
+ * });
42
+ *
43
+ * auditor.audit('user.created', { userId: '789', email: 'test@example.com' });
44
+ *
45
+ * // Flush inside transaction
46
+ * await auditor.flush(trx);
47
+ * ```
48
+ */
49
+ export class DefaultAuditor<
50
+ TAuditAction extends AuditableAction<string, unknown> = AuditableAction<
51
+ string,
52
+ unknown
53
+ >,
54
+ TTransaction = unknown,
55
+ > implements Auditor<TAuditAction, TTransaction>
56
+ {
57
+ readonly actor: AuditActor;
58
+ private readonly storage: AuditStorage;
59
+ private metadata?: AuditMetadata;
60
+ private readonly generateId: () => string;
61
+ private records: AuditRecord[] = [];
62
+ private transaction?: TTransaction;
63
+
64
+ constructor(config: DefaultAuditorConfig) {
65
+ this.actor = config.actor;
66
+ this.storage = config.storage;
67
+ this.metadata = config.metadata;
68
+ this.generateId = config.generateId ?? (() => nanoid());
69
+ }
70
+
71
+ audit<TType extends ExtractAuditType<TAuditAction>>(
72
+ type: TType,
73
+ payload: ExtractAuditPayload<TAuditAction, TType>,
74
+ options?: AuditOptions,
75
+ ): void {
76
+ const record: AuditRecord = {
77
+ id: this.generateId(),
78
+ type,
79
+ operation: options?.operation ?? 'CUSTOM',
80
+ table: options?.table,
81
+ entityId: options?.entityId,
82
+ oldValues: options?.oldValues,
83
+ newValues: options?.newValues,
84
+ payload,
85
+ timestamp: new Date(),
86
+ actor: this.actor,
87
+ metadata: this.metadata,
88
+ };
89
+
90
+ this.records.push(record);
91
+ }
92
+
93
+ record(record: Omit<AuditRecord, 'id' | 'timestamp' | 'actor'>): void {
94
+ const fullRecord: AuditRecord = {
95
+ ...record,
96
+ id: this.generateId(),
97
+ timestamp: new Date(),
98
+ actor: this.actor,
99
+ metadata: this.metadata
100
+ ? { ...this.metadata, ...record.metadata }
101
+ : record.metadata,
102
+ };
103
+
104
+ this.records.push(fullRecord);
105
+ }
106
+
107
+ getRecords(): AuditRecord[] {
108
+ return [...this.records];
109
+ }
110
+
111
+ async flush(trx?: TTransaction): Promise<void> {
112
+ if (this.records.length === 0) {
113
+ return;
114
+ }
115
+
116
+ const recordsToFlush = [...this.records];
117
+ this.records = [];
118
+
119
+ // Use explicitly passed transaction, or fall back to stored transaction
120
+ const transactionToUse = trx ?? this.transaction;
121
+ await this.storage.write(recordsToFlush, transactionToUse);
122
+ }
123
+
124
+ setTransaction(trx: TTransaction): void {
125
+ this.transaction = trx;
126
+ }
127
+
128
+ getTransaction(): TTransaction | undefined {
129
+ return this.transaction;
130
+ }
131
+
132
+ clear(): void {
133
+ this.records = [];
134
+ }
135
+
136
+ addMetadata(metadata: AuditMetadata): void {
137
+ this.metadata = this.metadata
138
+ ? { ...this.metadata, ...metadata }
139
+ : metadata;
140
+ }
141
+ }