@@ -9,16 +9,15 @@ import {
99 MessageProcessor ,
1010 type ProjectorOptions ,
1111 type ReactorOptions ,
12- type RecordedMessage ,
12+ getCheckpoint ,
1313 projector ,
1414 reactor ,
1515} from '@event-driven-io/emmett' ;
1616import { MongoClient } from 'mongodb' ;
17- import type { ReadEventMetadataWithGlobalPosition } from '../event' ;
1817import type { MongoDBEventStoreConnectionOptions } from '../mongoDBEventStore' ;
18+ import type { MongoDBChangeStreamMessageMetadata } from './mongoDBEventsConsumer' ;
1919import { readProcessorCheckpoint } from './readProcessorCheckpoint' ;
2020import { storeProcessorCheckpoint } from './storeProcessorCheckpoint' ;
21- import type { MongoDBResumeToken } from './subscriptions/types' ;
2221
2322type MongoDBConnectionOptions = {
2423 connectionOptions : MongoDBEventStoreConnectionOptions ;
@@ -31,98 +30,51 @@ export type MongoDBProcessorHandlerContext = {
3130export type MongoDBProcessor < MessageType extends Message = AnyMessage > =
3231 MessageProcessor <
3332 MessageType ,
34- ReadEventMetadataWithGlobalPosition ,
33+ MongoDBChangeStreamMessageMetadata ,
3534 MongoDBProcessorHandlerContext
3635 > ;
3736
3837export type MongoDBProcessorOptions < MessageType extends Message = Message > =
3938 ReactorOptions <
4039 MessageType ,
41- ReadEventMetadataWithGlobalPosition ,
40+ MongoDBChangeStreamMessageMetadata ,
4241 MongoDBProcessorHandlerContext
4342 > & { connectionOptions : MongoDBEventStoreConnectionOptions } ;
4443
45- export type MongoDBCheckpointer <
46- MessageType extends AnyMessage = AnyMessage ,
47- // eslint-disable-next-line @typescript-eslint/no-explicit-any
48- CheckpointType = any ,
49- > = Checkpointer <
50- MessageType ,
51- ReadEventMetadataWithGlobalPosition < CheckpointType > ,
52- MongoDBProcessorHandlerContext ,
53- CheckpointType
54- > ;
44+ export type MongoDBCheckpointer < MessageType extends AnyMessage = AnyMessage > =
45+ Checkpointer <
46+ MessageType ,
47+ MongoDBChangeStreamMessageMetadata ,
48+ MongoDBProcessorHandlerContext
49+ > ;
5550
5651export type MongoDBProjectorOptions < EventType extends AnyEvent = AnyEvent > =
5752 ProjectorOptions <
5853 EventType ,
59- ReadEventMetadataWithGlobalPosition ,
54+ MongoDBChangeStreamMessageMetadata ,
6055 MongoDBProcessorHandlerContext
6156 > &
6257 MongoDBConnectionOptions ;
6358
64- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65- const isResumeToken = ( value : any ) : value is MongoDBResumeToken =>
66- typeof value === 'object' &&
67- '_data' in value &&
68- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
69- typeof value . _data === 'string' ;
70-
71- export const getCheckpoint = <
72- MessageType extends AnyMessage = AnyMessage ,
73- CheckpointType = MongoDBCheckpointer ,
74- MessageMetadataType extends
75- ReadEventMetadataWithGlobalPosition < CheckpointType > = ReadEventMetadataWithGlobalPosition < CheckpointType > ,
76- > (
77- message : RecordedMessage < MessageType , MessageMetadataType > ,
78- ) : CheckpointType | null => {
79- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
80- return 'checkpoint' in message . metadata &&
81- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
82- isResumeToken ( message . metadata . checkpoint )
83- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
84- message . metadata . checkpoint
85- : 'globalPosition' in message . metadata &&
86- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
87- isResumeToken ( message . metadata . globalPosition )
88- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
89- message . metadata . globalPosition
90- : 'streamPosition' in message . metadata &&
91- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
92- isResumeToken ( message . metadata . streamPosition )
93- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
94- message . metadata . streamPosition
95- : null ;
96- } ;
97-
9859export const mongoDBCheckpointer = <
9960 MessageType extends Message = Message ,
100- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101- CheckpointType = any ,
102- > ( ) : MongoDBCheckpointer < MessageType , CheckpointType > => ( {
61+ > ( ) : MongoDBCheckpointer < MessageType > => ( {
10362 read : async ( options , context ) => {
104- const result = await readProcessorCheckpoint < CheckpointType > (
105- context . client ,
106- options ,
107- ) ;
63+ const result = await readProcessorCheckpoint ( context . client , options ) ;
10864
109- return { lastCheckpoint : result ?. lastProcessedPosition } ;
65+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
66+ return { lastCheckpoint : result ?. lastCheckpoint } ;
11067 } ,
11168 store : async ( options , context ) => {
112- const newPosition = getCheckpoint < MessageType , CheckpointType > (
113- options . message ,
114- ) ;
115-
116- const result = await storeProcessorCheckpoint < CheckpointType > (
117- context . client ,
118- {
119- lastProcessedPosition : options . lastCheckpoint ,
120- newPosition,
121- processorId : options . processorId ,
122- partition : options . partition ,
123- version : options . version || 0 ,
124- } ,
125- ) ;
69+ const newPosition = getCheckpoint ( options . message ) ;
70+
71+ const result = await storeProcessorCheckpoint ( context . client , {
72+ lastProcessedPosition : options . lastCheckpoint ,
73+ newPosition,
74+ processorId : options . processorId ,
75+ partition : options . partition ,
76+ version : options . version || 0 ,
77+ } ) ;
12678
12779 return result . success
12880 ? { success : true , newCheckpoint : result . newPosition }
@@ -134,8 +86,6 @@ const mongoDBProcessingScope = (options: {
13486 client : MongoClient ;
13587 processorId : string ;
13688} ) : MessageProcessingScope < MongoDBProcessorHandlerContext > => {
137- // const processorConnectionString = options.connectionString;
138-
13989 const processingScope : MessageProcessingScope <
14090 MongoDBProcessorHandlerContext
14191 > = async < Result = MessageHandlerResult > (
@@ -144,15 +94,6 @@ const mongoDBProcessingScope = (options: {
14494 ) => Result | Promise < Result > ,
14595 partialContext : Partial < MongoDBProcessorHandlerContext > ,
14696 ) => {
147- // const connection = partialContext?.connection;
148- // const connectionString =
149- // processorConnectionString ?? connection?.connectionString;
150-
151- // if (!connectionString)
152- // throw new EmmettError(
153- // `MongoDB processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`,
154- // );
155-
15697 return handler ( {
15798 client : options . client ,
15899 ...partialContext ,
@@ -174,6 +115,10 @@ export const mongoDBProjector = <EventType extends Event = Event>(
174115 }
175116 : undefined ,
176117 } ;
118+ // TODO: This should be eventually moved to the mongoDBProcessingScope
119+ // In the similar way as it's made in the postgresql processor
120+ // So creating client only if it's needed and different than consumer is passing
121+ // through handler context
177122 const client =
178123 'client' in connectionOptions && connectionOptions . client
179124 ? connectionOptions . client
@@ -184,7 +129,7 @@ export const mongoDBProjector = <EventType extends Event = Event>(
184129
185130 return projector <
186131 EventType ,
187- ReadEventMetadataWithGlobalPosition ,
132+ MongoDBChangeStreamMessageMetadata ,
188133 MongoDBProcessorHandlerContext
189134 > ( {
190135 ...options ,
0 commit comments