@sparkleideas/shared 3.0.0-alpha.7

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 (96) hide show
  1. package/README.md +323 -0
  2. package/__tests__/hooks/bash-safety.test.ts +289 -0
  3. package/__tests__/hooks/file-organization.test.ts +335 -0
  4. package/__tests__/hooks/git-commit.test.ts +336 -0
  5. package/__tests__/hooks/index.ts +23 -0
  6. package/__tests__/hooks/session-hooks.test.ts +357 -0
  7. package/__tests__/hooks/task-hooks.test.ts +193 -0
  8. package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
  9. package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
  10. package/docs/EVENTS_README.md +352 -0
  11. package/package.json +39 -0
  12. package/src/core/config/defaults.ts +207 -0
  13. package/src/core/config/index.ts +15 -0
  14. package/src/core/config/loader.ts +271 -0
  15. package/src/core/config/schema.ts +188 -0
  16. package/src/core/config/validator.ts +209 -0
  17. package/src/core/event-bus.ts +236 -0
  18. package/src/core/index.ts +22 -0
  19. package/src/core/interfaces/agent.interface.ts +251 -0
  20. package/src/core/interfaces/coordinator.interface.ts +363 -0
  21. package/src/core/interfaces/event.interface.ts +267 -0
  22. package/src/core/interfaces/index.ts +19 -0
  23. package/src/core/interfaces/memory.interface.ts +332 -0
  24. package/src/core/interfaces/task.interface.ts +223 -0
  25. package/src/core/orchestrator/event-coordinator.ts +122 -0
  26. package/src/core/orchestrator/health-monitor.ts +214 -0
  27. package/src/core/orchestrator/index.ts +89 -0
  28. package/src/core/orchestrator/lifecycle-manager.ts +263 -0
  29. package/src/core/orchestrator/session-manager.ts +279 -0
  30. package/src/core/orchestrator/task-manager.ts +317 -0
  31. package/src/events/domain-events.ts +584 -0
  32. package/src/events/event-store.test.ts +387 -0
  33. package/src/events/event-store.ts +588 -0
  34. package/src/events/example-usage.ts +293 -0
  35. package/src/events/index.ts +90 -0
  36. package/src/events/projections.ts +561 -0
  37. package/src/events/state-reconstructor.ts +349 -0
  38. package/src/events.ts +367 -0
  39. package/src/hooks/INTEGRATION.md +658 -0
  40. package/src/hooks/README.md +532 -0
  41. package/src/hooks/example-usage.ts +499 -0
  42. package/src/hooks/executor.ts +379 -0
  43. package/src/hooks/hooks.test.ts +421 -0
  44. package/src/hooks/index.ts +131 -0
  45. package/src/hooks/registry.ts +333 -0
  46. package/src/hooks/safety/bash-safety.ts +604 -0
  47. package/src/hooks/safety/file-organization.ts +473 -0
  48. package/src/hooks/safety/git-commit.ts +623 -0
  49. package/src/hooks/safety/index.ts +46 -0
  50. package/src/hooks/session-hooks.ts +559 -0
  51. package/src/hooks/task-hooks.ts +513 -0
  52. package/src/hooks/types.ts +357 -0
  53. package/src/hooks/verify-exports.test.ts +125 -0
  54. package/src/index.ts +195 -0
  55. package/src/mcp/connection-pool.ts +438 -0
  56. package/src/mcp/index.ts +183 -0
  57. package/src/mcp/server.ts +774 -0
  58. package/src/mcp/session-manager.ts +428 -0
  59. package/src/mcp/tool-registry.ts +566 -0
  60. package/src/mcp/transport/http.ts +557 -0
  61. package/src/mcp/transport/index.ts +294 -0
  62. package/src/mcp/transport/stdio.ts +324 -0
  63. package/src/mcp/transport/websocket.ts +484 -0
  64. package/src/mcp/types.ts +565 -0
  65. package/src/plugin-interface.ts +663 -0
  66. package/src/plugin-loader.ts +638 -0
  67. package/src/plugin-registry.ts +604 -0
  68. package/src/plugins/index.ts +34 -0
  69. package/src/plugins/official/hive-mind-plugin.ts +330 -0
  70. package/src/plugins/official/index.ts +24 -0
  71. package/src/plugins/official/maestro-plugin.ts +508 -0
  72. package/src/plugins/types.ts +108 -0
  73. package/src/resilience/bulkhead.ts +277 -0
  74. package/src/resilience/circuit-breaker.ts +326 -0
  75. package/src/resilience/index.ts +26 -0
  76. package/src/resilience/rate-limiter.ts +420 -0
  77. package/src/resilience/retry.ts +224 -0
  78. package/src/security/index.ts +39 -0
  79. package/src/security/input-validation.ts +265 -0
  80. package/src/security/secure-random.ts +159 -0
  81. package/src/services/index.ts +16 -0
  82. package/src/services/v3-progress.service.ts +505 -0
  83. package/src/types/agent.types.ts +144 -0
  84. package/src/types/index.ts +22 -0
  85. package/src/types/mcp.types.ts +300 -0
  86. package/src/types/memory.types.ts +263 -0
  87. package/src/types/swarm.types.ts +255 -0
  88. package/src/types/task.types.ts +205 -0
  89. package/src/types.ts +367 -0
  90. package/src/utils/secure-logger.d.ts +69 -0
  91. package/src/utils/secure-logger.d.ts.map +1 -0
  92. package/src/utils/secure-logger.js +208 -0
  93. package/src/utils/secure-logger.js.map +1 -0
  94. package/src/utils/secure-logger.ts +257 -0
  95. package/tmp.json +0 -0
  96. package/tsconfig.json +9 -0
@@ -0,0 +1,588 @@
1
+ /**
2
+ * Event Store Persistence (ADR-007)
3
+ *
4
+ * Provides persistent storage for domain events using SQLite.
5
+ * Supports event replay, snapshots, and projections.
6
+ *
7
+ * Key Features:
8
+ * - Append-only event log
9
+ * - Event versioning per aggregate
10
+ * - Event filtering and queries
11
+ * - Snapshot support for performance
12
+ * - Event replay for projections
13
+ * - Cross-platform SQLite (sql.js fallback)
14
+ *
15
+ * @module v3/shared/events/event-store
16
+ */
17
+
18
+ import { EventEmitter } from 'node:events';
19
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
20
+ import initSqlJs, { Database as SqlJsDatabase } from 'sql.js';
21
+ import { DomainEvent, AllDomainEvents } from './domain-events.js';
22
+
23
+ // =============================================================================
24
+ // Event Store Configuration
25
+ // =============================================================================
26
+
27
+ export interface EventStoreConfig {
28
+ /** Path to SQLite database file (:memory: for in-memory) */
29
+ databasePath: string;
30
+
31
+ /** Enable verbose logging */
32
+ verbose: boolean;
33
+
34
+ /** Auto-persist interval in milliseconds (0 = manual only) */
35
+ autoPersistInterval: number;
36
+
37
+ /** Maximum events before snapshot recommendation */
38
+ snapshotThreshold: number;
39
+
40
+ /** Path to sql.js WASM file (optional) */
41
+ wasmPath?: string;
42
+ }
43
+
44
+ const DEFAULT_CONFIG: EventStoreConfig = {
45
+ databasePath: ':memory:',
46
+ verbose: false,
47
+ autoPersistInterval: 5000, // 5 seconds
48
+ snapshotThreshold: 100,
49
+ };
50
+
51
+ // =============================================================================
52
+ // Event Store Interfaces
53
+ // =============================================================================
54
+
55
+ export interface EventFilter {
56
+ /** Filter by aggregate IDs */
57
+ aggregateIds?: string[];
58
+
59
+ /** Filter by aggregate types */
60
+ aggregateTypes?: Array<'agent' | 'task' | 'memory' | 'swarm'>;
61
+
62
+ /** Filter by event types */
63
+ eventTypes?: string[];
64
+
65
+ /** Filter events after timestamp */
66
+ afterTimestamp?: number;
67
+
68
+ /** Filter events before timestamp */
69
+ beforeTimestamp?: number;
70
+
71
+ /** Filter by minimum version */
72
+ fromVersion?: number;
73
+
74
+ /** Limit number of results */
75
+ limit?: number;
76
+
77
+ /** Offset for pagination */
78
+ offset?: number;
79
+ }
80
+
81
+ export interface EventSnapshot {
82
+ /** Aggregate ID */
83
+ aggregateId: string;
84
+
85
+ /** Aggregate type */
86
+ aggregateType: 'agent' | 'task' | 'memory' | 'swarm';
87
+
88
+ /** Version at snapshot */
89
+ version: number;
90
+
91
+ /** Snapshot state */
92
+ state: Record<string, unknown>;
93
+
94
+ /** Timestamp when snapshot was created */
95
+ timestamp: number;
96
+ }
97
+
98
+ export interface EventStoreStats {
99
+ totalEvents: number;
100
+ eventsByType: Record<string, number>;
101
+ eventsByAggregate: Record<string, number>;
102
+ oldestEvent: number | null;
103
+ newestEvent: number | null;
104
+ snapshotCount: number;
105
+ }
106
+
107
+ // =============================================================================
108
+ // Event Store Implementation
109
+ // =============================================================================
110
+
111
+ export class EventStore extends EventEmitter {
112
+ private config: EventStoreConfig;
113
+ private db: SqlJsDatabase | null = null;
114
+ private initialized: boolean = false;
115
+ private persistTimer: NodeJS.Timeout | null = null;
116
+ private SQL: any = null;
117
+
118
+ // Version tracking per aggregate
119
+ private aggregateVersions: Map<string, number> = new Map();
120
+
121
+ constructor(config: Partial<EventStoreConfig> = {}) {
122
+ super();
123
+ this.config = { ...DEFAULT_CONFIG, ...config };
124
+ }
125
+
126
+ /**
127
+ * Initialize the event store
128
+ */
129
+ async initialize(): Promise<void> {
130
+ if (this.initialized) return;
131
+
132
+ // Load sql.js WASM
133
+ this.SQL = await initSqlJs({
134
+ locateFile: this.config.wasmPath
135
+ ? () => this.config.wasmPath!
136
+ : (file) => `https://sql.js.org/dist/${file}`,
137
+ });
138
+
139
+ // Load existing database if exists
140
+ if (this.config.databasePath !== ':memory:' && existsSync(this.config.databasePath)) {
141
+ const buffer = readFileSync(this.config.databasePath);
142
+ this.db = new this.SQL.Database(new Uint8Array(buffer));
143
+
144
+ if (this.config.verbose) {
145
+ console.log(`[EventStore] Loaded database from ${this.config.databasePath}`);
146
+ }
147
+ } else {
148
+ this.db = new this.SQL.Database();
149
+
150
+ if (this.config.verbose) {
151
+ console.log('[EventStore] Created new event store database');
152
+ }
153
+ }
154
+
155
+ // Create schema
156
+ this.createSchema();
157
+
158
+ // Load aggregate versions
159
+ this.loadAggregateVersions();
160
+
161
+ // Set up auto-persist
162
+ if (this.config.autoPersistInterval > 0 && this.config.databasePath !== ':memory:') {
163
+ this.persistTimer = setInterval(() => {
164
+ this.persist().catch((err) => {
165
+ this.emit('error', { operation: 'auto-persist', error: err });
166
+ });
167
+ }, this.config.autoPersistInterval);
168
+ }
169
+
170
+ this.initialized = true;
171
+ this.emit('initialized');
172
+ }
173
+
174
+ /**
175
+ * Shutdown the event store
176
+ */
177
+ async shutdown(): Promise<void> {
178
+ if (!this.initialized || !this.db) return;
179
+
180
+ // Stop auto-persist
181
+ if (this.persistTimer) {
182
+ clearInterval(this.persistTimer);
183
+ this.persistTimer = null;
184
+ }
185
+
186
+ // Final persist
187
+ if (this.config.databasePath !== ':memory:') {
188
+ await this.persist();
189
+ }
190
+
191
+ this.db.close();
192
+ this.db = null;
193
+ this.initialized = false;
194
+ this.emit('shutdown');
195
+ }
196
+
197
+ /**
198
+ * Append a new event to the store
199
+ */
200
+ async append(event: DomainEvent): Promise<void> {
201
+ this.ensureInitialized();
202
+
203
+ // Get next version for aggregate
204
+ const currentVersion = this.aggregateVersions.get(event.aggregateId) || 0;
205
+ const nextVersion = currentVersion + 1;
206
+
207
+ // Set version on event
208
+ event.version = nextVersion;
209
+
210
+ // Insert event
211
+ const stmt = `
212
+ INSERT INTO events (
213
+ id, type, aggregate_id, aggregate_type, version, timestamp,
214
+ source, payload, metadata, causation_id, correlation_id
215
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
216
+ `;
217
+
218
+ this.db!.run(stmt, [
219
+ event.id,
220
+ event.type,
221
+ event.aggregateId,
222
+ event.aggregateType,
223
+ event.version,
224
+ event.timestamp,
225
+ event.source,
226
+ JSON.stringify(event.payload),
227
+ JSON.stringify(event.metadata || {}),
228
+ event.causationId || null,
229
+ event.correlationId || null,
230
+ ]);
231
+
232
+ // Update version tracker
233
+ this.aggregateVersions.set(event.aggregateId, nextVersion);
234
+
235
+ // Emit event appended notification
236
+ this.emit('event:appended', event);
237
+
238
+ // Check if snapshot needed
239
+ if (nextVersion % this.config.snapshotThreshold === 0) {
240
+ this.emit('snapshot:recommended', { aggregateId: event.aggregateId, version: nextVersion });
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Get events for a specific aggregate
246
+ */
247
+ async getEvents(aggregateId: string, fromVersion?: number): Promise<DomainEvent[]> {
248
+ this.ensureInitialized();
249
+
250
+ let sql = 'SELECT * FROM events WHERE aggregate_id = ?';
251
+ const params: any[] = [aggregateId];
252
+
253
+ if (fromVersion !== undefined) {
254
+ sql += ' AND version >= ?';
255
+ params.push(fromVersion);
256
+ }
257
+
258
+ sql += ' ORDER BY version ASC';
259
+
260
+ const stmt = this.db!.prepare(sql);
261
+ const events: DomainEvent[] = [];
262
+
263
+ stmt.bind(params);
264
+ while (stmt.step()) {
265
+ const row = stmt.getAsObject();
266
+ events.push(this.rowToEvent(row));
267
+ }
268
+
269
+ stmt.free();
270
+
271
+ return events;
272
+ }
273
+
274
+ /**
275
+ * Get events by type
276
+ */
277
+ async getEventsByType(type: string): Promise<DomainEvent[]> {
278
+ this.ensureInitialized();
279
+
280
+ const stmt = this.db!.prepare('SELECT * FROM events WHERE type = ? ORDER BY timestamp ASC');
281
+ const events: DomainEvent[] = [];
282
+
283
+ stmt.bind([type]);
284
+ while (stmt.step()) {
285
+ const row = stmt.getAsObject();
286
+ events.push(this.rowToEvent(row));
287
+ }
288
+
289
+ stmt.free();
290
+
291
+ return events;
292
+ }
293
+
294
+ /**
295
+ * Query events with filters
296
+ */
297
+ async query(filter: EventFilter): Promise<DomainEvent[]> {
298
+ this.ensureInitialized();
299
+
300
+ let sql = 'SELECT * FROM events WHERE 1=1';
301
+ const params: any[] = [];
302
+
303
+ // Aggregate ID filter
304
+ if (filter.aggregateIds && filter.aggregateIds.length > 0) {
305
+ sql += ` AND aggregate_id IN (${filter.aggregateIds.map(() => '?').join(',')})`;
306
+ params.push(...filter.aggregateIds);
307
+ }
308
+
309
+ // Aggregate type filter
310
+ if (filter.aggregateTypes && filter.aggregateTypes.length > 0) {
311
+ sql += ` AND aggregate_type IN (${filter.aggregateTypes.map(() => '?').join(',')})`;
312
+ params.push(...filter.aggregateTypes);
313
+ }
314
+
315
+ // Event type filter
316
+ if (filter.eventTypes && filter.eventTypes.length > 0) {
317
+ sql += ` AND type IN (${filter.eventTypes.map(() => '?').join(',')})`;
318
+ params.push(...filter.eventTypes);
319
+ }
320
+
321
+ // Timestamp filters
322
+ if (filter.afterTimestamp) {
323
+ sql += ' AND timestamp > ?';
324
+ params.push(filter.afterTimestamp);
325
+ }
326
+
327
+ if (filter.beforeTimestamp) {
328
+ sql += ' AND timestamp < ?';
329
+ params.push(filter.beforeTimestamp);
330
+ }
331
+
332
+ // Version filter
333
+ if (filter.fromVersion) {
334
+ sql += ' AND version >= ?';
335
+ params.push(filter.fromVersion);
336
+ }
337
+
338
+ // Order by timestamp
339
+ sql += ' ORDER BY timestamp ASC';
340
+
341
+ // Pagination
342
+ if (filter.limit) {
343
+ sql += ' LIMIT ?';
344
+ params.push(filter.limit);
345
+ }
346
+
347
+ if (filter.offset) {
348
+ sql += ' OFFSET ?';
349
+ params.push(filter.offset);
350
+ }
351
+
352
+ const stmt = this.db!.prepare(sql);
353
+ const events: DomainEvent[] = [];
354
+
355
+ stmt.bind(params);
356
+ while (stmt.step()) {
357
+ const row = stmt.getAsObject();
358
+ events.push(this.rowToEvent(row));
359
+ }
360
+
361
+ stmt.free();
362
+
363
+ return events;
364
+ }
365
+
366
+ /**
367
+ * Replay events from a specific version
368
+ */
369
+ async *replay(fromVersion: number = 0): AsyncIterable<DomainEvent> {
370
+ this.ensureInitialized();
371
+
372
+ const stmt = this.db!.prepare('SELECT * FROM events WHERE version >= ? ORDER BY version ASC');
373
+ stmt.bind([fromVersion]);
374
+
375
+ while (stmt.step()) {
376
+ const row = stmt.getAsObject();
377
+ yield this.rowToEvent(row);
378
+ }
379
+
380
+ stmt.free();
381
+ }
382
+
383
+ /**
384
+ * Save a snapshot for an aggregate
385
+ */
386
+ async saveSnapshot(snapshot: EventSnapshot): Promise<void> {
387
+ this.ensureInitialized();
388
+
389
+ const stmt = `
390
+ INSERT OR REPLACE INTO snapshots (
391
+ aggregate_id, aggregate_type, version, state, timestamp
392
+ ) VALUES (?, ?, ?, ?, ?)
393
+ `;
394
+
395
+ this.db!.run(stmt, [
396
+ snapshot.aggregateId,
397
+ snapshot.aggregateType,
398
+ snapshot.version,
399
+ JSON.stringify(snapshot.state),
400
+ snapshot.timestamp,
401
+ ]);
402
+
403
+ this.emit('snapshot:saved', snapshot);
404
+ }
405
+
406
+ /**
407
+ * Get snapshot for an aggregate
408
+ */
409
+ async getSnapshot(aggregateId: string): Promise<EventSnapshot | null> {
410
+ this.ensureInitialized();
411
+
412
+ const stmt = this.db!.prepare(
413
+ 'SELECT * FROM snapshots WHERE aggregate_id = ? ORDER BY version DESC LIMIT 1'
414
+ );
415
+
416
+ const row = stmt.getAsObject([aggregateId]);
417
+ stmt.free();
418
+
419
+ if (!row || Object.keys(row).length === 0) {
420
+ return null;
421
+ }
422
+
423
+ return {
424
+ aggregateId: row.aggregate_id as string,
425
+ aggregateType: row.aggregate_type as any,
426
+ version: row.version as number,
427
+ state: JSON.parse(row.state as string),
428
+ timestamp: row.timestamp as number,
429
+ };
430
+ }
431
+
432
+ /**
433
+ * Get event store statistics
434
+ */
435
+ async getStats(): Promise<EventStoreStats> {
436
+ this.ensureInitialized();
437
+
438
+ // Total events
439
+ const totalStmt = this.db!.prepare('SELECT COUNT(*) as count FROM events');
440
+ const totalRow = totalStmt.getAsObject();
441
+ totalStmt.free();
442
+ const totalEvents = (totalRow.count as number) || 0;
443
+
444
+ // Events by type
445
+ const typeStmt = this.db!.prepare('SELECT type, COUNT(*) as count FROM events GROUP BY type');
446
+ const eventsByType: Record<string, number> = {};
447
+ while (typeStmt.step()) {
448
+ const row = typeStmt.getAsObject();
449
+ eventsByType[row.type as string] = (row.count as number) || 0;
450
+ }
451
+ typeStmt.free();
452
+
453
+ // Events by aggregate
454
+ const aggStmt = this.db!.prepare(
455
+ 'SELECT aggregate_id, COUNT(*) as count FROM events GROUP BY aggregate_id'
456
+ );
457
+ const eventsByAggregate: Record<string, number> = {};
458
+ while (aggStmt.step()) {
459
+ const row = aggStmt.getAsObject();
460
+ eventsByAggregate[row.aggregate_id as string] = (row.count as number) || 0;
461
+ }
462
+ aggStmt.free();
463
+
464
+ // Timestamp range
465
+ const rangeStmt = this.db!.prepare('SELECT MIN(timestamp) as oldest, MAX(timestamp) as newest FROM events');
466
+ const rangeRow = rangeStmt.getAsObject();
467
+ rangeStmt.free();
468
+
469
+ // Snapshot count
470
+ const snapshotStmt = this.db!.prepare('SELECT COUNT(*) as count FROM snapshots');
471
+ const snapshotRow = snapshotStmt.getAsObject();
472
+ snapshotStmt.free();
473
+
474
+ return {
475
+ totalEvents,
476
+ eventsByType,
477
+ eventsByAggregate,
478
+ oldestEvent: (rangeRow.oldest as number) || null,
479
+ newestEvent: (rangeRow.newest as number) || null,
480
+ snapshotCount: (snapshotRow.count as number) || 0,
481
+ };
482
+ }
483
+
484
+ /**
485
+ * Persist to disk
486
+ */
487
+ async persist(): Promise<void> {
488
+ if (!this.db || this.config.databasePath === ':memory:') {
489
+ return;
490
+ }
491
+
492
+ const data = this.db.export();
493
+ const buffer = Buffer.from(data);
494
+
495
+ writeFileSync(this.config.databasePath, buffer);
496
+
497
+ if (this.config.verbose) {
498
+ console.log(`[EventStore] Persisted ${buffer.length} bytes to ${this.config.databasePath}`);
499
+ }
500
+
501
+ this.emit('persisted', { size: buffer.length, path: this.config.databasePath });
502
+ }
503
+
504
+ // ===== Private Methods =====
505
+
506
+ private createSchema(): void {
507
+ if (!this.db) return;
508
+
509
+ // Events table
510
+ this.db.run(`
511
+ CREATE TABLE IF NOT EXISTS events (
512
+ id TEXT PRIMARY KEY,
513
+ type TEXT NOT NULL,
514
+ aggregate_id TEXT NOT NULL,
515
+ aggregate_type TEXT NOT NULL,
516
+ version INTEGER NOT NULL,
517
+ timestamp INTEGER NOT NULL,
518
+ source TEXT NOT NULL,
519
+ payload TEXT NOT NULL,
520
+ metadata TEXT,
521
+ causation_id TEXT,
522
+ correlation_id TEXT
523
+ )
524
+ `);
525
+
526
+ // Indexes for performance
527
+ this.db.run('CREATE INDEX IF NOT EXISTS idx_aggregate_id ON events(aggregate_id)');
528
+ this.db.run('CREATE INDEX IF NOT EXISTS idx_aggregate_type ON events(aggregate_type)');
529
+ this.db.run('CREATE INDEX IF NOT EXISTS idx_event_type ON events(type)');
530
+ this.db.run('CREATE INDEX IF NOT EXISTS idx_timestamp ON events(timestamp)');
531
+ this.db.run('CREATE INDEX IF NOT EXISTS idx_version ON events(version)');
532
+ this.db.run(
533
+ 'CREATE UNIQUE INDEX IF NOT EXISTS idx_aggregate_version ON events(aggregate_id, version)'
534
+ );
535
+
536
+ // Snapshots table
537
+ this.db.run(`
538
+ CREATE TABLE IF NOT EXISTS snapshots (
539
+ aggregate_id TEXT PRIMARY KEY,
540
+ aggregate_type TEXT NOT NULL,
541
+ version INTEGER NOT NULL,
542
+ state TEXT NOT NULL,
543
+ timestamp INTEGER NOT NULL
544
+ )
545
+ `);
546
+
547
+ if (this.config.verbose) {
548
+ console.log('[EventStore] Schema created successfully');
549
+ }
550
+ }
551
+
552
+ private loadAggregateVersions(): void {
553
+ if (!this.db) return;
554
+
555
+ const stmt = this.db.prepare(
556
+ 'SELECT aggregate_id, MAX(version) as max_version FROM events GROUP BY aggregate_id'
557
+ );
558
+
559
+ while (stmt.step()) {
560
+ const row = stmt.getAsObject();
561
+ this.aggregateVersions.set(row.aggregate_id as string, (row.max_version as number) || 0);
562
+ }
563
+
564
+ stmt.free();
565
+ }
566
+
567
+ private rowToEvent(row: any): DomainEvent {
568
+ return {
569
+ id: row.id as string,
570
+ type: row.type as string,
571
+ aggregateId: row.aggregate_id as string,
572
+ aggregateType: row.aggregate_type as any,
573
+ version: row.version as number,
574
+ timestamp: row.timestamp as number,
575
+ source: row.source as any,
576
+ payload: JSON.parse(row.payload as string),
577
+ metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined,
578
+ causationId: row.causation_id as string | undefined,
579
+ correlationId: row.correlation_id as string | undefined,
580
+ };
581
+ }
582
+
583
+ private ensureInitialized(): void {
584
+ if (!this.initialized || !this.db) {
585
+ throw new Error('EventStore not initialized. Call initialize() first.');
586
+ }
587
+ }
588
+ }