@ocoda/event-sourcing-mariadb 1.0.2 → 1.1.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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # @ocoda/event-sourcing-mariadb
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#371](https://github.com/ocoda/event-sourcing/pull/371) [`eff4abd`](https://github.com/ocoda/event-sourcing/commit/eff4abda2b44a7fbcb1be7bccde7fc9267e7fded) Thanks [@drieshooghe](https://github.com/drieshooghe)! - Support for a getAllEnvelopes method on the EventStore
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`eff4abd`](https://github.com/ocoda/event-sourcing/commit/eff4abda2b44a7fbcb1be7bccde7fc9267e7fded)]:
12
+ - @ocoda/event-sourcing@1.1.0
13
+
3
14
  ## 1.0.2
4
15
 
5
16
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IEventPayload, IEvent, EventStore, IEventPool, IEventCollection, IEventCollectionFilter, EventStream, IEventFilter, EventEnvelope, EventStoreConfig, AggregateRoot, ISnapshot, SnapshotStore, ISnapshotPool, ISnapshotCollection, ISnapshotCollectionFilter, SnapshotStream, ISnapshotFilter, SnapshotEnvelope, ILatestSnapshotFilter, SnapshotStoreConfig } from '@ocoda/event-sourcing';
1
+ import { IEventPayload, IEvent, EventStore, IEventPool, IEventCollection, IEventCollectionFilter, EventStream, IEventFilter, EventEnvelope, IAllEventsFilter, EventStoreConfig, AggregateRoot, ISnapshot, SnapshotStore, ISnapshotPool, ISnapshotCollection, ISnapshotCollectionFilter, SnapshotStream, ISnapshotFilter, SnapshotEnvelope, ILatestSnapshotFilter, SnapshotStoreConfig } from '@ocoda/event-sourcing';
2
2
  import { Type } from '@nestjs/common';
3
3
  import { PoolConfig } from 'mariadb';
4
4
 
@@ -23,8 +23,9 @@ declare class MariaDBEventStore extends EventStore<MariaDBEventStoreConfig> {
23
23
  getEvents({ streamId }: EventStream, filter?: IEventFilter): AsyncGenerator<IEvent[]>;
24
24
  getEvent({ streamId }: EventStream, version: number, pool?: IEventPool): Promise<IEvent>;
25
25
  appendEvents(stream: EventStream, aggregateVersion: number, events: IEvent[] | EventEnvelope[], pool?: IEventPool): Promise<EventEnvelope[]>;
26
- getEnvelopes({ streamId }: EventStream, filter?: IEventFilter): AsyncGenerator<EventEnvelope[]>;
27
26
  getEnvelope({ streamId }: EventStream, version: number, pool?: IEventPool): Promise<EventEnvelope>;
27
+ getEnvelopes({ streamId }: EventStream, filter?: IEventFilter): AsyncGenerator<EventEnvelope[]>;
28
+ getAllEnvelopes(filter: IAllEventsFilter): AsyncGenerator<EventEnvelope[]>;
28
29
  }
29
30
 
30
31
  interface MariaDBEventStoreConfig extends EventStoreConfig, PoolConfig {
@@ -52,11 +53,11 @@ declare class MariaDBSnapshotStore extends SnapshotStore<MariaDBSnapshotStoreCon
52
53
  getSnapshot<A extends AggregateRoot>({ streamId }: SnapshotStream, version: number, pool?: ISnapshotPool): Promise<ISnapshot<A>>;
53
54
  appendSnapshot<A extends AggregateRoot>(stream: SnapshotStream, aggregateVersion: number, snapshot: ISnapshot<A>, pool?: ISnapshotPool): Promise<SnapshotEnvelope<A>>;
54
55
  getLastSnapshot<A extends AggregateRoot>(stream: SnapshotStream, pool?: ISnapshotPool): Promise<ISnapshot<A>>;
55
- getManyLastSnapshots<A extends AggregateRoot>(streams: SnapshotStream[], pool?: ISnapshotPool): Promise<Map<SnapshotStream, ISnapshot<A>>>;
56
+ getLastSnapshots<A extends AggregateRoot>(streams: SnapshotStream[], pool?: ISnapshotPool): Promise<Map<SnapshotStream, ISnapshot<A>>>;
56
57
  getLastEnvelope<A extends AggregateRoot>(stream: SnapshotStream, pool?: ISnapshotPool): Promise<SnapshotEnvelope<A>>;
57
58
  getEnvelopes<A extends AggregateRoot>({ streamId }: SnapshotStream, filter?: ISnapshotFilter): AsyncGenerator<SnapshotEnvelope<A>[]>;
58
59
  getEnvelope<A extends AggregateRoot>({ streamId }: SnapshotStream, version: number, pool?: ISnapshotPool): Promise<SnapshotEnvelope<A>>;
59
- getLastAggregateEnvelopes<A extends AggregateRoot>(aggregateName: string, filter?: ILatestSnapshotFilter): AsyncGenerator<SnapshotEnvelope<A>[]>;
60
+ getLastEnvelopesForAggregate<A extends AggregateRoot>(aggregate: Type<A>, filter?: ILatestSnapshotFilter): AsyncGenerator<SnapshotEnvelope<A>[]>;
60
61
  getManyLastSnapshotEnvelopes<A extends AggregateRoot>(streams: SnapshotStream[], pool?: ISnapshotPool): Promise<Map<SnapshotStream, SnapshotEnvelope<A>>>;
61
62
  private getLastStreamEntities;
62
63
  }
package/dist/index.js CHANGED
@@ -93,13 +93,14 @@ var _MariaDBEventStore = class _MariaDBEventStore extends import_event_sourcing.
93
93
  version INT NOT NULL,
94
94
  event VARCHAR(80) NOT NULL,
95
95
  payload JSON NOT NULL,
96
+ event_date VARCHAR(7) NOT NULL,
96
97
  event_id VARCHAR(40) NOT NULL,
97
98
  aggregate_id VARCHAR(40) NOT NULL,
98
99
  occurred_on TIMESTAMP NOT NULL,
99
100
  correlation_id VARCHAR(255),
100
101
  causation_id VARCHAR(255),
101
102
  PRIMARY KEY (stream_id, version),
102
- INDEX idx_event_id (event_id)
103
+ INDEX idx_event_date_id (event_date, event_id)
103
104
  )`);
104
105
  return collection;
105
106
  } catch (error) {
@@ -245,13 +246,14 @@ var _MariaDBEventStore = class _MariaDBEventStore extends import_event_sourcing.
245
246
  envelopes.push(envelope);
246
247
  }
247
248
  yield connection.beginTransaction();
248
- yield connection.batch(`INSERT INTO \`${collection}\` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, envelopes.map(({ event, payload, metadata }) => {
249
+ yield connection.batch(`INSERT INTO \`${collection}\` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, envelopes.map(({ event, payload, metadata }) => {
249
250
  var _a, _b;
250
251
  return [
251
252
  stream.streamId,
252
253
  metadata.version,
253
254
  event,
254
255
  JSON.stringify(payload),
256
+ metadata.eventId.yearMonth,
255
257
  metadata.eventId.value,
256
258
  metadata.aggregateId,
257
259
  metadata.occurredOn,
@@ -274,6 +276,26 @@ var _MariaDBEventStore = class _MariaDBEventStore extends import_event_sourcing.
274
276
  }
275
277
  });
276
278
  }
279
+ getEnvelope(_0, _1, _2) {
280
+ return __async(this, arguments, function* ({ streamId }, version, pool) {
281
+ const collection = import_event_sourcing.EventCollection.get(pool);
282
+ const [entity] = yield this.pool.query(`SELECT event, payload, event_id, aggregate_id, version, occurred_on, correlation_id, causation_id FROM \`${collection}\` WHERE stream_id = ? AND version = ?`, [
283
+ streamId,
284
+ version
285
+ ]);
286
+ if (!entity) {
287
+ throw new import_event_sourcing.EventNotFoundException(streamId, version);
288
+ }
289
+ return import_event_sourcing.EventEnvelope.from(entity.event, entity.payload, {
290
+ eventId: import_event_sourcing.EventId.from(entity.event_id),
291
+ aggregateId: entity.aggregate_id,
292
+ version: entity.version,
293
+ occurredOn: entity.occurred_on,
294
+ correlationId: entity.correlation_id,
295
+ causationId: entity.causation_id
296
+ });
297
+ });
298
+ }
277
299
  getEnvelopes(_0, _1) {
278
300
  return __asyncGenerator(this, arguments, function* ({ streamId }, filter) {
279
301
  const connection = this.pool.getConnection();
@@ -338,24 +360,59 @@ var _MariaDBEventStore = class _MariaDBEventStore extends import_event_sourcing.
338
360
  }
339
361
  });
340
362
  }
341
- getEnvelope(_0, _1, _2) {
342
- return __async(this, arguments, function* ({ streamId }, version, pool) {
343
- const collection = import_event_sourcing.EventCollection.get(pool);
344
- const [entity] = yield this.pool.query(`SELECT event, payload, event_id, aggregate_id, version, occurred_on, correlation_id, causation_id FROM \`${collection}\` WHERE stream_id = ? AND version = ?`, [
345
- streamId,
346
- version
347
- ]);
348
- if (!entity) {
349
- throw new import_event_sourcing.EventNotFoundException(streamId, version);
363
+ getAllEnvelopes(filter) {
364
+ return __asyncGenerator(this, null, function* () {
365
+ const connection = this.pool.getConnection();
366
+ const collection = import_event_sourcing.EventCollection.get(filter == null ? void 0 : filter.pool);
367
+ const yearMonths = this.getYearMonthRange(filter.since, filter.until);
368
+ const batch = (filter == null ? void 0 : filter.batch) || import_event_sourcing.DEFAULT_BATCH_SIZE;
369
+ const query = `
370
+ SELECT event, payload, event_id, aggregate_id, version, occurred_on, correlation_id, causation_id
371
+ FROM \`${collection}\`
372
+ WHERE event_date IN (?)
373
+ ORDER BY event_date ASC, event_id ASC
374
+ `;
375
+ const params = [
376
+ yearMonths
377
+ ];
378
+ const client = yield new __await(connection);
379
+ const stream = client.queryStream(query, params);
380
+ try {
381
+ let batchedEvents = [];
382
+ try {
383
+ for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
384
+ const { event, payload, event_id, aggregate_id, version, occurred_on, correlation_id, causation_id } = temp.value;
385
+ batchedEvents.push(import_event_sourcing.EventEnvelope.from(event, payload, {
386
+ eventId: import_event_sourcing.EventId.from(event_id),
387
+ aggregateId: aggregate_id,
388
+ version,
389
+ occurredOn: occurred_on,
390
+ correlationId: correlation_id,
391
+ causationId: causation_id
392
+ }));
393
+ if (batchedEvents.length === batch) {
394
+ yield batchedEvents;
395
+ batchedEvents = [];
396
+ }
397
+ }
398
+ } catch (temp) {
399
+ error = [temp];
400
+ } finally {
401
+ try {
402
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
403
+ } finally {
404
+ if (error)
405
+ throw error[0];
406
+ }
407
+ }
408
+ if (batchedEvents.length > 0) {
409
+ yield batchedEvents;
410
+ }
411
+ } catch (e) {
412
+ stream.destroy();
413
+ } finally {
414
+ yield new __await(client.release());
350
415
  }
351
- return import_event_sourcing.EventEnvelope.from(entity.event, entity.payload, {
352
- eventId: import_event_sourcing.EventId.from(entity.event_id),
353
- aggregateId: entity.aggregate_id,
354
- version: entity.version,
355
- occurredOn: entity.occurred_on,
356
- correlationId: entity.correlation_id,
357
- causationId: entity.causation_id
358
- });
359
416
  });
360
417
  }
361
418
  };
@@ -575,7 +632,7 @@ var _MariaDBSnapshotStore = class _MariaDBSnapshotStore extends import_event_sou
575
632
  }
576
633
  });
577
634
  }
578
- getManyLastSnapshots(streams, pool) {
635
+ getLastSnapshots(streams, pool) {
579
636
  return __async(this, null, function* () {
580
637
  const collection = import_event_sourcing2.SnapshotCollection.get(pool);
581
638
  const entities = yield this.getLastStreamEntities(collection, streams, [
@@ -690,26 +747,27 @@ var _MariaDBSnapshotStore = class _MariaDBSnapshotStore extends import_event_sou
690
747
  });
691
748
  });
692
749
  }
693
- getLastAggregateEnvelopes(aggregateName, filter) {
750
+ getLastEnvelopesForAggregate(aggregate, filter) {
694
751
  return __asyncGenerator(this, null, function* () {
695
752
  const connection = this.pool.getConnection();
696
753
  const collection = import_event_sourcing2.SnapshotCollection.get(filter == null ? void 0 : filter.pool);
697
- const fromId = filter == null ? void 0 : filter.fromId;
754
+ const { streamName } = (0, import_event_sourcing2.getAggregateMetadata)(aggregate);
755
+ const aggregateId = filter == null ? void 0 : filter.aggregateId;
698
756
  const limit = (filter == null ? void 0 : filter.limit) || Number.MAX_SAFE_INTEGER;
699
757
  const batch = (filter == null ? void 0 : filter.batch) || import_event_sourcing2.DEFAULT_BATCH_SIZE;
700
758
  const query = `
701
759
  SELECT payload, aggregate_id, registered_on, snapshot_id, version
702
760
  FROM \`${collection}\`
703
761
  WHERE aggregate_name = ?
704
- AND ${fromId ? "latest >= ?" : "latest LIKE 'latest%'"}
762
+ AND ${aggregateId ? "latest >= ?" : "latest LIKE 'latest%'"}
705
763
  LIMIT ?
706
764
  `;
707
- const params = fromId ? [
708
- aggregateName,
709
- fromId,
765
+ const params = aggregateId ? [
766
+ streamName,
767
+ aggregateId,
710
768
  limit
711
769
  ] : [
712
- aggregateName,
770
+ streamName,
713
771
  limit
714
772
  ];
715
773
  const client = yield new __await(connection);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocoda/event-sourcing-mariadb",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "NestJS Event Sourcing Library (@mariadb)",
5
5
  "author": "Dries Hooghe <dries@drieshooghe.com>",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "@nestjs/core": "^10.0.0",
20
20
  "reflect-metadata": "^0.2.0",
21
21
  "rxjs": "^7.2.0",
22
- "@ocoda/event-sourcing": "1.0.2"
22
+ "@ocoda/event-sourcing": "1.1.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@biomejs/biome": "1.9.3",