johanhaleby / occurrent

Unintrusive Event Sourcing Library for the JVM
https://occurrent.org
125 stars 16 forks source link

Add DominEventQueries "DSL" #92

Closed johanhaleby closed 2 years ago

johanhaleby commented 3 years ago

Small wrapper that makes use the CloudEventConverter:

class GameEventQueries(private val eventStoreQueries: EventStoreQueries, private val cloudEventConverter: CloudEventConverter<GameEvent>) {

    @Suppress("UNCHECKED_CAST")
    fun <T : GameEvent> query(filter: Filter): Sequence<T> =
        eventStoreQueries.queryForSequence(filter)
            .map(cloudEventConverter::toDomainEvent)
            .map { it as T }

    fun <T : GameEvent> queryOne(filter: Filter): T = query<T>(filter).first()
}