@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/dist/cache.cjs
CHANGED
|
@@ -1,167 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/cache.ts
|
|
3
|
-
/**
|
|
4
|
-
* Cache-based audit storage implementation.
|
|
5
|
-
* Uses any @geekmidas/cache implementation for storage.
|
|
6
|
-
*
|
|
7
|
-
* Best suited for:
|
|
8
|
-
* - Development and testing
|
|
9
|
-
* - Temporary audit logs that don't need persistence
|
|
10
|
-
* - Applications with existing cache infrastructure
|
|
11
|
-
* - Distributed systems needing shared audit state (with Redis/Upstash)
|
|
12
|
-
*
|
|
13
|
-
* Note: Query performance may degrade with large numbers of records
|
|
14
|
-
* since filtering happens in memory after fetching all records.
|
|
15
|
-
*
|
|
16
|
-
* @template TAuditAction - Optional type parameter for type-safe audit actions.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import { CacheAuditStorage } from '@geekmidas/audit/cache';
|
|
21
|
-
* import { InMemoryCache } from '@geekmidas/cache/memory';
|
|
22
|
-
* import { UpstashCache } from '@geekmidas/cache/upstash';
|
|
23
|
-
*
|
|
24
|
-
* // With in-memory cache (development/testing)
|
|
25
|
-
* const storage = new CacheAuditStorage({
|
|
26
|
-
* cache: new InMemoryCache(),
|
|
27
|
-
* ttl: 86400, // 24 hours
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* // With Upstash Redis (production)
|
|
31
|
-
* const storage = new CacheAuditStorage({
|
|
32
|
-
* cache: new UpstashCache({
|
|
33
|
-
* url: process.env.UPSTASH_REDIS_URL,
|
|
34
|
-
* token: process.env.UPSTASH_REDIS_TOKEN,
|
|
35
|
-
* }),
|
|
36
|
-
* ttl: 604800, // 7 days
|
|
37
|
-
* });
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
var CacheAuditStorage = class {
|
|
41
|
-
cache;
|
|
42
|
-
prefix;
|
|
43
|
-
ttl;
|
|
44
|
-
indexKey;
|
|
45
|
-
constructor(config) {
|
|
46
|
-
this.cache = config.cache;
|
|
47
|
-
this.prefix = config.prefix ?? "audit";
|
|
48
|
-
this.ttl = config.ttl;
|
|
49
|
-
this.indexKey = `${this.prefix}:__index__`;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Write audit records to cache.
|
|
53
|
-
*/
|
|
54
|
-
async write(records) {
|
|
55
|
-
if (records.length === 0) return;
|
|
56
|
-
const existingIds = await this.cache.get(this.indexKey) ?? [];
|
|
57
|
-
const newIds = [];
|
|
58
|
-
for (const record of records) {
|
|
59
|
-
const key = this.getRecordKey(record.id);
|
|
60
|
-
const serialized = this.serializeRecord(record);
|
|
61
|
-
await this.cache.set(key, serialized, this.ttl);
|
|
62
|
-
newIds.push(record.id);
|
|
63
|
-
}
|
|
64
|
-
const updatedIds = [...existingIds, ...newIds];
|
|
65
|
-
await this.cache.set(this.indexKey, updatedIds, this.ttl);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Query audit records from cache.
|
|
69
|
-
*/
|
|
70
|
-
async query(options) {
|
|
71
|
-
const allRecords = await this.getAllRecords();
|
|
72
|
-
let results = this.applyFilters(allRecords, options);
|
|
73
|
-
const orderBy = options.orderBy ?? "timestamp";
|
|
74
|
-
const orderDirection = options.orderDirection ?? "desc";
|
|
75
|
-
results.sort((a, b) => {
|
|
76
|
-
const aValue = orderBy === "timestamp" ? a.timestamp.getTime() : a.type;
|
|
77
|
-
const bValue = orderBy === "timestamp" ? b.timestamp.getTime() : b.type;
|
|
78
|
-
if (aValue < bValue) return orderDirection === "asc" ? -1 : 1;
|
|
79
|
-
if (aValue > bValue) return orderDirection === "asc" ? 1 : -1;
|
|
80
|
-
return 0;
|
|
81
|
-
});
|
|
82
|
-
if (options.offset !== void 0) results = results.slice(options.offset);
|
|
83
|
-
if (options.limit !== void 0) results = results.slice(0, options.limit);
|
|
84
|
-
return results;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Count audit records matching filters.
|
|
88
|
-
*/
|
|
89
|
-
async count(options) {
|
|
90
|
-
const allRecords = await this.getAllRecords();
|
|
91
|
-
return this.applyFilters(allRecords, options).length;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Get all stored records (for testing/debugging).
|
|
95
|
-
*/
|
|
96
|
-
async getRecords() {
|
|
97
|
-
return this.getAllRecords();
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Clear all stored records.
|
|
101
|
-
*/
|
|
102
|
-
async clear() {
|
|
103
|
-
const ids = await this.cache.get(this.indexKey) ?? [];
|
|
104
|
-
for (const id of ids) await this.cache.delete(this.getRecordKey(id));
|
|
105
|
-
await this.cache.delete(this.indexKey);
|
|
106
|
-
}
|
|
107
|
-
getRecordKey(id) {
|
|
108
|
-
return `${this.prefix}:${id}`;
|
|
109
|
-
}
|
|
110
|
-
async getAllRecords() {
|
|
111
|
-
const ids = await this.cache.get(this.indexKey) ?? [];
|
|
112
|
-
const records = [];
|
|
113
|
-
const validIds = [];
|
|
114
|
-
for (const id of ids) {
|
|
115
|
-
const key = this.getRecordKey(id);
|
|
116
|
-
const serialized = await this.cache.get(key);
|
|
117
|
-
if (serialized) {
|
|
118
|
-
records.push(this.deserializeRecord(serialized));
|
|
119
|
-
validIds.push(id);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
if (validIds.length !== ids.length) await this.cache.set(this.indexKey, validIds, this.ttl);
|
|
123
|
-
return records;
|
|
124
|
-
}
|
|
125
|
-
serializeRecord(record) {
|
|
126
|
-
return {
|
|
127
|
-
...record,
|
|
128
|
-
timestamp: record.timestamp.toISOString()
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
deserializeRecord(serialized) {
|
|
132
|
-
return {
|
|
133
|
-
...serialized,
|
|
134
|
-
timestamp: new Date(serialized.timestamp)
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
applyFilters(records, options) {
|
|
138
|
-
return records.filter((record) => {
|
|
139
|
-
if (options.type !== void 0) {
|
|
140
|
-
if (Array.isArray(options.type)) {
|
|
141
|
-
if (!options.type.includes(record.type)) return false;
|
|
142
|
-
} else if (record.type !== options.type) return false;
|
|
143
|
-
}
|
|
144
|
-
if (options.entityId !== void 0) {
|
|
145
|
-
const entityId = typeof options.entityId === "string" ? options.entityId : JSON.stringify(options.entityId);
|
|
146
|
-
if ((typeof record.entityId === "string" ? record.entityId : JSON.stringify(record.entityId)) !== entityId) return false;
|
|
147
|
-
}
|
|
148
|
-
if (options.table !== void 0) {
|
|
149
|
-
if (record.table !== options.table) return false;
|
|
150
|
-
}
|
|
151
|
-
if (options.actorId !== void 0) {
|
|
152
|
-
if (record.actor?.id !== options.actorId) return false;
|
|
153
|
-
}
|
|
154
|
-
if (options.from !== void 0) {
|
|
155
|
-
if (record.timestamp < options.from) return false;
|
|
156
|
-
}
|
|
157
|
-
if (options.to !== void 0) {
|
|
158
|
-
if (record.timestamp > options.to) return false;
|
|
159
|
-
}
|
|
160
|
-
return true;
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
//#endregion
|
|
165
|
-
exports.CacheAuditStorage = CacheAuditStorage;
|
|
1
|
+
const require_cache = require('./cache-l7jAptBl.cjs');
|
|
166
2
|
|
|
167
|
-
|
|
3
|
+
exports.CacheAuditStorage = require_cache.CacheAuditStorage;
|
package/dist/cache.d.cts
CHANGED
|
@@ -1,92 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Configuration for CacheAuditStorage.
|
|
6
|
-
*/
|
|
7
|
-
export interface CacheAuditStorageConfig {
|
|
8
|
-
/** Cache instance to use for storage */
|
|
9
|
-
cache: Cache;
|
|
10
|
-
/**
|
|
11
|
-
* Key prefix for audit records.
|
|
12
|
-
* Records are stored as `${prefix}:${id}`.
|
|
13
|
-
* @default 'audit'
|
|
14
|
-
*/
|
|
15
|
-
prefix?: string;
|
|
16
|
-
/**
|
|
17
|
-
* TTL (time-to-live) in seconds for audit records.
|
|
18
|
-
* If not set, records use the cache's default TTL.
|
|
19
|
-
*/
|
|
20
|
-
ttl?: number;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Cache-based audit storage implementation.
|
|
24
|
-
* Uses any @geekmidas/cache implementation for storage.
|
|
25
|
-
*
|
|
26
|
-
* Best suited for:
|
|
27
|
-
* - Development and testing
|
|
28
|
-
* - Temporary audit logs that don't need persistence
|
|
29
|
-
* - Applications with existing cache infrastructure
|
|
30
|
-
* - Distributed systems needing shared audit state (with Redis/Upstash)
|
|
31
|
-
*
|
|
32
|
-
* Note: Query performance may degrade with large numbers of records
|
|
33
|
-
* since filtering happens in memory after fetching all records.
|
|
34
|
-
*
|
|
35
|
-
* @template TAuditAction - Optional type parameter for type-safe audit actions.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* import { CacheAuditStorage } from '@geekmidas/audit/cache';
|
|
40
|
-
* import { InMemoryCache } from '@geekmidas/cache/memory';
|
|
41
|
-
* import { UpstashCache } from '@geekmidas/cache/upstash';
|
|
42
|
-
*
|
|
43
|
-
* // With in-memory cache (development/testing)
|
|
44
|
-
* const storage = new CacheAuditStorage({
|
|
45
|
-
* cache: new InMemoryCache(),
|
|
46
|
-
* ttl: 86400, // 24 hours
|
|
47
|
-
* });
|
|
48
|
-
*
|
|
49
|
-
* // With Upstash Redis (production)
|
|
50
|
-
* const storage = new CacheAuditStorage({
|
|
51
|
-
* cache: new UpstashCache({
|
|
52
|
-
* url: process.env.UPSTASH_REDIS_URL,
|
|
53
|
-
* token: process.env.UPSTASH_REDIS_TOKEN,
|
|
54
|
-
* }),
|
|
55
|
-
* ttl: 604800, // 7 days
|
|
56
|
-
* });
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export declare class CacheAuditStorage<TAuditAction extends AuditableAction<string, unknown> = AuditableAction<string, unknown>> implements AuditStorage<TAuditAction> {
|
|
60
|
-
private readonly cache;
|
|
61
|
-
private readonly prefix;
|
|
62
|
-
private readonly ttl?;
|
|
63
|
-
private readonly indexKey;
|
|
64
|
-
constructor(config: CacheAuditStorageConfig);
|
|
65
|
-
/**
|
|
66
|
-
* Write audit records to cache.
|
|
67
|
-
*/
|
|
68
|
-
write(records: AuditRecord[]): Promise<void>;
|
|
69
|
-
/**
|
|
70
|
-
* Query audit records from cache.
|
|
71
|
-
*/
|
|
72
|
-
query(options: AuditQueryOptions): Promise<AuditRecord[]>;
|
|
73
|
-
/**
|
|
74
|
-
* Count audit records matching filters.
|
|
75
|
-
*/
|
|
76
|
-
count(options: Omit<AuditQueryOptions, 'limit' | 'offset'>): Promise<number>;
|
|
77
|
-
/**
|
|
78
|
-
* Get all stored records (for testing/debugging).
|
|
79
|
-
*/
|
|
80
|
-
getRecords(): Promise<AuditRecord[]>;
|
|
81
|
-
/**
|
|
82
|
-
* Clear all stored records.
|
|
83
|
-
*/
|
|
84
|
-
clear(): Promise<void>;
|
|
85
|
-
private getRecordKey;
|
|
86
|
-
private getAllRecords;
|
|
87
|
-
private serializeRecord;
|
|
88
|
-
private deserializeRecord;
|
|
89
|
-
private applyFilters;
|
|
90
|
-
}
|
|
91
|
-
//#endregion
|
|
92
|
-
//# sourceMappingURL=cache.d.cts.map
|
|
1
|
+
import "./Auditor-DpBHXIJM.cjs";
|
|
2
|
+
import { CacheAuditStorage, CacheAuditStorageConfig } from "./cache-Bih6HxY6.cjs";
|
|
3
|
+
export { CacheAuditStorage, CacheAuditStorageConfig };
|
package/dist/cache.d.mts
CHANGED
|
@@ -1,92 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Configuration for CacheAuditStorage.
|
|
6
|
-
*/
|
|
7
|
-
export interface CacheAuditStorageConfig {
|
|
8
|
-
/** Cache instance to use for storage */
|
|
9
|
-
cache: Cache;
|
|
10
|
-
/**
|
|
11
|
-
* Key prefix for audit records.
|
|
12
|
-
* Records are stored as `${prefix}:${id}`.
|
|
13
|
-
* @default 'audit'
|
|
14
|
-
*/
|
|
15
|
-
prefix?: string;
|
|
16
|
-
/**
|
|
17
|
-
* TTL (time-to-live) in seconds for audit records.
|
|
18
|
-
* If not set, records use the cache's default TTL.
|
|
19
|
-
*/
|
|
20
|
-
ttl?: number;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Cache-based audit storage implementation.
|
|
24
|
-
* Uses any @geekmidas/cache implementation for storage.
|
|
25
|
-
*
|
|
26
|
-
* Best suited for:
|
|
27
|
-
* - Development and testing
|
|
28
|
-
* - Temporary audit logs that don't need persistence
|
|
29
|
-
* - Applications with existing cache infrastructure
|
|
30
|
-
* - Distributed systems needing shared audit state (with Redis/Upstash)
|
|
31
|
-
*
|
|
32
|
-
* Note: Query performance may degrade with large numbers of records
|
|
33
|
-
* since filtering happens in memory after fetching all records.
|
|
34
|
-
*
|
|
35
|
-
* @template TAuditAction - Optional type parameter for type-safe audit actions.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* import { CacheAuditStorage } from '@geekmidas/audit/cache';
|
|
40
|
-
* import { InMemoryCache } from '@geekmidas/cache/memory';
|
|
41
|
-
* import { UpstashCache } from '@geekmidas/cache/upstash';
|
|
42
|
-
*
|
|
43
|
-
* // With in-memory cache (development/testing)
|
|
44
|
-
* const storage = new CacheAuditStorage({
|
|
45
|
-
* cache: new InMemoryCache(),
|
|
46
|
-
* ttl: 86400, // 24 hours
|
|
47
|
-
* });
|
|
48
|
-
*
|
|
49
|
-
* // With Upstash Redis (production)
|
|
50
|
-
* const storage = new CacheAuditStorage({
|
|
51
|
-
* cache: new UpstashCache({
|
|
52
|
-
* url: process.env.UPSTASH_REDIS_URL,
|
|
53
|
-
* token: process.env.UPSTASH_REDIS_TOKEN,
|
|
54
|
-
* }),
|
|
55
|
-
* ttl: 604800, // 7 days
|
|
56
|
-
* });
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export declare class CacheAuditStorage<TAuditAction extends AuditableAction<string, unknown> = AuditableAction<string, unknown>> implements AuditStorage<TAuditAction> {
|
|
60
|
-
private readonly cache;
|
|
61
|
-
private readonly prefix;
|
|
62
|
-
private readonly ttl?;
|
|
63
|
-
private readonly indexKey;
|
|
64
|
-
constructor(config: CacheAuditStorageConfig);
|
|
65
|
-
/**
|
|
66
|
-
* Write audit records to cache.
|
|
67
|
-
*/
|
|
68
|
-
write(records: AuditRecord[]): Promise<void>;
|
|
69
|
-
/**
|
|
70
|
-
* Query audit records from cache.
|
|
71
|
-
*/
|
|
72
|
-
query(options: AuditQueryOptions): Promise<AuditRecord[]>;
|
|
73
|
-
/**
|
|
74
|
-
* Count audit records matching filters.
|
|
75
|
-
*/
|
|
76
|
-
count(options: Omit<AuditQueryOptions, 'limit' | 'offset'>): Promise<number>;
|
|
77
|
-
/**
|
|
78
|
-
* Get all stored records (for testing/debugging).
|
|
79
|
-
*/
|
|
80
|
-
getRecords(): Promise<AuditRecord[]>;
|
|
81
|
-
/**
|
|
82
|
-
* Clear all stored records.
|
|
83
|
-
*/
|
|
84
|
-
clear(): Promise<void>;
|
|
85
|
-
private getRecordKey;
|
|
86
|
-
private getAllRecords;
|
|
87
|
-
private serializeRecord;
|
|
88
|
-
private deserializeRecord;
|
|
89
|
-
private applyFilters;
|
|
90
|
-
}
|
|
91
|
-
//#endregion
|
|
92
|
-
//# sourceMappingURL=cache.d.mts.map
|
|
1
|
+
import "./Auditor-CW2NrfHS.mjs";
|
|
2
|
+
import { CacheAuditStorage, CacheAuditStorageConfig } from "./cache-wET57t6d.mjs";
|
|
3
|
+
export { CacheAuditStorage, CacheAuditStorageConfig };
|
package/dist/cache.mjs
CHANGED
|
@@ -1,166 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Cache-based audit storage implementation.
|
|
4
|
-
* Uses any @geekmidas/cache implementation for storage.
|
|
5
|
-
*
|
|
6
|
-
* Best suited for:
|
|
7
|
-
* - Development and testing
|
|
8
|
-
* - Temporary audit logs that don't need persistence
|
|
9
|
-
* - Applications with existing cache infrastructure
|
|
10
|
-
* - Distributed systems needing shared audit state (with Redis/Upstash)
|
|
11
|
-
*
|
|
12
|
-
* Note: Query performance may degrade with large numbers of records
|
|
13
|
-
* since filtering happens in memory after fetching all records.
|
|
14
|
-
*
|
|
15
|
-
* @template TAuditAction - Optional type parameter for type-safe audit actions.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import { CacheAuditStorage } from '@geekmidas/audit/cache';
|
|
20
|
-
* import { InMemoryCache } from '@geekmidas/cache/memory';
|
|
21
|
-
* import { UpstashCache } from '@geekmidas/cache/upstash';
|
|
22
|
-
*
|
|
23
|
-
* // With in-memory cache (development/testing)
|
|
24
|
-
* const storage = new CacheAuditStorage({
|
|
25
|
-
* cache: new InMemoryCache(),
|
|
26
|
-
* ttl: 86400, // 24 hours
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* // With Upstash Redis (production)
|
|
30
|
-
* const storage = new CacheAuditStorage({
|
|
31
|
-
* cache: new UpstashCache({
|
|
32
|
-
* url: process.env.UPSTASH_REDIS_URL,
|
|
33
|
-
* token: process.env.UPSTASH_REDIS_TOKEN,
|
|
34
|
-
* }),
|
|
35
|
-
* ttl: 604800, // 7 days
|
|
36
|
-
* });
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
var CacheAuditStorage = class {
|
|
40
|
-
cache;
|
|
41
|
-
prefix;
|
|
42
|
-
ttl;
|
|
43
|
-
indexKey;
|
|
44
|
-
constructor(config) {
|
|
45
|
-
this.cache = config.cache;
|
|
46
|
-
this.prefix = config.prefix ?? "audit";
|
|
47
|
-
this.ttl = config.ttl;
|
|
48
|
-
this.indexKey = `${this.prefix}:__index__`;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Write audit records to cache.
|
|
52
|
-
*/
|
|
53
|
-
async write(records) {
|
|
54
|
-
if (records.length === 0) return;
|
|
55
|
-
const existingIds = await this.cache.get(this.indexKey) ?? [];
|
|
56
|
-
const newIds = [];
|
|
57
|
-
for (const record of records) {
|
|
58
|
-
const key = this.getRecordKey(record.id);
|
|
59
|
-
const serialized = this.serializeRecord(record);
|
|
60
|
-
await this.cache.set(key, serialized, this.ttl);
|
|
61
|
-
newIds.push(record.id);
|
|
62
|
-
}
|
|
63
|
-
const updatedIds = [...existingIds, ...newIds];
|
|
64
|
-
await this.cache.set(this.indexKey, updatedIds, this.ttl);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Query audit records from cache.
|
|
68
|
-
*/
|
|
69
|
-
async query(options) {
|
|
70
|
-
const allRecords = await this.getAllRecords();
|
|
71
|
-
let results = this.applyFilters(allRecords, options);
|
|
72
|
-
const orderBy = options.orderBy ?? "timestamp";
|
|
73
|
-
const orderDirection = options.orderDirection ?? "desc";
|
|
74
|
-
results.sort((a, b) => {
|
|
75
|
-
const aValue = orderBy === "timestamp" ? a.timestamp.getTime() : a.type;
|
|
76
|
-
const bValue = orderBy === "timestamp" ? b.timestamp.getTime() : b.type;
|
|
77
|
-
if (aValue < bValue) return orderDirection === "asc" ? -1 : 1;
|
|
78
|
-
if (aValue > bValue) return orderDirection === "asc" ? 1 : -1;
|
|
79
|
-
return 0;
|
|
80
|
-
});
|
|
81
|
-
if (options.offset !== void 0) results = results.slice(options.offset);
|
|
82
|
-
if (options.limit !== void 0) results = results.slice(0, options.limit);
|
|
83
|
-
return results;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Count audit records matching filters.
|
|
87
|
-
*/
|
|
88
|
-
async count(options) {
|
|
89
|
-
const allRecords = await this.getAllRecords();
|
|
90
|
-
return this.applyFilters(allRecords, options).length;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Get all stored records (for testing/debugging).
|
|
94
|
-
*/
|
|
95
|
-
async getRecords() {
|
|
96
|
-
return this.getAllRecords();
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Clear all stored records.
|
|
100
|
-
*/
|
|
101
|
-
async clear() {
|
|
102
|
-
const ids = await this.cache.get(this.indexKey) ?? [];
|
|
103
|
-
for (const id of ids) await this.cache.delete(this.getRecordKey(id));
|
|
104
|
-
await this.cache.delete(this.indexKey);
|
|
105
|
-
}
|
|
106
|
-
getRecordKey(id) {
|
|
107
|
-
return `${this.prefix}:${id}`;
|
|
108
|
-
}
|
|
109
|
-
async getAllRecords() {
|
|
110
|
-
const ids = await this.cache.get(this.indexKey) ?? [];
|
|
111
|
-
const records = [];
|
|
112
|
-
const validIds = [];
|
|
113
|
-
for (const id of ids) {
|
|
114
|
-
const key = this.getRecordKey(id);
|
|
115
|
-
const serialized = await this.cache.get(key);
|
|
116
|
-
if (serialized) {
|
|
117
|
-
records.push(this.deserializeRecord(serialized));
|
|
118
|
-
validIds.push(id);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (validIds.length !== ids.length) await this.cache.set(this.indexKey, validIds, this.ttl);
|
|
122
|
-
return records;
|
|
123
|
-
}
|
|
124
|
-
serializeRecord(record) {
|
|
125
|
-
return {
|
|
126
|
-
...record,
|
|
127
|
-
timestamp: record.timestamp.toISOString()
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
deserializeRecord(serialized) {
|
|
131
|
-
return {
|
|
132
|
-
...serialized,
|
|
133
|
-
timestamp: new Date(serialized.timestamp)
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
applyFilters(records, options) {
|
|
137
|
-
return records.filter((record) => {
|
|
138
|
-
if (options.type !== void 0) {
|
|
139
|
-
if (Array.isArray(options.type)) {
|
|
140
|
-
if (!options.type.includes(record.type)) return false;
|
|
141
|
-
} else if (record.type !== options.type) return false;
|
|
142
|
-
}
|
|
143
|
-
if (options.entityId !== void 0) {
|
|
144
|
-
const entityId = typeof options.entityId === "string" ? options.entityId : JSON.stringify(options.entityId);
|
|
145
|
-
if ((typeof record.entityId === "string" ? record.entityId : JSON.stringify(record.entityId)) !== entityId) return false;
|
|
146
|
-
}
|
|
147
|
-
if (options.table !== void 0) {
|
|
148
|
-
if (record.table !== options.table) return false;
|
|
149
|
-
}
|
|
150
|
-
if (options.actorId !== void 0) {
|
|
151
|
-
if (record.actor?.id !== options.actorId) return false;
|
|
152
|
-
}
|
|
153
|
-
if (options.from !== void 0) {
|
|
154
|
-
if (record.timestamp < options.from) return false;
|
|
155
|
-
}
|
|
156
|
-
if (options.to !== void 0) {
|
|
157
|
-
if (record.timestamp > options.to) return false;
|
|
158
|
-
}
|
|
159
|
-
return true;
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
//#endregion
|
|
164
|
-
export { CacheAuditStorage };
|
|
1
|
+
import { CacheAuditStorage } from "./cache-BbIl31RL.mjs";
|
|
165
2
|
|
|
166
|
-
|
|
3
|
+
export { CacheAuditStorage };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(exports, '__toESM', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return __toESM;
|
|
29
|
+
}
|
|
30
|
+
});
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.DefaultAuditor = require_DefaultAuditor.DefaultAuditor;
|
|
1
|
+
const require_DefaultAuditor = require('./DefaultAuditor-JbZ_BQfh.cjs');
|
|
2
|
+
|
|
3
|
+
exports.DefaultAuditor = require_DefaultAuditor.DefaultAuditor;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DefaultAuditor, DefaultAuditorConfig } from "./DefaultAuditor.cjs";
|
|
3
|
-
export {
|
|
1
|
+
import { AuditActor, AuditMetadata, AuditOperation, AuditOptions, AuditQueryOptions, AuditRecord, AuditStorage, AuditableAction, Auditor, ExtractAuditPayload, ExtractAuditType, ExtractAuditorAction, ExtractStorageAuditAction, MappedAudit } from "./Auditor-DpBHXIJM.cjs";
|
|
2
|
+
import { DefaultAuditor, DefaultAuditorConfig } from "./DefaultAuditor-GRFI86N_.cjs";
|
|
3
|
+
export { AuditActor, AuditMetadata, AuditOperation, AuditOptions, AuditQueryOptions, AuditRecord, AuditStorage, AuditableAction, Auditor, DefaultAuditor, DefaultAuditorConfig, ExtractAuditPayload, ExtractAuditType, ExtractAuditorAction, ExtractStorageAuditAction, MappedAudit };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DefaultAuditor, DefaultAuditorConfig } from "./DefaultAuditor.mjs";
|
|
3
|
-
export {
|
|
1
|
+
import { AuditActor, AuditMetadata, AuditOperation, AuditOptions, AuditQueryOptions, AuditRecord, AuditStorage, AuditableAction, Auditor, ExtractAuditPayload, ExtractAuditType, ExtractAuditorAction, ExtractStorageAuditAction, MappedAudit } from "./Auditor-CW2NrfHS.mjs";
|
|
2
|
+
import { DefaultAuditor, DefaultAuditorConfig } from "./DefaultAuditor-DlJ1ENfx.mjs";
|
|
3
|
+
export { AuditActor, AuditMetadata, AuditOperation, AuditOptions, AuditQueryOptions, AuditRecord, AuditStorage, AuditableAction, Auditor, DefaultAuditor, DefaultAuditorConfig, ExtractAuditPayload, ExtractAuditType, ExtractAuditorAction, ExtractStorageAuditAction, MappedAudit };
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { DefaultAuditor } from "./DefaultAuditor.mjs";
|
|
2
|
-
|
|
1
|
+
import { DefaultAuditor } from "./DefaultAuditor-BAVnNmRh.mjs";
|
|
2
|
+
|
|
3
|
+
export { DefaultAuditor };
|
package/dist/knex.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const nanoid = require_chunk.__toESM(require("nanoid"));
|
|
3
|
+
|
|
3
4
|
//#region src/knex.ts
|
|
4
5
|
/**
|
|
5
6
|
* Type guard for an active Knex transaction.
|
|
@@ -102,7 +103,8 @@ var KnexAuditStorage = class {
|
|
|
102
103
|
query = query.orderBy(orderBy === "timestamp" ? "timestamp" : "type", orderDirection);
|
|
103
104
|
if (options.limit !== void 0) query = query.limit(options.limit);
|
|
104
105
|
if (options.offset !== void 0) query = query.offset(options.offset);
|
|
105
|
-
|
|
106
|
+
const rows = await query;
|
|
107
|
+
return rows.map((row) => this.fromRow(row));
|
|
106
108
|
}
|
|
107
109
|
async count(options) {
|
|
108
110
|
let query = this.db(this.tableName).count({ count: "id" });
|
|
@@ -141,10 +143,8 @@ var KnexAuditStorage = class {
|
|
|
141
143
|
});
|
|
142
144
|
}
|
|
143
145
|
applyFilters(query, options) {
|
|
144
|
-
if (options.type !== void 0)
|
|
145
|
-
|
|
146
|
-
else query = query.where("type", options.type);
|
|
147
|
-
}
|
|
146
|
+
if (options.type !== void 0) if (Array.isArray(options.type)) query = query.whereIn("type", options.type);
|
|
147
|
+
else query = query.where("type", options.type);
|
|
148
148
|
if (options.entityId !== void 0) {
|
|
149
149
|
const entityId = typeof options.entityId === "string" ? options.entityId : JSON.stringify(options.entityId);
|
|
150
150
|
query = query.where("entityId", entityId);
|
|
@@ -213,7 +213,7 @@ var KnexAuditStorage = class {
|
|
|
213
213
|
return {};
|
|
214
214
|
}
|
|
215
215
|
getActorData(actor) {
|
|
216
|
-
const { id, type
|
|
216
|
+
const { id, type,...rest } = actor;
|
|
217
217
|
return rest;
|
|
218
218
|
}
|
|
219
219
|
parseEntityId(entityId) {
|
|
@@ -226,8 +226,8 @@ var KnexAuditStorage = class {
|
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
};
|
|
229
|
+
|
|
229
230
|
//#endregion
|
|
230
231
|
exports.KnexAuditStorage = KnexAuditStorage;
|
|
231
232
|
exports.withAuditableTransaction = withAuditableTransaction;
|
|
232
|
-
|
|
233
233
|
//# sourceMappingURL=knex.cjs.map
|