@event-driven-io/emmett-postgresql 0.43.0-beta.20 → 0.43.0-beta.21

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
@@ -213,8 +213,13 @@ type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType
213
213
  eventsOptions?: {
214
214
  schema?: EventStoreReadSchemaOptions<EventType, EventPayloadType>;
215
215
  };
216
- getDocumentId: (event: ReadEvent<EventType>) => string;
217
216
  } & ({
217
+ getDocumentId: (event: ReadEvent<EventType>) => string | null;
218
+ getDocumentIds?: never;
219
+ } | {
220
+ getDocumentId?: never;
221
+ getDocumentIds: (event: ReadEvent<EventType>) => string[];
222
+ }) & ({
218
223
  evolve: PongoWithNullableDocumentEvolve<Document, EventType, EventMetaDataType>;
219
224
  } | {
220
225
  evolve: PongoWithNotNullDocumentEvolve<Document, EventType, EventMetaDataType>;
@@ -223,7 +228,6 @@ type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType
223
228
  declare const pongoMultiStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>(options: PongoMultiStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
224
229
  type PongoSingleStreamProjectionOptions<Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType, DocumentPayload extends PongoDocument = Document> = {
225
230
  canHandle: CanHandle<EventType>;
226
- getDocumentId?: (event: ReadEvent<EventType>) => string;
227
231
  version?: number;
228
232
  collectionName: string;
229
233
  collectionOptions?: PongoDBCollectionOptions<Document, DocumentPayload>;
@@ -231,12 +235,25 @@ type PongoSingleStreamProjectionOptions<Document extends PongoDocument, EventTyp
231
235
  schema?: EventStoreReadSchemaOptions<EventType, EventPayloadType>;
232
236
  };
233
237
  } & ({
238
+ getDocumentId: (event: ReadEvent<EventType>) => string | null;
239
+ getDocumentIds?: never;
240
+ } | {
241
+ getDocumentId?: never;
242
+ getDocumentIds: (event: ReadEvent<EventType>) => string[];
243
+ } | {
244
+ getDocumentId?: never;
245
+ getDocumentIds?: never;
246
+ }) & ({
234
247
  evolve: PongoWithNullableDocumentEvolve<Document, EventType, EventMetaDataType>;
235
248
  } | {
236
249
  evolve: PongoWithNotNullDocumentEvolve<Document, EventType, EventMetaDataType>;
237
250
  initialState: () => Document;
238
251
  }) & JSONSerializationOptions;
239
- declare const pongoSingleStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>(options: PongoSingleStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
252
+ declare const pongoSingleStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>({
253
+ getDocumentId,
254
+ getDocumentIds,
255
+ ...options
256
+ }: PongoSingleStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
240
257
  //#endregion
241
258
  //#region src/eventStore/projections/pongo/pongoProjectionSpec.d.ts
242
259
  type PongoAssertOptions = {
package/dist/index.d.ts CHANGED
@@ -213,8 +213,13 @@ type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType
213
213
  eventsOptions?: {
214
214
  schema?: EventStoreReadSchemaOptions<EventType, EventPayloadType>;
215
215
  };
216
- getDocumentId: (event: ReadEvent<EventType>) => string;
217
216
  } & ({
217
+ getDocumentId: (event: ReadEvent<EventType>) => string | null;
218
+ getDocumentIds?: never;
219
+ } | {
220
+ getDocumentId?: never;
221
+ getDocumentIds: (event: ReadEvent<EventType>) => string[];
222
+ }) & ({
218
223
  evolve: PongoWithNullableDocumentEvolve<Document, EventType, EventMetaDataType>;
219
224
  } | {
220
225
  evolve: PongoWithNotNullDocumentEvolve<Document, EventType, EventMetaDataType>;
@@ -223,7 +228,6 @@ type PongoMultiStreamProjectionOptions<Document extends PongoDocument, EventType
223
228
  declare const pongoMultiStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>(options: PongoMultiStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
224
229
  type PongoSingleStreamProjectionOptions<Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType, DocumentPayload extends PongoDocument = Document> = {
225
230
  canHandle: CanHandle<EventType>;
226
- getDocumentId?: (event: ReadEvent<EventType>) => string;
227
231
  version?: number;
228
232
  collectionName: string;
229
233
  collectionOptions?: PongoDBCollectionOptions<Document, DocumentPayload>;
@@ -231,12 +235,25 @@ type PongoSingleStreamProjectionOptions<Document extends PongoDocument, EventTyp
231
235
  schema?: EventStoreReadSchemaOptions<EventType, EventPayloadType>;
232
236
  };
233
237
  } & ({
238
+ getDocumentId: (event: ReadEvent<EventType>) => string | null;
239
+ getDocumentIds?: never;
240
+ } | {
241
+ getDocumentId?: never;
242
+ getDocumentIds: (event: ReadEvent<EventType>) => string[];
243
+ } | {
244
+ getDocumentId?: never;
245
+ getDocumentIds?: never;
246
+ }) & ({
234
247
  evolve: PongoWithNullableDocumentEvolve<Document, EventType, EventMetaDataType>;
235
248
  } | {
236
249
  evolve: PongoWithNotNullDocumentEvolve<Document, EventType, EventMetaDataType>;
237
250
  initialState: () => Document;
238
251
  }) & JSONSerializationOptions;
239
- declare const pongoSingleStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>(options: PongoSingleStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
252
+ declare const pongoSingleStreamProjection: <Document extends PongoDocument, EventType extends Event, EventMetaDataType extends PostgresReadEventMetadata = PostgresReadEventMetadata, EventPayloadType extends Event = EventType>({
253
+ getDocumentId,
254
+ getDocumentIds,
255
+ ...options
256
+ }: PongoSingleStreamProjectionOptions<Document, EventType, EventMetaDataType, EventPayloadType>) => PostgreSQLProjectionDefinition<EventType, EventPayloadType>;
240
257
  //#endregion
241
258
  //#region src/eventStore/projections/pongo/pongoProjectionSpec.d.ts
242
259
  type PongoAssertOptions = {
package/dist/index.js CHANGED
@@ -569,7 +569,7 @@ const pongoProjection = ({ name, kind, version, truncate, handle, canHandle, eve
569
569
  } : void 0
570
570
  });
571
571
  const pongoMultiStreamProjection = (options) => {
572
- const { collectionName, getDocumentId, canHandle } = options;
572
+ const { collectionName, getDocumentId, getDocumentIds, canHandle } = options;
573
573
  const collectionNameWithVersion = options.version && options.version > 0 ? `${collectionName}_v${options.version}` : collectionName;
574
574
  return pongoProjection({
575
575
  name: collectionNameWithVersion,
@@ -578,12 +578,13 @@ const pongoMultiStreamProjection = (options) => {
578
578
  eventsOptions: options.eventsOptions,
579
579
  handle: async (events, { pongo }) => {
580
580
  const collection = pongo.db().collection(collectionNameWithVersion, options.collectionOptions);
581
- const eventsByDocumentId = events.map((event) => {
582
- return {
583
- documentId: getDocumentId(event),
581
+ const eventsByDocumentId = events.flatMap((event) => {
582
+ return (getDocumentId ? [getDocumentId(event)].filter((e) => e !== null) : getDocumentIds(event)).map((documentId) => ({
583
+ documentId,
584
584
  event
585
- };
585
+ }));
586
586
  }).reduce((acc, { documentId, event }) => {
587
+ if (documentId === null) return acc;
587
588
  if (!acc.has(documentId)) acc.set(documentId, []);
588
589
  acc.get(documentId).push(event);
589
590
  return acc;
@@ -627,11 +628,11 @@ const pongoMultiStreamProjection = (options) => {
627
628
  }
628
629
  });
629
630
  };
630
- const pongoSingleStreamProjection = (options) => {
631
+ const pongoSingleStreamProjection = ({ getDocumentId, getDocumentIds, ...options }) => {
631
632
  return pongoMultiStreamProjection({
632
- ...options,
633
633
  kind: "emt:projections:postgresql:pongo:single_stream",
634
- getDocumentId: options.getDocumentId ?? ((event) => event.metadata.streamName)
634
+ ...options,
635
+ ...getDocumentId ? { getDocumentId } : getDocumentIds ? { getDocumentIds } : { getDocumentId: (event) => event.metadata.streamName }
635
636
  });
636
637
  };
637
638
 
@@ -1019,10 +1020,10 @@ const transactionToPostgreSQLProjectionHandlerContext = async (connectionString,
1019
1020
  });
1020
1021
  const handleProjections = async (options) => {
1021
1022
  const { projections: allProjections, events, connection: { pool, transaction, connectionString }, partition = defaultTag } = options;
1022
- const eventTypes = events.map((e) => e.type);
1023
- const projections = allProjections.filter((p) => p.canHandle.some((type) => eventTypes.includes(type)));
1024
1023
  const client = await transaction.connection.open();
1025
- for (const projection of projections) {
1024
+ for (const projection of allProjections) {
1025
+ const filteredEvents = events.filter(({ type }) => projection.canHandle.includes(type));
1026
+ if (filteredEvents.length === 0) continue;
1026
1027
  if (projection.name) {
1027
1028
  if (!await postgreSQLProjectionLock({
1028
1029
  projectionName: projection.name,
@@ -1030,7 +1031,7 @@ const handleProjections = async (options) => {
1030
1031
  version: projection.version ?? 1
1031
1032
  }).tryAcquire({ execute: transaction.execute })) continue;
1032
1033
  }
1033
- await projection.handle(events, {
1034
+ await projection.handle(filteredEvents, {
1034
1035
  connection: {
1035
1036
  connectionString,
1036
1037
  pool,