@event-driven-io/emmett-postgresql 0.43.0-beta.28 → 0.43.0-beta.29

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/dist/index.d.cts CHANGED
@@ -380,6 +380,7 @@ type AppendToStreamBeforeCommitHook = (messages: RecordedMessage[], context: {
380
380
  }) => Promise<void>;
381
381
  declare const appendToStream: (connection: PgConnection, streamName: string, streamType: string, messages: Message[], options?: AppendToStreamOptions & {
382
382
  partition?: string;
383
+ messageIdGenerator?: () => string;
383
384
  beforeCommitHook?: AppendToStreamBeforeCommitHook;
384
385
  }) => Promise<AppendToStreamResult>;
385
386
  type AppendToStreamSqlResult = {
package/dist/index.d.ts CHANGED
@@ -380,6 +380,7 @@ type AppendToStreamBeforeCommitHook = (messages: RecordedMessage[], context: {
380
380
  }) => Promise<void>;
381
381
  declare const appendToStream: (connection: PgConnection, streamName: string, streamType: string, messages: Message[], options?: AppendToStreamOptions & {
382
382
  partition?: string;
383
+ messageIdGenerator?: () => string;
383
384
  beforeCommitHook?: AppendToStreamBeforeCommitHook;
384
385
  }) => Promise<AppendToStreamResult>;
385
386
  type AppendToStreamSqlResult = {
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AssertionError, ConsumerStartPositions, EmmettError, ExpectedVersionConflictError, JSONSerializer, NO_CONCURRENCY_CHECK, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, assertDeepEqual, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertIsNotNull, assertIsNull, assertThatArray, assertTrue, asyncAwaiter, asyncRetry, bigInt, bigIntProcessorCheckpoint, defaultProcessorPartition, defaultProcessorVersion, downcastRecordedMessages, getCheckpoint, getProcessorInstanceId, getProjectorId, getWorkflowId, hashText, inMemoryCheckpointer, isBigint, isErrorConstructor, mergeObservability, noopScope, projection, projector, reactor, reduceAsync, unknownTag, upcastRecordedMessage, workflowProcessor } from "@event-driven-io/emmett";
1
+ import { AssertionError, ConsumerStartPositions, EmmettError, ExpectedVersionConflictError, JSONSerializer, NO_CONCURRENCY_CHECK, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, assertDeepEqual, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertIsNotNull, assertIsNull, assertThatArray, assertTrue, asyncAwaiter, asyncRetry, bigInt, bigIntProcessorCheckpoint, defaultProcessorPartition, defaultProcessorVersion, downcastRecordedMessages, eventStoreCollector, eventStoreObservability, getCheckpoint, getProcessorInstanceId, getProjectorId, getWorkflowId, hashText, inMemoryCheckpointer, isBigint, isErrorConstructor, mergeObservability, noopScope, projection, projector, reactor, reduceAsync, unknownTag, upcastRecordedMessage, withOperationScope, workflowProcessor } from "@event-driven-io/emmett";
2
2
  import { DumboError, JSONSerializer as JSONSerializer$1, SQL, UniqueConstraintError, dumbo, fromDatabaseDriverType, getFormatter, mapRows, runSQLMigrations, single, singleOrNull, sqlMigration } from "@event-driven-io/dumbo";
3
3
  import { pongoClient } from "@event-driven-io/pongo";
4
4
  import { pgDriver } from "@event-driven-io/pongo/pg";
@@ -766,6 +766,8 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
766
766
  let migrateSchema = void 0;
767
767
  const autoGenerateSchema = options.schema?.autoMigration === void 0 || options.schema?.autoMigration !== "None";
768
768
  const inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(({ projection }) => projection);
769
+ const observability = eventStoreObservability(options);
770
+ const collector = eventStoreCollector(observability);
769
771
  const migrate = async (migrationOptions) => {
770
772
  if (!migrateSchema) {
771
773
  migrateSchema = createEventStoreSchema(connectionString, pool, {
@@ -799,11 +801,19 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
799
801
  if (!autoGenerateSchema) return Promise.resolve();
800
802
  return migrate();
801
803
  };
802
- const beforeCommitHook = inlineProjections.length > 0 ? async (events, { transaction }) => handleProjections({
804
+ const readStreamFromPostgreSQL = (streamName, readOptions) => collector.instrumentRead(streamName, async () => {
805
+ await ensureSchemaExists();
806
+ return readStream(pool.execute, streamName, {
807
+ ...readOptions,
808
+ serialization: options.serialization ?? readOptions?.serialization
809
+ });
810
+ }, readOptions?.observability);
811
+ const beforeCommitHook = (streamName, appendScope) => inlineProjections.length > 0 ? async (events, { transaction }) => collector.instrumentInlineProjection(streamName, appendScope, async (observabilityScope) => handleProjections({
803
812
  projections: inlineProjections,
804
813
  events,
805
- ...await transactionToPostgreSQLProjectionHandlerContext(connectionString, pool, transaction)
806
- }) : void 0;
814
+ ...await transactionToPostgreSQLProjectionHandlerContext(connectionString, pool, transaction),
815
+ observabilityScope
816
+ })) : void 0;
807
817
  return {
808
818
  schema: {
809
819
  sql: () => SQL.describe(schemaSQL, getFormatter(fromDatabaseDriverType(pool.driverType).databaseType)),
@@ -822,36 +832,36 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
822
832
  }) }
823
833
  },
824
834
  async aggregateStream(streamName, options) {
825
- const { evolve, initialState, read } = options;
826
- const expectedStreamVersion = read?.expectedStreamVersion;
827
- let state = initialState();
828
- const result = await this.readStream(streamName, read);
829
- const currentStreamVersion = result.currentStreamVersion;
830
- assertExpectedVersionMatchesCurrent(currentStreamVersion, expectedStreamVersion, PostgreSQLEventStoreDefaultStreamVersion);
831
- for (const event of result.events) {
832
- if (!event) continue;
833
- state = evolve(state, event);
834
- }
835
- return {
836
- currentStreamVersion,
837
- state,
838
- streamExists: result.streamExists
839
- };
840
- },
841
- readStream: async (streamName, readOptions) => {
842
- await ensureSchemaExists();
843
- return readStream(pool.execute, streamName, {
844
- ...readOptions,
845
- serialization: options.serialization ?? readOptions?.serialization
846
- });
835
+ return collector.instrumentAggregate(streamName, async (scope) => {
836
+ const { evolve, initialState, read } = options;
837
+ const expectedStreamVersion = read?.expectedStreamVersion;
838
+ let state = initialState();
839
+ const result = await readStreamFromPostgreSQL(streamName, {
840
+ ...read ?? {},
841
+ observability: withOperationScope(scope, read?.observability)
842
+ });
843
+ const currentStreamVersion = result.currentStreamVersion;
844
+ assertExpectedVersionMatchesCurrent(currentStreamVersion, expectedStreamVersion, PostgreSQLEventStoreDefaultStreamVersion);
845
+ for (const event of result.events) {
846
+ if (!event) continue;
847
+ state = evolve(state, event);
848
+ }
849
+ return {
850
+ currentStreamVersion,
851
+ state,
852
+ streamExists: result.streamExists
853
+ };
854
+ }, options.observability);
847
855
  },
848
- appendToStream: async (streamName, events, appendOptions) => {
856
+ readStream: async (streamName, readOptions) => readStreamFromPostgreSQL(streamName, readOptions),
857
+ appendToStream: async (streamName, events, appendOptions) => collector.instrumentAppend(streamName, events, async (scope) => {
849
858
  await ensureSchemaExists();
850
859
  const [firstPart, ...rest] = streamName.split("-");
851
860
  const streamType = firstPart && rest.length > 0 ? firstPart : unknownTag;
852
861
  const appendResult = await pool.withConnection(async (connection) => appendToStream(connection, streamName, streamType, downcastRecordedMessages(events, appendOptions?.schema?.versioning), {
853
862
  ...appendOptions,
854
- beforeCommitHook
863
+ messageIdGenerator: () => observability.contextGenerator.generateMessageId(),
864
+ beforeCommitHook: beforeCommitHook(streamName, scope)
855
865
  }));
856
866
  if (!appendResult.success) throw new ExpectedVersionConflictError(-1n, appendOptions?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK);
857
867
  return {
@@ -859,7 +869,7 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
859
869
  lastEventGlobalPosition: PostgreSQLEventStoreCheckpoint.toProcessorCheckpoint(appendResult.checkpoints[appendResult.checkpoints.length - 1]),
860
870
  createdNewStream: appendResult.nextStreamPosition >= BigInt(events.length)
861
871
  };
862
- },
872
+ }, appendOptions?.observability),
863
873
  streamExists: async (streamName, options) => {
864
874
  await ensureSchemaExists();
865
875
  return streamExists(pool.execute, streamName, options);
@@ -1040,7 +1050,7 @@ const handleProjections = async (options) => {
1040
1050
  transaction
1041
1051
  },
1042
1052
  execute: transaction.execute,
1043
- observabilityScope: noopScope
1053
+ observabilityScope: options.observabilityScope
1044
1054
  });
1045
1055
  }
1046
1056
  };
@@ -1276,7 +1286,7 @@ const appendToStream = (connection, streamName, streamType, messages, options) =
1276
1286
  ...e,
1277
1287
  kind: e.kind ?? "Event",
1278
1288
  metadata: {
1279
- messageId: v4(),
1289
+ messageId: options?.messageIdGenerator?.() ?? v4(),
1280
1290
  ..."metadata" in e ? e.metadata ?? {} : {}
1281
1291
  }
1282
1292
  }));