@geekmidas/audit 10.0.0-alpha.99 → 10.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.
- package/CHANGELOG.md +109 -0
- package/README.md +2 -2
- package/TECHNICAL.md +937 -0
- package/dist/{Auditor-BBXXCD0v.d.cts → Auditor-CW2NrfHS.d.mts} +7 -2
- package/dist/Auditor-CW2NrfHS.d.mts.map +1 -0
- package/dist/{Auditor-BBXXCD0v.d.mts → Auditor-DpBHXIJM.d.cts} +7 -2
- package/dist/Auditor-DpBHXIJM.d.cts.map +1 -0
- package/dist/Auditor.d.cts +1 -1
- package/dist/Auditor.d.mts +1 -1
- package/dist/Auditor.mjs +0 -1
- package/dist/DefaultAuditor-BAVnNmRh.mjs +96 -0
- package/dist/DefaultAuditor-BAVnNmRh.mjs.map +1 -0
- package/dist/DefaultAuditor-DlJ1ENfx.d.mts +59 -0
- package/dist/DefaultAuditor-DlJ1ENfx.d.mts.map +1 -0
- package/dist/DefaultAuditor-GRFI86N_.d.cts +59 -0
- package/dist/DefaultAuditor-GRFI86N_.d.cts.map +1 -0
- package/dist/DefaultAuditor-JbZ_BQfh.cjs +102 -0
- package/dist/DefaultAuditor-JbZ_BQfh.cjs.map +1 -0
- package/dist/DefaultAuditor.cjs +2 -95
- package/dist/DefaultAuditor.d.cts +3 -55
- package/dist/DefaultAuditor.d.mts +3 -55
- package/dist/DefaultAuditor.mjs +2 -94
- package/dist/cache-BbIl31RL.mjs +167 -0
- package/dist/cache-BbIl31RL.mjs.map +1 -0
- package/dist/cache-Bih6HxY6.d.cts +96 -0
- package/dist/cache-Bih6HxY6.d.cts.map +1 -0
- package/dist/cache-l7jAptBl.cjs +173 -0
- package/dist/cache-l7jAptBl.cjs.map +1 -0
- package/dist/cache-wET57t6d.d.mts +96 -0
- package/dist/cache-wET57t6d.d.mts.map +1 -0
- package/dist/cache.cjs +2 -166
- package/dist/cache.d.cts +3 -92
- package/dist/cache.d.mts +3 -92
- package/dist/cache.mjs +2 -165
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +3 -2
- package/dist/knex.cjs +9 -9
- package/dist/knex.cjs.map +1 -1
- package/dist/knex.d.cts +11 -9
- package/dist/knex.d.cts.map +1 -1
- package/dist/knex.d.mts +11 -9
- package/dist/knex.d.mts.map +1 -1
- package/dist/knex.mjs +7 -7
- package/dist/knex.mjs.map +1 -1
- package/dist/kysely.cjs +9 -9
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +11 -9
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +11 -9
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +7 -7
- package/dist/kysely.mjs.map +1 -1
- package/dist/memory.cjs +7 -6
- package/dist/memory.cjs.map +1 -1
- package/dist/memory.d.cts +7 -3
- package/dist/memory.d.cts.map +1 -1
- package/dist/memory.d.mts +7 -3
- package/dist/memory.d.mts.map +1 -1
- package/dist/memory.mjs +4 -3
- package/dist/memory.mjs.map +1 -1
- package/dist/storage.d.cts +1 -1
- package/dist/storage.d.mts +1 -1
- package/dist/storage.mjs +0 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/types.mjs +0 -1
- package/package.json +13 -19
- package/src/Auditor.ts +157 -0
- package/src/DefaultAuditor.ts +141 -0
- package/src/__tests__/CacheAuditStorage.spec.ts +382 -0
- package/src/__tests__/DefaultAuditor.spec.ts +529 -0
- package/src/__tests__/InMemoryAuditStorage.spec.ts +317 -0
- package/src/__tests__/KnexAuditStorage.integration.spec.ts +739 -0
- package/src/__tests__/KyselyAuditStorage.integration.spec.ts +518 -0
- package/src/__tests__/KyselyAuditStorage.spec.ts +443 -0
- package/src/cache.ts +263 -0
- package/src/index.ts +23 -0
- package/src/knex.ts +430 -0
- package/src/kysely.ts +424 -0
- package/src/memory.ts +50 -0
- package/src/storage.ts +203 -0
- package/src/types.ts +154 -0
- package/tsconfig.json +9 -0
- package/dist/Auditor-BBXXCD0v.d.cts.map +0 -1
- package/dist/Auditor-BBXXCD0v.d.mts.map +0 -1
- package/dist/DefaultAuditor.cjs.map +0 -1
- package/dist/DefaultAuditor.d.cts.map +0 -1
- package/dist/DefaultAuditor.d.mts.map +0 -1
- package/dist/DefaultAuditor.mjs.map +0 -1
- package/dist/cache.cjs.map +0 -1
- package/dist/cache.d.cts.map +0 -1
- package/dist/cache.d.mts.map +0 -1
- package/dist/cache.mjs.map +0 -1
package/src/types.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { InferStandardSchema } from '@geekmidas/schema';
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents an auditable action with a type and payload.
|
|
6
|
+
* Similar to PublishableMessage in @geekmidas/events.
|
|
7
|
+
*
|
|
8
|
+
* @template TType - The audit type/name (e.g., 'user.created')
|
|
9
|
+
* @template TPayload - The audit payload data
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* type AppAuditAction =
|
|
14
|
+
* | AuditableAction<'user.created', { userId: string; email: string }>
|
|
15
|
+
* | AuditableAction<'user.updated', { userId: string; changes: string[] }>
|
|
16
|
+
* | AuditableAction<'order.placed', { orderId: string; total: number }>;
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type AuditableAction<TType extends string, TPayload = unknown> = {
|
|
20
|
+
type: TType;
|
|
21
|
+
payload: TPayload;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Extract the type string from an AuditableAction union.
|
|
26
|
+
*/
|
|
27
|
+
export type ExtractAuditType<T extends AuditableAction<string, unknown>> =
|
|
28
|
+
T extends AuditableAction<infer TType, unknown> ? TType : never;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extract the payload for a specific audit type from an AuditableAction union.
|
|
32
|
+
*/
|
|
33
|
+
export type ExtractAuditPayload<
|
|
34
|
+
T extends AuditableAction<string, unknown>,
|
|
35
|
+
TType extends ExtractAuditType<T>,
|
|
36
|
+
> = T extends AuditableAction<TType, infer TPayload> ? TPayload : never;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Audit operation types for database auditing.
|
|
40
|
+
*/
|
|
41
|
+
export type AuditOperation = 'INSERT' | 'UPDATE' | 'DELETE' | 'CUSTOM';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Represents the actor who performed an audited action.
|
|
45
|
+
*/
|
|
46
|
+
export interface AuditActor {
|
|
47
|
+
/** Unique identifier for the actor (user ID, service ID, etc.) */
|
|
48
|
+
id?: string;
|
|
49
|
+
/** Type of actor ('user', 'system', 'service', etc.) */
|
|
50
|
+
type?: string;
|
|
51
|
+
/** Additional actor properties */
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Metadata associated with an audit record.
|
|
57
|
+
*/
|
|
58
|
+
export interface AuditMetadata {
|
|
59
|
+
/** Request correlation ID */
|
|
60
|
+
requestId?: string;
|
|
61
|
+
/** Which endpoint was called */
|
|
62
|
+
endpoint?: string;
|
|
63
|
+
/** HTTP method */
|
|
64
|
+
method?: string;
|
|
65
|
+
/** Client IP address */
|
|
66
|
+
ip?: string;
|
|
67
|
+
/** Client user agent */
|
|
68
|
+
userAgent?: string;
|
|
69
|
+
/** Additional metadata */
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A complete audit record representing a tracked action.
|
|
75
|
+
*/
|
|
76
|
+
export interface AuditRecord<TPayload = unknown> {
|
|
77
|
+
/** Unique identifier for this audit record */
|
|
78
|
+
id: string;
|
|
79
|
+
/** Audit type (e.g., 'user.created', 'order.placed') */
|
|
80
|
+
type: string;
|
|
81
|
+
/** Operation type for database audits */
|
|
82
|
+
operation: AuditOperation;
|
|
83
|
+
/** Database table name (for database operations) */
|
|
84
|
+
table?: string;
|
|
85
|
+
/** Entity primary key(s) */
|
|
86
|
+
entityId?: string | Record<string, unknown>;
|
|
87
|
+
/** Previous state (for UPDATE/DELETE) */
|
|
88
|
+
oldValues?: Record<string, unknown>;
|
|
89
|
+
/** New state (for INSERT/UPDATE) */
|
|
90
|
+
newValues?: Record<string, unknown>;
|
|
91
|
+
/** Custom payload (for CUSTOM operations) */
|
|
92
|
+
payload?: TPayload;
|
|
93
|
+
/** When the audit was recorded */
|
|
94
|
+
timestamp: Date;
|
|
95
|
+
/** Who performed the action */
|
|
96
|
+
actor?: AuditActor;
|
|
97
|
+
/** Request context */
|
|
98
|
+
metadata?: AuditMetadata;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Options for manual audit calls.
|
|
103
|
+
*/
|
|
104
|
+
export interface AuditOptions {
|
|
105
|
+
/** Entity primary key(s) for easier querying */
|
|
106
|
+
entityId?: string | Record<string, unknown>;
|
|
107
|
+
/** Database table name */
|
|
108
|
+
table?: string;
|
|
109
|
+
/** Operation type (defaults to 'CUSTOM') */
|
|
110
|
+
operation?: AuditOperation;
|
|
111
|
+
/** Previous state */
|
|
112
|
+
oldValues?: Record<string, unknown>;
|
|
113
|
+
/** New state */
|
|
114
|
+
newValues?: Record<string, unknown>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Mapped audit definition for declarative auditing.
|
|
119
|
+
* Similar to MappedEvent in @geekmidas/events.
|
|
120
|
+
*/
|
|
121
|
+
export interface MappedAudit<
|
|
122
|
+
TAuditAction extends AuditableAction<string, unknown>,
|
|
123
|
+
TOutput extends StandardSchemaV1 | undefined = undefined,
|
|
124
|
+
> {
|
|
125
|
+
/** The audit type - must be a valid type from the AuditableAction union */
|
|
126
|
+
type: ExtractAuditType<TAuditAction>;
|
|
127
|
+
/** Function to extract payload from the response */
|
|
128
|
+
payload: (
|
|
129
|
+
response: InferStandardSchema<TOutput>,
|
|
130
|
+
) => ExtractAuditPayload<TAuditAction, ExtractAuditType<TAuditAction>>;
|
|
131
|
+
/** Optional condition - only audit if this returns true */
|
|
132
|
+
when?: (response: InferStandardSchema<TOutput>) => boolean;
|
|
133
|
+
/** Optional entity ID extractor for easier querying */
|
|
134
|
+
entityId?: (
|
|
135
|
+
response: InferStandardSchema<TOutput>,
|
|
136
|
+
) => string | Record<string, unknown>;
|
|
137
|
+
/** Optional table name for database association */
|
|
138
|
+
table?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Extract the AuditableAction type from an Auditor.
|
|
143
|
+
*/
|
|
144
|
+
export type ExtractAuditorAction<T> = T extends Auditor<infer A> ? A : never;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Extract the AuditableAction type from an AuditStorage.
|
|
148
|
+
*/
|
|
149
|
+
export type ExtractStorageAuditAction<T> =
|
|
150
|
+
T extends AuditStorage<infer A> ? A : AuditableAction<string, unknown>;
|
|
151
|
+
|
|
152
|
+
// Forward declaration for Auditor type extraction
|
|
153
|
+
import type { Auditor } from './Auditor';
|
|
154
|
+
import type { AuditStorage } from './storage';
|
package/tsconfig.json
ADDED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Auditor-BBXXCD0v.d.cts","names":[],"sources":["../src/storage.ts","../src/types.ts","../src/Auditor.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;UAuBiB,wBAAwB;;EAExC,eAAe,KAAK;;EAEpB,MAAM,MAAM,eAAe;;;;;UAMX;;EAEhB;;EAEA,oBAAoB;;EAEpB;;EAEA;;EAEA,OAAO;;EAEP,KAAK;;EAEL;;EAEA;;EAEA;;EAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAgCgB,aAChB,qBAAqB,mCAAmC;;WAM/C,oBAAoB;;;;;;;;E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3C;;;;UAKK;;EAEhB;;EAEA;;GAEC;;;;;UAMe;;EAEhB;;EAEA;;EAEA;;EAEA;;EAEA;;GAEC;;;;;UAMe,YAAY;;EAE5B;;EAEA;;EAEA,WAAW;;EAEX;;EAEA,oBAAoB;;EAEpB,YAAY;;EAEZ,YAAY;;EAEZ,UAAU;;EAEV,WAAW;;EAEX,QAAQ;;EAER,WAAW;;;;;UAMK;;EAEhB,oBAAoB;;EAEpB;;EAEA,YAAY;;EAEZ,YAAY;;EAEZ,YAAY;;;;;;UAOI,YAChB,qBAAqB,kCACrB,gBAAgB;;EAGhB,MAAM,iBAAiB;;EAEvB,UACC,UAAU,oBAAoB,aAC1B,oBAAoB,cAAc,iBAAiB;;EAExD,QAAQ,UAAU,oBAAoB;;EAEtC,YACC,UAAU,oBAAoB,sBACjB;;EAEd;;;;;KAMW,qBAAqB,KAAK,UAAU,cAAc,KAAK;;;;KAKvD,0BAA0B,KACrC,UAAU,mBAAmB,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;UCrHtB,QAChB,qBAAqB,mCAAmC,kCAIxD;;;;;WAMS,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;EA0BhB,MAAM,cAAc,iBAAiB,eACpC,MAAM,OACN,SAAS,oBAAoB,cAAc,QAC3C,UAAU;;;;;;;;EAUX,OAAO,QAAQ,KAAK;;;;;EAMpB,cAAc;;;;;;;EAQd,MAAM,MAAM,eAAe;;;;;EAM3B;;;;;;;;;;;;;;;;;;;EAoBA,YAAY,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;EA2BtB,eAAe,KAAK;;;;;EAMpB,kBAAkB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultAuditor.cjs","names":[],"sources":["../src/DefaultAuditor.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type { Auditor } from './Auditor';\nimport type { AuditStorage } from './storage';\nimport type {\n\tAuditActor,\n\tAuditableAction,\n\tAuditMetadata,\n\tAuditOptions,\n\tAuditRecord,\n\tExtractAuditPayload,\n\tExtractAuditType,\n} from './types';\n\n/**\n * Configuration for DefaultAuditor.\n */\nexport interface DefaultAuditorConfig {\n\t/** The actor performing audits (set at construction, immutable) */\n\tactor: AuditActor;\n\t/** Storage backend for persisting audits */\n\tstorage: AuditStorage;\n\t/** Optional metadata to attach to all audits */\n\tmetadata?: AuditMetadata;\n\t/** Optional custom ID generator (defaults to nanoid) */\n\tgenerateId?: () => string;\n}\n\n/**\n * Default implementation of the Auditor interface.\n * Collects audit records in memory and flushes to storage.\n *\n * @template TAuditAction - Union of all allowed audit action types\n * @template TTransaction - Transaction type (e.g., Kysely Transaction)\n *\n * @example\n * ```typescript\n * const auditor = new DefaultAuditor<AppAuditAction>({\n * actor: { id: 'user-123', type: 'user' },\n * storage: auditStorage,\n * metadata: { requestId: 'req-456', endpoint: '/users' },\n * });\n *\n * auditor.audit('user.created', { userId: '789', email: 'test@example.com' });\n *\n * // Flush inside transaction\n * await auditor.flush(trx);\n * ```\n */\nexport class DefaultAuditor<\n\tTAuditAction extends AuditableAction<string, unknown> = AuditableAction<\n\t\tstring,\n\t\tunknown\n\t>,\n\tTTransaction = unknown,\n> implements Auditor<TAuditAction, TTransaction>\n{\n\treadonly actor: AuditActor;\n\tprivate readonly storage: AuditStorage;\n\tprivate metadata?: AuditMetadata;\n\tprivate readonly generateId: () => string;\n\tprivate records: AuditRecord[] = [];\n\tprivate transaction?: TTransaction;\n\n\tconstructor(config: DefaultAuditorConfig) {\n\t\tthis.actor = config.actor;\n\t\tthis.storage = config.storage;\n\t\tthis.metadata = config.metadata;\n\t\tthis.generateId = config.generateId ?? (() => nanoid());\n\t}\n\n\taudit<TType extends ExtractAuditType<TAuditAction>>(\n\t\ttype: TType,\n\t\tpayload: ExtractAuditPayload<TAuditAction, TType>,\n\t\toptions?: AuditOptions,\n\t): void {\n\t\tconst record: AuditRecord = {\n\t\t\tid: this.generateId(),\n\t\t\ttype,\n\t\t\toperation: options?.operation ?? 'CUSTOM',\n\t\t\ttable: options?.table,\n\t\t\tentityId: options?.entityId,\n\t\t\toldValues: options?.oldValues,\n\t\t\tnewValues: options?.newValues,\n\t\t\tpayload,\n\t\t\ttimestamp: new Date(),\n\t\t\tactor: this.actor,\n\t\t\tmetadata: this.metadata,\n\t\t};\n\n\t\tthis.records.push(record);\n\t}\n\n\trecord(record: Omit<AuditRecord, 'id' | 'timestamp' | 'actor'>): void {\n\t\tconst fullRecord: AuditRecord = {\n\t\t\t...record,\n\t\t\tid: this.generateId(),\n\t\t\ttimestamp: new Date(),\n\t\t\tactor: this.actor,\n\t\t\tmetadata: this.metadata\n\t\t\t\t? { ...this.metadata, ...record.metadata }\n\t\t\t\t: record.metadata,\n\t\t};\n\n\t\tthis.records.push(fullRecord);\n\t}\n\n\tgetRecords(): AuditRecord[] {\n\t\treturn [...this.records];\n\t}\n\n\tasync flush(trx?: TTransaction): Promise<void> {\n\t\tif (this.records.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst recordsToFlush = [...this.records];\n\t\tthis.records = [];\n\n\t\t// Use explicitly passed transaction, or fall back to stored transaction\n\t\tconst transactionToUse = trx ?? this.transaction;\n\t\tawait this.storage.write(recordsToFlush, transactionToUse);\n\t}\n\n\tsetTransaction(trx: TTransaction): void {\n\t\tthis.transaction = trx;\n\t}\n\n\tgetTransaction(): TTransaction | undefined {\n\t\treturn this.transaction;\n\t}\n\n\tclear(): void {\n\t\tthis.records = [];\n\t}\n\n\taddMetadata(metadata: AuditMetadata): void {\n\t\tthis.metadata = this.metadata\n\t\t\t? { ...this.metadata, ...metadata }\n\t\t\t: metadata;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgDA,IAAa,iBAAb,MAOA;CACC;CACA;CACA;CACA;CACA,UAAiC,CAAC;CAClC;CAEA,YAAY,QAA8B;EACzC,KAAK,QAAQ,OAAO;EACpB,KAAK,UAAU,OAAO;EACtB,KAAK,WAAW,OAAO;EACvB,KAAK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W;;EAEX;;;;;;;;;;;;;;;;;;;;;;;qBAwBY,eACZ,qBAAqB,mCAAmC,kCAIxD,mCACY,QAAQ,cAAc;WAEzB,OAAO;mBACC;UACT;mBACS;UACT;UACA;EAER,YAAY,QAAQ;EAOpB,MAAM,cAAc,iBAAiB,eACpC,MAAM,OACN,SAAS,oBAAoB,cAAc,QAC3C,UAAU;EAmBX,OAAO,QAAQ,KAAK;EAcpB,cAAc;EAIR,MAAM,MAAM,eAAe;EAajC,eAAe,KAAK;EAIpB,kBAAkB;EAIlB;EAIA,YAAY,UAAU"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultAuditor.d.mts","names":[],"sources":["../src/DefaultAuditor.ts"],"mappings":";;;;;iBAgBiB;;EAEhB,OAAO;;EAEP,SAAS;;EAET,WAAW;;EAEX;;;;;;;;;;;;;;;;;;;;;;;qBAwBY,eACZ,qBAAqB,mCAAmC,kCAIxD,mCACY,QAAQ,cAAc;WAEzB,OAAO;mBACC;UACT;mBACS;UACT;UACA;EAER,YAAY,QAAQ;EAOpB,MAAM,cAAc,iBAAiB,eACpC,MAAM,OACN,SAAS,oBAAoB,cAAc,QAC3C,UAAU;EAmBX,OAAO,QAAQ,KAAK;EAcpB,cAAc;EAIR,MAAM,MAAM,eAAe;EAajC,eAAe,KAAK;EAIpB,kBAAkB;EAIlB;EAIA,YAAY,UAAU"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultAuditor.mjs","names":[],"sources":["../src/DefaultAuditor.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type { Auditor } from './Auditor';\nimport type { AuditStorage } from './storage';\nimport type {\n\tAuditActor,\n\tAuditableAction,\n\tAuditMetadata,\n\tAuditOptions,\n\tAuditRecord,\n\tExtractAuditPayload,\n\tExtractAuditType,\n} from './types';\n\n/**\n * Configuration for DefaultAuditor.\n */\nexport interface DefaultAuditorConfig {\n\t/** The actor performing audits (set at construction, immutable) */\n\tactor: AuditActor;\n\t/** Storage backend for persisting audits */\n\tstorage: AuditStorage;\n\t/** Optional metadata to attach to all audits */\n\tmetadata?: AuditMetadata;\n\t/** Optional custom ID generator (defaults to nanoid) */\n\tgenerateId?: () => string;\n}\n\n/**\n * Default implementation of the Auditor interface.\n * Collects audit records in memory and flushes to storage.\n *\n * @template TAuditAction - Union of all allowed audit action types\n * @template TTransaction - Transaction type (e.g., Kysely Transaction)\n *\n * @example\n * ```typescript\n * const auditor = new DefaultAuditor<AppAuditAction>({\n * actor: { id: 'user-123', type: 'user' },\n * storage: auditStorage,\n * metadata: { requestId: 'req-456', endpoint: '/users' },\n * });\n *\n * auditor.audit('user.created', { userId: '789', email: 'test@example.com' });\n *\n * // Flush inside transaction\n * await auditor.flush(trx);\n * ```\n */\nexport class DefaultAuditor<\n\tTAuditAction extends AuditableAction<string, unknown> = AuditableAction<\n\t\tstring,\n\t\tunknown\n\t>,\n\tTTransaction = unknown,\n> implements Auditor<TAuditAction, TTransaction>\n{\n\treadonly actor: AuditActor;\n\tprivate readonly storage: AuditStorage;\n\tprivate metadata?: AuditMetadata;\n\tprivate readonly generateId: () => string;\n\tprivate records: AuditRecord[] = [];\n\tprivate transaction?: TTransaction;\n\n\tconstructor(config: DefaultAuditorConfig) {\n\t\tthis.actor = config.actor;\n\t\tthis.storage = config.storage;\n\t\tthis.metadata = config.metadata;\n\t\tthis.generateId = config.generateId ?? (() => nanoid());\n\t}\n\n\taudit<TType extends ExtractAuditType<TAuditAction>>(\n\t\ttype: TType,\n\t\tpayload: ExtractAuditPayload<TAuditAction, TType>,\n\t\toptions?: AuditOptions,\n\t): void {\n\t\tconst record: AuditRecord = {\n\t\t\tid: this.generateId(),\n\t\t\ttype,\n\t\t\toperation: options?.operation ?? 'CUSTOM',\n\t\t\ttable: options?.table,\n\t\t\tentityId: options?.entityId,\n\t\t\toldValues: options?.oldValues,\n\t\t\tnewValues: options?.newValues,\n\t\t\tpayload,\n\t\t\ttimestamp: new Date(),\n\t\t\tactor: this.actor,\n\t\t\tmetadata: this.metadata,\n\t\t};\n\n\t\tthis.records.push(record);\n\t}\n\n\trecord(record: Omit<AuditRecord, 'id' | 'timestamp' | 'actor'>): void {\n\t\tconst fullRecord: AuditRecord = {\n\t\t\t...record,\n\t\t\tid: this.generateId(),\n\t\t\ttimestamp: new Date(),\n\t\t\tactor: this.actor,\n\t\t\tmetadata: this.metadata\n\t\t\t\t? { ...this.metadata, ...record.metadata }\n\t\t\t\t: record.metadata,\n\t\t};\n\n\t\tthis.records.push(fullRecord);\n\t}\n\n\tgetRecords(): AuditRecord[] {\n\t\treturn [...this.records];\n\t}\n\n\tasync flush(trx?: TTransaction): Promise<void> {\n\t\tif (this.records.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst recordsToFlush = [...this.records];\n\t\tthis.records = [];\n\n\t\t// Use explicitly passed transaction, or fall back to stored transaction\n\t\tconst transactionToUse = trx ?? this.transaction;\n\t\tawait this.storage.write(recordsToFlush, transactionToUse);\n\t}\n\n\tsetTransaction(trx: TTransaction): void {\n\t\tthis.transaction = trx;\n\t}\n\n\tgetTransaction(): TTransaction | undefined {\n\t\treturn this.transaction;\n\t}\n\n\tclear(): void {\n\t\tthis.records = [];\n\t}\n\n\taddMetadata(metadata: AuditMetadata): void {\n\t\tthis.metadata = this.metadata\n\t\t\t? { ...this.metadata, ...metadata }\n\t\t\t: metadata;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAgDA,IAAa,iBAAb,MAOA;CACC;CACA;CACA;CACA;CACA,UAAiC,CAAC;CAClC;CAEA,YAAY,QAA8B;EACzC,KAAK,QAAQ,OAAO;EACpB,KAAK,UAAU,OAAO;EACtB,KAAK,WAAW,OAAO;EACvB,KAAK,aAAa,OAAO,qBAAqB,OAAO;CACtD;CAEA,MACC,MACA,SACA,SACO;EACP,MAAM,SAAsB;GAC3B,IAAI,KAAK,WAAW;GACpB;GACA,WAAW,SAAS,aAAa;GACjC,OAAO,SAAS;GAChB,UAAU,SAAS;GACnB,WAAW,SAAS;GACpB,WAAW,SAAS;GACpB;GACA,2BAAW,IAAI,KAAK;GACpB,OAAO,KAAK;GACZ,UAAU,KAAK;EAChB;EAEA,KAAK,QAAQ,KAAK,MAAM;CACzB;CAEA,OAAO,QAA+D;EACrE,MAAM,aAA0B;GAC/B,GAAG;GACH,IAAI,KAAK,WAAW;GACpB,2BAAW,IAAI,KAAK;GACpB,OAAO,KAAK;GACZ,UAAU,KAAK,WACZ;IAAE,GAAG,KAAK;IAAU,GAAG,OAAO;GAAS,IACvC,OAAO;EACX;EAEA,KAAK,QAAQ,KAAK,UAAU;CAC7B;CAEA,aAA4B;EAC3B,OAAO,CAAC,GAAG,KAAK,OAAO;CACxB;CAEA,MAAM,MAAM,KAAmC;EAC9C,IAAI,KAAK,QAAQ,WAAW,GAC3B;EAGD,MAAM,iBAAiB,CAAC,GAAG,KAAK,OAAO;EACvC,KAAK,UAAU,CAAC;EAGhB,MAAM,mBAAmB,OAAO,KAAK;EACrC,MAAM,KAAK,QAAQ,MAAM,gBAAgB,gBAAgB;CAC1D;CAEA,eAAe,KAAyB;EACvC,KAAK,cAAc;CACpB;CAEA,iBAA2C;EAC1C,OAAO,KAAK;CACb;CAEA,QAAc;EACb,KAAK,UAAU,CAAC;CACjB;CAEA,YAAY,UAA+B;EAC1C,KAAK,WAAW,KAAK,WAClB;GAAE,GAAG,KAAK;GAAU,GAAG;EAAS,IAChC;CACJ;AACD"}
|
package/dist/cache.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.cjs","names":[],"sources":["../src/cache.ts"],"sourcesContent":["import type { Cache } from '@geekmidas/cache';\nimport type { AuditQueryOptions, AuditStorage } from './storage';\nimport type { AuditableAction, AuditRecord } from './types';\n\n/**\n * Configuration for CacheAuditStorage.\n */\nexport interface CacheAuditStorageConfig {\n\t/** Cache instance to use for storage */\n\tcache: Cache;\n\t/**\n\t * Key prefix for audit records.\n\t * Records are stored as `${prefix}:${id}`.\n\t * @default 'audit'\n\t */\n\tprefix?: string;\n\t/**\n\t * TTL (time-to-live) in seconds for audit records.\n\t * If not set, records use the cache's default TTL.\n\t */\n\tttl?: number;\n}\n\n/**\n * Cache-based audit storage implementation.\n * Uses any @geekmidas/cache implementation for storage.\n *\n * Best suited for:\n * - Development and testing\n * - Temporary audit logs that don't need persistence\n * - Applications with existing cache infrastructure\n * - Distributed systems needing shared audit state (with Redis/Upstash)\n *\n * Note: Query performance may degrade with large numbers of records\n * since filtering happens in memory after fetching all records.\n *\n * @template TAuditAction - Optional type parameter for type-safe audit actions.\n *\n * @example\n * ```typescript\n * import { CacheAuditStorage } from '@geekmidas/audit/cache';\n * import { InMemoryCache } from '@geekmidas/cache/memory';\n * import { UpstashCache } from '@geekmidas/cache/upstash';\n *\n * // With in-memory cache (development/testing)\n * const storage = new CacheAuditStorage({\n * cache: new InMemoryCache(),\n * ttl: 86400, // 24 hours\n * });\n *\n * // With Upstash Redis (production)\n * const storage = new CacheAuditStorage({\n * cache: new UpstashCache({\n * url: process.env.UPSTASH_REDIS_URL,\n * token: process.env.UPSTASH_REDIS_TOKEN,\n * }),\n * ttl: 604800, // 7 days\n * });\n * ```\n */\nexport class CacheAuditStorage<\n\tTAuditAction extends AuditableAction<string, unknown> = AuditableAction<\n\t\tstring,\n\t\tunknown\n\t>,\n> implements AuditStorage<TAuditAction>\n{\n\tprivate readonly cache: Cache;\n\tprivate readonly prefix: string;\n\tprivate readonly ttl?: number;\n\tprivate readonly indexKey: string;\n\n\tconstructor(config: CacheAuditStorageConfig) {\n\t\tthis.cache = config.cache;\n\t\tthis.prefix = config.prefix ?? 'audit';\n\t\tthis.ttl = config.ttl;\n\t\tthis.indexKey = `${this.prefix}:__index__`;\n\t}\n\n\t/**\n\t * Write audit records to cache.\n\t */\n\tasync write(records: AuditRecord[]): Promise<void> {\n\t\tif (records.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get existing index\n\t\tconst existingIds = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\t\tconst newIds: string[] = [];\n\n\t\t// Write each record\n\t\tfor (const record of records) {\n\t\t\tconst key = this.getRecordKey(record.id);\n\t\t\t// Serialize Date to ISO string for cache storage\n\t\t\tconst serialized = this.serializeRecord(record);\n\t\t\tawait this.cache.set(key, serialized, this.ttl);\n\t\t\tnewIds.push(record.id);\n\t\t}\n\n\t\t// Update index with new IDs\n\t\tconst updatedIds = [...existingIds, ...newIds];\n\t\tawait this.cache.set(this.indexKey, updatedIds, this.ttl);\n\t}\n\n\t/**\n\t * Query audit records from cache.\n\t */\n\tasync query(options: AuditQueryOptions): Promise<AuditRecord[]> {\n\t\tconst allRecords = await this.getAllRecords();\n\t\tlet results = this.applyFilters(allRecords, options);\n\n\t\t// Ordering\n\t\tconst orderBy = options.orderBy ?? 'timestamp';\n\t\tconst orderDirection = options.orderDirection ?? 'desc';\n\t\tresults.sort((a, b) => {\n\t\t\tconst aValue = orderBy === 'timestamp' ? a.timestamp.getTime() : a.type;\n\t\t\tconst bValue = orderBy === 'timestamp' ? b.timestamp.getTime() : b.type;\n\t\t\tif (aValue < bValue) return orderDirection === 'asc' ? -1 : 1;\n\t\t\tif (aValue > bValue) return orderDirection === 'asc' ? 1 : -1;\n\t\t\treturn 0;\n\t\t});\n\n\t\t// Pagination\n\t\tif (options.offset !== undefined) {\n\t\t\tresults = results.slice(options.offset);\n\t\t}\n\t\tif (options.limit !== undefined) {\n\t\t\tresults = results.slice(0, options.limit);\n\t\t}\n\n\t\treturn results;\n\t}\n\n\t/**\n\t * Count audit records matching filters.\n\t */\n\tasync count(\n\t\toptions: Omit<AuditQueryOptions, 'limit' | 'offset'>,\n\t): Promise<number> {\n\t\tconst allRecords = await this.getAllRecords();\n\t\treturn this.applyFilters(allRecords, options).length;\n\t}\n\n\t/**\n\t * Get all stored records (for testing/debugging).\n\t */\n\tasync getRecords(): Promise<AuditRecord[]> {\n\t\treturn this.getAllRecords();\n\t}\n\n\t/**\n\t * Clear all stored records.\n\t */\n\tasync clear(): Promise<void> {\n\t\tconst ids = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\n\t\t// Delete all records\n\t\tfor (const id of ids) {\n\t\t\tawait this.cache.delete(this.getRecordKey(id));\n\t\t}\n\n\t\t// Delete index\n\t\tawait this.cache.delete(this.indexKey);\n\t}\n\n\tprivate getRecordKey(id: string): string {\n\t\treturn `${this.prefix}:${id}`;\n\t}\n\n\tprivate async getAllRecords(): Promise<AuditRecord[]> {\n\t\tconst ids = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\t\tconst records: AuditRecord[] = [];\n\t\tconst validIds: string[] = [];\n\n\t\tfor (const id of ids) {\n\t\t\tconst key = this.getRecordKey(id);\n\t\t\tconst serialized = await this.cache.get<SerializedAuditRecord>(key);\n\t\t\tif (serialized) {\n\t\t\t\trecords.push(this.deserializeRecord(serialized));\n\t\t\t\tvalidIds.push(id);\n\t\t\t}\n\t\t}\n\n\t\t// Clean up index if some records expired\n\t\tif (validIds.length !== ids.length) {\n\t\t\tawait this.cache.set(this.indexKey, validIds, this.ttl);\n\t\t}\n\n\t\treturn records;\n\t}\n\n\tprivate serializeRecord(record: AuditRecord): SerializedAuditRecord {\n\t\treturn {\n\t\t\t...record,\n\t\t\ttimestamp: record.timestamp.toISOString(),\n\t\t};\n\t}\n\n\tprivate deserializeRecord(serialized: SerializedAuditRecord): AuditRecord {\n\t\treturn {\n\t\t\t...serialized,\n\t\t\ttimestamp: new Date(serialized.timestamp),\n\t\t};\n\t}\n\n\tprivate applyFilters(\n\t\trecords: AuditRecord[],\n\t\toptions: AuditQueryOptions,\n\t): AuditRecord[] {\n\t\treturn records.filter((record) => {\n\t\t\t// Type filter\n\t\t\tif (options.type !== undefined) {\n\t\t\t\tif (Array.isArray(options.type)) {\n\t\t\t\t\tif (!options.type.includes(record.type)) return false;\n\t\t\t\t} else {\n\t\t\t\t\tif (record.type !== options.type) return false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Entity ID filter\n\t\t\tif (options.entityId !== undefined) {\n\t\t\t\tconst entityId =\n\t\t\t\t\ttypeof options.entityId === 'string'\n\t\t\t\t\t\t? options.entityId\n\t\t\t\t\t\t: JSON.stringify(options.entityId);\n\t\t\t\tconst recordEntityId =\n\t\t\t\t\ttypeof record.entityId === 'string'\n\t\t\t\t\t\t? record.entityId\n\t\t\t\t\t\t: JSON.stringify(record.entityId);\n\t\t\t\tif (recordEntityId !== entityId) return false;\n\t\t\t}\n\n\t\t\t// Table filter\n\t\t\tif (options.table !== undefined) {\n\t\t\t\tif (record.table !== options.table) return false;\n\t\t\t}\n\n\t\t\t// Actor ID filter\n\t\t\tif (options.actorId !== undefined) {\n\t\t\t\tif (record.actor?.id !== options.actorId) return false;\n\t\t\t}\n\n\t\t\t// Date range filters\n\t\t\tif (options.from !== undefined) {\n\t\t\t\tif (record.timestamp < options.from) return false;\n\t\t\t}\n\t\t\tif (options.to !== undefined) {\n\t\t\t\tif (record.timestamp > options.to) return false;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t});\n\t}\n}\n\n/**\n * Serialized version of AuditRecord for cache storage.\n * Dates are stored as ISO strings.\n */\ntype SerializedAuditRecord = Omit<AuditRecord, 'timestamp'> & {\n\ttimestamp: string;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,IAAa,oBAAb,MAMA;CACC;CACA;CACA;CACA;CAEA,YAAY,QAAiC;EAC5C,KAAK,QAAQ,OAAO;EACpB,KAAK,SAAS,OAAO,UAAU;EAC/B,KAAK,MAAM,OAAO;EAClB,KAAK,WAAW,GAAG,KAAK,OAAO;CAChC;;;;CAKA,MAAM,MAAM,SAAuC;EAClD,IAAI,QAAQ,WAAW,GACtB;EAID,MAAM,cAAe,MAAM,KAAK,MAAM,IAAc,KAAK,QAAQ,KAAM,CAAC;EACxE,MAAM,SAAmB,CAAC;EAG1B,KAAK,MAAM,UAAU,SAAS;GAC7B,MAAM,MAA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;GAGA,IAAI,QAAQ,UAAU,KAAA,GACjB;QAAA,OAAO,UAAU,QAAQ,OAAO,OAAO;GAAA;GAI5C,IAAI,QAAQ,YAAY,KAAA,GACnB;QAAA,OAAO,OAAO,OAAO,QAAQ,SAAS,OAAO;GAAA;GAIlD,IAAI,QAAQ,SAAS,KAAA,GAChB;QAAA,OAAO,YAAY,QAAQ,MAAM,OAAO;GAAA;GAE7C,IAAI,QAAQ,OAAO,KAAA,GACd;QAAA,OAAO,YAAY,QAAQ,IAAI,OAAO;GAAA;GAG3C,OAAO;EACR,CAAC;CACF;AACD"}
|
package/dist/cache.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.cts","names":[],"sources":["../src/cache.ts"],"mappings":";;;;;;iBAOiB;;EAEhB,OAAO;;;;;;EAMP;;;;;EAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAwCY,kBACZ,qBAAqB,mCAAmC,6CAI5C,aAAa;mBAER;mBACA;mBACA;mBACA;EAEjB,YAAY,QAAQ;;;;EAUd,MAAM,SAAS,gBAAgB;;;;EA0B/B,MAAM,SAAS,oBAAoB,QAAQ;;;;EA6B3C,MACL,SAAS,KAAK,yCACZ;;;;EAQG,cAAc,QAAQ;;;;EAOtB,SAAS;UAYP;UAIM;UAsBN;UAOA;UAOA"}
|
package/dist/cache.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.mts","names":[],"sources":["../src/cache.ts"],"mappings":";;;;;;iBAOiB;;EAEhB,OAAO;;;;;;EAMP;;;;;EAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAwCY,kBACZ,qBAAqB,mCAAmC,6CAI5C,aAAa;mBAER;mBACA;mBACA;mBACA;EAEjB,YAAY,QAAQ;;;;EAUd,MAAM,SAAS,gBAAgB;;;;EA0B/B,MAAM,SAAS,oBAAoB,QAAQ;;;;EA6B3C,MACL,SAAS,KAAK,yCACZ;;;;EAQG,cAAc,QAAQ;;;;EAOtB,SAAS;UAYP;UAIM;UAsBN;UAOA;UAOA"}
|
package/dist/cache.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cache.mjs","names":[],"sources":["../src/cache.ts"],"sourcesContent":["import type { Cache } from '@geekmidas/cache';\nimport type { AuditQueryOptions, AuditStorage } from './storage';\nimport type { AuditableAction, AuditRecord } from './types';\n\n/**\n * Configuration for CacheAuditStorage.\n */\nexport interface CacheAuditStorageConfig {\n\t/** Cache instance to use for storage */\n\tcache: Cache;\n\t/**\n\t * Key prefix for audit records.\n\t * Records are stored as `${prefix}:${id}`.\n\t * @default 'audit'\n\t */\n\tprefix?: string;\n\t/**\n\t * TTL (time-to-live) in seconds for audit records.\n\t * If not set, records use the cache's default TTL.\n\t */\n\tttl?: number;\n}\n\n/**\n * Cache-based audit storage implementation.\n * Uses any @geekmidas/cache implementation for storage.\n *\n * Best suited for:\n * - Development and testing\n * - Temporary audit logs that don't need persistence\n * - Applications with existing cache infrastructure\n * - Distributed systems needing shared audit state (with Redis/Upstash)\n *\n * Note: Query performance may degrade with large numbers of records\n * since filtering happens in memory after fetching all records.\n *\n * @template TAuditAction - Optional type parameter for type-safe audit actions.\n *\n * @example\n * ```typescript\n * import { CacheAuditStorage } from '@geekmidas/audit/cache';\n * import { InMemoryCache } from '@geekmidas/cache/memory';\n * import { UpstashCache } from '@geekmidas/cache/upstash';\n *\n * // With in-memory cache (development/testing)\n * const storage = new CacheAuditStorage({\n * cache: new InMemoryCache(),\n * ttl: 86400, // 24 hours\n * });\n *\n * // With Upstash Redis (production)\n * const storage = new CacheAuditStorage({\n * cache: new UpstashCache({\n * url: process.env.UPSTASH_REDIS_URL,\n * token: process.env.UPSTASH_REDIS_TOKEN,\n * }),\n * ttl: 604800, // 7 days\n * });\n * ```\n */\nexport class CacheAuditStorage<\n\tTAuditAction extends AuditableAction<string, unknown> = AuditableAction<\n\t\tstring,\n\t\tunknown\n\t>,\n> implements AuditStorage<TAuditAction>\n{\n\tprivate readonly cache: Cache;\n\tprivate readonly prefix: string;\n\tprivate readonly ttl?: number;\n\tprivate readonly indexKey: string;\n\n\tconstructor(config: CacheAuditStorageConfig) {\n\t\tthis.cache = config.cache;\n\t\tthis.prefix = config.prefix ?? 'audit';\n\t\tthis.ttl = config.ttl;\n\t\tthis.indexKey = `${this.prefix}:__index__`;\n\t}\n\n\t/**\n\t * Write audit records to cache.\n\t */\n\tasync write(records: AuditRecord[]): Promise<void> {\n\t\tif (records.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Get existing index\n\t\tconst existingIds = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\t\tconst newIds: string[] = [];\n\n\t\t// Write each record\n\t\tfor (const record of records) {\n\t\t\tconst key = this.getRecordKey(record.id);\n\t\t\t// Serialize Date to ISO string for cache storage\n\t\t\tconst serialized = this.serializeRecord(record);\n\t\t\tawait this.cache.set(key, serialized, this.ttl);\n\t\t\tnewIds.push(record.id);\n\t\t}\n\n\t\t// Update index with new IDs\n\t\tconst updatedIds = [...existingIds, ...newIds];\n\t\tawait this.cache.set(this.indexKey, updatedIds, this.ttl);\n\t}\n\n\t/**\n\t * Query audit records from cache.\n\t */\n\tasync query(options: AuditQueryOptions): Promise<AuditRecord[]> {\n\t\tconst allRecords = await this.getAllRecords();\n\t\tlet results = this.applyFilters(allRecords, options);\n\n\t\t// Ordering\n\t\tconst orderBy = options.orderBy ?? 'timestamp';\n\t\tconst orderDirection = options.orderDirection ?? 'desc';\n\t\tresults.sort((a, b) => {\n\t\t\tconst aValue = orderBy === 'timestamp' ? a.timestamp.getTime() : a.type;\n\t\t\tconst bValue = orderBy === 'timestamp' ? b.timestamp.getTime() : b.type;\n\t\t\tif (aValue < bValue) return orderDirection === 'asc' ? -1 : 1;\n\t\t\tif (aValue > bValue) return orderDirection === 'asc' ? 1 : -1;\n\t\t\treturn 0;\n\t\t});\n\n\t\t// Pagination\n\t\tif (options.offset !== undefined) {\n\t\t\tresults = results.slice(options.offset);\n\t\t}\n\t\tif (options.limit !== undefined) {\n\t\t\tresults = results.slice(0, options.limit);\n\t\t}\n\n\t\treturn results;\n\t}\n\n\t/**\n\t * Count audit records matching filters.\n\t */\n\tasync count(\n\t\toptions: Omit<AuditQueryOptions, 'limit' | 'offset'>,\n\t): Promise<number> {\n\t\tconst allRecords = await this.getAllRecords();\n\t\treturn this.applyFilters(allRecords, options).length;\n\t}\n\n\t/**\n\t * Get all stored records (for testing/debugging).\n\t */\n\tasync getRecords(): Promise<AuditRecord[]> {\n\t\treturn this.getAllRecords();\n\t}\n\n\t/**\n\t * Clear all stored records.\n\t */\n\tasync clear(): Promise<void> {\n\t\tconst ids = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\n\t\t// Delete all records\n\t\tfor (const id of ids) {\n\t\t\tawait this.cache.delete(this.getRecordKey(id));\n\t\t}\n\n\t\t// Delete index\n\t\tawait this.cache.delete(this.indexKey);\n\t}\n\n\tprivate getRecordKey(id: string): string {\n\t\treturn `${this.prefix}:${id}`;\n\t}\n\n\tprivate async getAllRecords(): Promise<AuditRecord[]> {\n\t\tconst ids = (await this.cache.get<string[]>(this.indexKey)) ?? [];\n\t\tconst records: AuditRecord[] = [];\n\t\tconst validIds: string[] = [];\n\n\t\tfor (const id of ids) {\n\t\t\tconst key = this.getRecordKey(id);\n\t\t\tconst serialized = await this.cache.get<SerializedAuditRecord>(key);\n\t\t\tif (serialized) {\n\t\t\t\trecords.push(this.deserializeRecord(serialized));\n\t\t\t\tvalidIds.push(id);\n\t\t\t}\n\t\t}\n\n\t\t// Clean up index if some records expired\n\t\tif (validIds.length !== ids.length) {\n\t\t\tawait this.cache.set(this.indexKey, validIds, this.ttl);\n\t\t}\n\n\t\treturn records;\n\t}\n\n\tprivate serializeRecord(record: AuditRecord): SerializedAuditRecord {\n\t\treturn {\n\t\t\t...record,\n\t\t\ttimestamp: record.timestamp.toISOString(),\n\t\t};\n\t}\n\n\tprivate deserializeRecord(serialized: SerializedAuditRecord): AuditRecord {\n\t\treturn {\n\t\t\t...serialized,\n\t\t\ttimestamp: new Date(serialized.timestamp),\n\t\t};\n\t}\n\n\tprivate applyFilters(\n\t\trecords: AuditRecord[],\n\t\toptions: AuditQueryOptions,\n\t): AuditRecord[] {\n\t\treturn records.filter((record) => {\n\t\t\t// Type filter\n\t\t\tif (options.type !== undefined) {\n\t\t\t\tif (Array.isArray(options.type)) {\n\t\t\t\t\tif (!options.type.includes(record.type)) return false;\n\t\t\t\t} else {\n\t\t\t\t\tif (record.type !== options.type) return false;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Entity ID filter\n\t\t\tif (options.entityId !== undefined) {\n\t\t\t\tconst entityId =\n\t\t\t\t\ttypeof options.entityId === 'string'\n\t\t\t\t\t\t? options.entityId\n\t\t\t\t\t\t: JSON.stringify(options.entityId);\n\t\t\t\tconst recordEntityId =\n\t\t\t\t\ttypeof record.entityId === 'string'\n\t\t\t\t\t\t? record.entityId\n\t\t\t\t\t\t: JSON.stringify(record.entityId);\n\t\t\t\tif (recordEntityId !== entityId) return false;\n\t\t\t}\n\n\t\t\t// Table filter\n\t\t\tif (options.table !== undefined) {\n\t\t\t\tif (record.table !== options.table) return false;\n\t\t\t}\n\n\t\t\t// Actor ID filter\n\t\t\tif (options.actorId !== undefined) {\n\t\t\t\tif (record.actor?.id !== options.actorId) return false;\n\t\t\t}\n\n\t\t\t// Date range filters\n\t\t\tif (options.from !== undefined) {\n\t\t\t\tif (record.timestamp < options.from) return false;\n\t\t\t}\n\t\t\tif (options.to !== undefined) {\n\t\t\t\tif (record.timestamp > options.to) return false;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t});\n\t}\n}\n\n/**\n * Serialized version of AuditRecord for cache storage.\n * Dates are stored as ISO strings.\n */\ntype SerializedAuditRecord = Omit<AuditRecord, 'timestamp'> & {\n\ttimestamp: string;\n};\n"],"mappings":";;;;;;;;;;;;;;;;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