@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.cjs CHANGED
@@ -570,7 +570,7 @@ const pongoProjection = ({ name, kind, version, truncate, handle, canHandle, eve
570
570
  } : void 0
571
571
  });
572
572
  const pongoMultiStreamProjection = (options) => {
573
- const { collectionName, getDocumentId, canHandle } = options;
573
+ const { collectionName, getDocumentId, getDocumentIds, canHandle } = options;
574
574
  const collectionNameWithVersion = options.version && options.version > 0 ? `${collectionName}_v${options.version}` : collectionName;
575
575
  return pongoProjection({
576
576
  name: collectionNameWithVersion,
@@ -579,12 +579,13 @@ const pongoMultiStreamProjection = (options) => {
579
579
  eventsOptions: options.eventsOptions,
580
580
  handle: async (events, { pongo }) => {
581
581
  const collection = pongo.db().collection(collectionNameWithVersion, options.collectionOptions);
582
- const eventsByDocumentId = events.map((event) => {
583
- return {
584
- documentId: getDocumentId(event),
582
+ const eventsByDocumentId = events.flatMap((event) => {
583
+ return (getDocumentId ? [getDocumentId(event)].filter((e) => e !== null) : getDocumentIds(event)).map((documentId) => ({
584
+ documentId,
585
585
  event
586
- };
586
+ }));
587
587
  }).reduce((acc, { documentId, event }) => {
588
+ if (documentId === null) return acc;
588
589
  if (!acc.has(documentId)) acc.set(documentId, []);
589
590
  acc.get(documentId).push(event);
590
591
  return acc;
@@ -628,11 +629,11 @@ const pongoMultiStreamProjection = (options) => {
628
629
  }
629
630
  });
630
631
  };
631
- const pongoSingleStreamProjection = (options) => {
632
+ const pongoSingleStreamProjection = ({ getDocumentId, getDocumentIds, ...options }) => {
632
633
  return pongoMultiStreamProjection({
633
- ...options,
634
634
  kind: "emt:projections:postgresql:pongo:single_stream",
635
- getDocumentId: options.getDocumentId ?? ((event) => event.metadata.streamName)
635
+ ...options,
636
+ ...getDocumentId ? { getDocumentId } : getDocumentIds ? { getDocumentIds } : { getDocumentId: (event) => event.metadata.streamName }
636
637
  });
637
638
  };
638
639
 
@@ -1020,10 +1021,10 @@ const transactionToPostgreSQLProjectionHandlerContext = async (connectionString,
1020
1021
  });
1021
1022
  const handleProjections = async (options) => {
1022
1023
  const { projections: allProjections, events, connection: { pool, transaction, connectionString }, partition = defaultTag } = options;
1023
- const eventTypes = events.map((e) => e.type);
1024
- const projections = allProjections.filter((p) => p.canHandle.some((type) => eventTypes.includes(type)));
1025
1024
  const client = await transaction.connection.open();
1026
- for (const projection of projections) {
1025
+ for (const projection of allProjections) {
1026
+ const filteredEvents = events.filter(({ type }) => projection.canHandle.includes(type));
1027
+ if (filteredEvents.length === 0) continue;
1027
1028
  if (projection.name) {
1028
1029
  if (!await postgreSQLProjectionLock({
1029
1030
  projectionName: projection.name,
@@ -1031,7 +1032,7 @@ const handleProjections = async (options) => {
1031
1032
  version: projection.version ?? 1
1032
1033
  }).tryAcquire({ execute: transaction.execute })) continue;
1033
1034
  }
1034
- await projection.handle(events, {
1035
+ await projection.handle(filteredEvents, {
1035
1036
  connection: {
1036
1037
  connectionString,
1037
1038
  pool,