Uploaded image for project: 'Spring Data Commons'
  1. Spring Data Commons
  2. DATACMNS-1508

Add support for Coroutines repositories




      As a complement of Coroutines extensions, for the sake of parity with the Reactor and RxJava support and because Coroutines allow to leverage Reactive infrastructure in a more imperative way, and for consistency with Spring WebFlux which provides both functional and annotation variants, I think it would be great if Spring Data could support Coroutines repositories.

      With he recently released Flow type, it is now possible to provide such API that I have drafted:

      public interface CoCrudRepository<T, ID> : Repository<T, ID> {
      	suspend fun save(entity: T): T
      	fun saveAll(entities: Iterable<T>): Flow<T>
      	fun saveAll(entityStream: Flow<T>): Flow<T>
      	suspend fun findById(id: ID): T?
      	suspend fun findById(id: Flow<ID>): T?
      	suspend fun existsById(id: ID): Boolean
      	suspend fun existsById(id: Flow<ID>): Boolean
      	fun findAll(): Flow<T>
      	fun findAllById(ids: Iterable<ID> ): Flow<T>
      	fun findAllById(idStream: Flow<ID>): Flow<T>
      	suspend fun count(): Long
      	suspend fun deleteById(id: ID)
      	suspend fun deleteById(id: Flow<ID>)
      	suspend fun delete(entity: T)
      	suspend fun deleteAll(entities: Iterable<out T>)
      	suspend fun deleteAll(entityStream: Flow<out T>)
      	suspend fun deleteAll()

      Notice that Spring Framework 5.2 ReactiveAdapterRegistry supports Flow <-> Publisher adaptation, that MethodParameter is now Coroutines compliant, and that Spring Framework is able to adapt suspending functions to Mono even if this is not exposed yet (we could expose it for Spring Data needs).

      Based on what we did on Framework side, it seems possible to implement Coroutines repositories like RxJava ones and consider it as an alternative API that we adapt to leverage our Reactive infrastructure.

      Such API would be very popular I think.


          Issue Links



              mp911de Mark Paluch
              sdeleuze Sébastien Deleuze
              Last updater:
              Spring Issues Spring Issues
              4 Vote for this issue
              5 Start watching this issue