@@ -90,16 +90,19 @@ class AggregateEventRepositoryImpl(
9090 * Fetching the current state as a series/flow of Events
9191 */
9292 override fun Command?.fetchEvents (): Flow <Pair <Event , UUID ?>> =
93+ fetchEventsAndMetaData().map { Pair (it.first, it.second) }
94+
95+ /* *
96+ * Fetching the current state as a series/flow of Events and Metadata.
97+ * This method is implemented by Default in the EventRepository interface, but we need/want to override it here.
98+ */
99+ override fun Command?.fetchEventsAndMetaData (): Flow <Triple <Event , UUID ?, Map <String , Any >>> =
93100 when (this ) {
94- is Command -> getEvents(deciderId()).map { it.toEventWithId() }
101+ is Command -> getEvents(deciderId()).map { it.toEventWithIdAndMetaData() }
102+ .map { Triple (it.first, it.second, mapOf (" commandId" to it.third as Any )) }
103+
95104 null -> emptyFlow()
96105 }
97- .onCompletion {
98- when (it) {
99- null -> logger.debug { " fetching the aggregate events by command ${this @fetchEvents} completed with success" }
100- else -> logger.warn { " fetching the aggregate events by command ${this @fetchEvents} completed with exception $it " }
101- }
102- }
103106 .flowOn(dbDispatcher)
104107
105108 /* *
@@ -117,54 +120,71 @@ class AggregateEventRepositoryImpl(
117120 *
118121 * `latestVersionProvider` is used to fetch the latest version of the event stream, per need
119122 */
120- override fun Flow<Event?>.save (latestVersionProvider : (Event ? ) -> UUID ? ): Flow <Pair <Event , UUID >> = flow {
123+ override fun Flow<Event?>.save (latestVersionProvider : (Event ? ) -> UUID ? ): Flow <Pair <Event , UUID >> =
124+ saveWithMetaData(latestVersionProvider, emptyMap()).map { Pair (it.first, it.second) }
125+
126+ /* *
127+ * Storing the new state as a series/flow of Events and Metadata
128+ * This method is implemented by Default in the EventRepository interface, but we need/want to override it here.
129+ *
130+ * `latestVersionProvider` is used to fetch the latest version of the event stream, per need
131+ */
132+ override fun Flow<Event?>.saveWithMetaData (
133+ latestVersionProvider : (Event ? ) -> UUID ? ,
134+ metaData : Map <String , Any >
135+ ): Flow <Triple <Event , UUID , Map <String , Any >>> = flow {
121136 val previousIds: MutableMap <String , UUID ?> = emptyMap<String , UUID ?>().toMutableMap()
122137 emitAll(
123138 filterNotNull()
124139 .map {
125140 previousIds.computeIfAbsent(it.deciderId()) { _ -> latestVersionProvider(it) }
126141 val eventId = UUID .randomUUID()
127- val eventEntity = it.toEventEntity(eventId, previousIds[it.deciderId()])
142+ val eventEntity =
143+ it.toEventEntity(eventId, previousIds[it.deciderId()], metaData[" commandId" ] as UUID )
128144 previousIds[it.deciderId()] = eventId
129145 eventEntity
130146 }
131147 .appendAll()
132- .map { it.toEventWithId() }
148+ .map { it.toEventWithIdAndMetaData() }
149+ .map { Triple (it.first, it.second, mapOf (" commandId" to it.third as Any )) }
133150 )
134151 }
135- .onCompletion {
136- when (it) {
137- null -> logger.debug { " saving new events completed successfully" }
138- else -> logger.warn { " saving new events completed with exception $it " }
139- }
140- }
141152 .flowOn(dbDispatcher)
142153
143154 /* *
144155 * Storing the new state as a series/flow of Events
145156 *
146157 * `latestVersion` is used to provide you with the latest known version of the state/stream
147158 */
148- override fun Flow<Event?>.save (latestVersion : UUID ? ): Flow <Pair <Event , UUID >> = flow {
159+ override fun Flow<Event?>.save (latestVersion : UUID ? ): Flow <Pair <Event , UUID >> =
160+ saveWithMetaData(latestVersion, emptyMap()).map { Pair (it.first, it.second) }
161+
162+ /* *
163+ * Storing the new state as a series/flow of Events with metadata
164+ * This method is implemented by Default in the EventRepository interface, but we need/want to override it here.
165+ *
166+ *
167+ * `latestVersion` is used to provide you with the latest known version of the state/stream
168+ * `metaData` is used to provide you with the metadata
169+ */
170+ override fun Flow<Event?>.saveWithMetaData (
171+ latestVersion : UUID ? ,
172+ metaData : Map <String , Any >
173+ ): Flow <Triple <Event , UUID , Map <String , Any >>> = flow {
149174 var previousId: UUID ? = latestVersion
150175 emitAll(
151176 filterNotNull()
152177 .map {
153178 val eventId = UUID .randomUUID()
154- val eventEntity = it.toEventEntity(eventId, previousId)
179+ val eventEntity = it.toEventEntity(eventId, previousId, metaData[ " commandId " ] as UUID )
155180 previousId = eventId
156181 eventEntity
157182 }
158183 .appendAll()
159- .map { it.toEventWithId() }
184+ .map { it.toEventWithIdAndMetaData() }
185+ .map { Triple (it.first, it.second, mapOf (" commandId" to it.third as Any )) }
160186 )
161187 }
162- .onCompletion {
163- when (it) {
164- null -> logger.debug { " saving new events completed successfully" }
165- else -> logger.warn { " saving new events completed with exception $it " }
166- }
167- }
168188 .flowOn(dbDispatcher)
169189}
170190
@@ -175,7 +195,7 @@ internal data class EventEntity(
175195 val event : String ,
176196 val data : ByteArray ,
177197 val eventId : UUID ,
178- val commandId : UUID ? ,
198+ val commandId : UUID ,
179199 val previousId : UUID ? ,
180200 val final : Boolean ,
181201 val createdAt : OffsetDateTime ? = null ,
@@ -198,7 +218,10 @@ internal val eventMapper: (Row, RowMetadata) -> EventEntity = { row, _ ->
198218}
199219
200220internal fun EventEntity.toEventWithId () = Pair <Event , UUID >(Json .decodeFromString(data.decodeToString()), eventId)
201- internal fun Event.toEventEntity (eventId : UUID , previousId : UUID ? , commandId : UUID ? = null) = EventEntity (
221+ internal fun EventEntity.toEventWithIdAndMetaData () =
222+ Triple <Event , UUID , UUID ?>(Json .decodeFromString(data.decodeToString()), eventId, commandId)
223+
224+ internal fun Event.toEventEntity (eventId : UUID , previousId : UUID ? , commandId : UUID ) = EventEntity (
202225 decider(),
203226 deciderId(),
204227 event(),
0 commit comments