@event-driven-io/emmett-postgresql 0.43.0-beta.27 → 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.cjs +40 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +41 -31
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -767,6 +767,8 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
|
|
|
767
767
|
let migrateSchema = void 0;
|
|
768
768
|
const autoGenerateSchema = options.schema?.autoMigration === void 0 || options.schema?.autoMigration !== "None";
|
|
769
769
|
const inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(({ projection }) => projection);
|
|
770
|
+
const observability = (0, _event_driven_io_emmett.eventStoreObservability)(options);
|
|
771
|
+
const collector = (0, _event_driven_io_emmett.eventStoreCollector)(observability);
|
|
770
772
|
const migrate = async (migrationOptions) => {
|
|
771
773
|
if (!migrateSchema) {
|
|
772
774
|
migrateSchema = createEventStoreSchema(connectionString, pool, {
|
|
@@ -800,11 +802,19 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
|
|
|
800
802
|
if (!autoGenerateSchema) return Promise.resolve();
|
|
801
803
|
return migrate();
|
|
802
804
|
};
|
|
803
|
-
const
|
|
805
|
+
const readStreamFromPostgreSQL = (streamName, readOptions) => collector.instrumentRead(streamName, async () => {
|
|
806
|
+
await ensureSchemaExists();
|
|
807
|
+
return readStream(pool.execute, streamName, {
|
|
808
|
+
...readOptions,
|
|
809
|
+
serialization: options.serialization ?? readOptions?.serialization
|
|
810
|
+
});
|
|
811
|
+
}, readOptions?.observability);
|
|
812
|
+
const beforeCommitHook = (streamName, appendScope) => inlineProjections.length > 0 ? async (events, { transaction }) => collector.instrumentInlineProjection(streamName, appendScope, async (observabilityScope) => handleProjections({
|
|
804
813
|
projections: inlineProjections,
|
|
805
814
|
events,
|
|
806
|
-
...await transactionToPostgreSQLProjectionHandlerContext(connectionString, pool, transaction)
|
|
807
|
-
|
|
815
|
+
...await transactionToPostgreSQLProjectionHandlerContext(connectionString, pool, transaction),
|
|
816
|
+
observabilityScope
|
|
817
|
+
})) : void 0;
|
|
808
818
|
return {
|
|
809
819
|
schema: {
|
|
810
820
|
sql: () => _event_driven_io_dumbo.SQL.describe(schemaSQL, (0, _event_driven_io_dumbo.getFormatter)((0, _event_driven_io_dumbo.fromDatabaseDriverType)(pool.driverType).databaseType)),
|
|
@@ -823,36 +833,36 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
|
|
|
823
833
|
}) }
|
|
824
834
|
},
|
|
825
835
|
async aggregateStream(streamName, options) {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
serialization: options.serialization ?? readOptions?.serialization
|
|
847
|
-
});
|
|
836
|
+
return collector.instrumentAggregate(streamName, async (scope) => {
|
|
837
|
+
const { evolve, initialState, read } = options;
|
|
838
|
+
const expectedStreamVersion = read?.expectedStreamVersion;
|
|
839
|
+
let state = initialState();
|
|
840
|
+
const result = await readStreamFromPostgreSQL(streamName, {
|
|
841
|
+
...read ?? {},
|
|
842
|
+
observability: (0, _event_driven_io_emmett.withOperationScope)(scope, read?.observability)
|
|
843
|
+
});
|
|
844
|
+
const currentStreamVersion = result.currentStreamVersion;
|
|
845
|
+
(0, _event_driven_io_emmett.assertExpectedVersionMatchesCurrent)(currentStreamVersion, expectedStreamVersion, PostgreSQLEventStoreDefaultStreamVersion);
|
|
846
|
+
for (const event of result.events) {
|
|
847
|
+
if (!event) continue;
|
|
848
|
+
state = evolve(state, event);
|
|
849
|
+
}
|
|
850
|
+
return {
|
|
851
|
+
currentStreamVersion,
|
|
852
|
+
state,
|
|
853
|
+
streamExists: result.streamExists
|
|
854
|
+
};
|
|
855
|
+
}, options.observability);
|
|
848
856
|
},
|
|
849
|
-
|
|
857
|
+
readStream: async (streamName, readOptions) => readStreamFromPostgreSQL(streamName, readOptions),
|
|
858
|
+
appendToStream: async (streamName, events, appendOptions) => collector.instrumentAppend(streamName, events, async (scope) => {
|
|
850
859
|
await ensureSchemaExists();
|
|
851
860
|
const [firstPart, ...rest] = streamName.split("-");
|
|
852
861
|
const streamType = firstPart && rest.length > 0 ? firstPart : _event_driven_io_emmett.unknownTag;
|
|
853
862
|
const appendResult = await pool.withConnection(async (connection) => appendToStream(connection, streamName, streamType, (0, _event_driven_io_emmett.downcastRecordedMessages)(events, appendOptions?.schema?.versioning), {
|
|
854
863
|
...appendOptions,
|
|
855
|
-
|
|
864
|
+
messageIdGenerator: () => observability.contextGenerator.generateMessageId(),
|
|
865
|
+
beforeCommitHook: beforeCommitHook(streamName, scope)
|
|
856
866
|
}));
|
|
857
867
|
if (!appendResult.success) throw new _event_driven_io_emmett.ExpectedVersionConflictError(-1n, appendOptions?.expectedStreamVersion ?? _event_driven_io_emmett.NO_CONCURRENCY_CHECK);
|
|
858
868
|
return {
|
|
@@ -860,7 +870,7 @@ const getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOp
|
|
|
860
870
|
lastEventGlobalPosition: PostgreSQLEventStoreCheckpoint.toProcessorCheckpoint(appendResult.checkpoints[appendResult.checkpoints.length - 1]),
|
|
861
871
|
createdNewStream: appendResult.nextStreamPosition >= BigInt(events.length)
|
|
862
872
|
};
|
|
863
|
-
},
|
|
873
|
+
}, appendOptions?.observability),
|
|
864
874
|
streamExists: async (streamName, options) => {
|
|
865
875
|
await ensureSchemaExists();
|
|
866
876
|
return streamExists(pool.execute, streamName, options);
|
|
@@ -1041,7 +1051,7 @@ const handleProjections = async (options) => {
|
|
|
1041
1051
|
transaction
|
|
1042
1052
|
},
|
|
1043
1053
|
execute: transaction.execute,
|
|
1044
|
-
observabilityScope:
|
|
1054
|
+
observabilityScope: options.observabilityScope
|
|
1045
1055
|
});
|
|
1046
1056
|
}
|
|
1047
1057
|
};
|
|
@@ -1277,7 +1287,7 @@ const appendToStream = (connection, streamName, streamType, messages, options) =
|
|
|
1277
1287
|
...e,
|
|
1278
1288
|
kind: e.kind ?? "Event",
|
|
1279
1289
|
metadata: {
|
|
1280
|
-
messageId: (0, uuid.v4)(),
|
|
1290
|
+
messageId: options?.messageIdGenerator?.() ?? (0, uuid.v4)(),
|
|
1281
1291
|
..."metadata" in e ? e.metadata ?? {} : {}
|
|
1282
1292
|
}
|
|
1283
1293
|
}));
|