Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow suspend + List in CoroutineCrudRepository #395

Closed
Tienisto opened this issue Jul 10, 2020 · 4 comments
Closed

Allow suspend + List in CoroutineCrudRepository #395

Tienisto opened this issue Jul 10, 2020 · 4 comments
Labels
type: enhancement A general enhancement

Comments

@Tienisto
Copy link

Tienisto commented Jul 10, 2020

Hello,

usually when I want a list, I use a function returning a Flow followed by toList() which is working perfectly.
Here is what I write now:

interface UserRepo : CoroutineCrudRepository<User, Long> {
    suspend fun findByEmail(email: String): User?
    fun findByFirstName(firstName: String): Flow<User>
}

// in use:
val users = userRepo.findByFirstName("Max").toList() // toList() is the build-in suspended function

What about including this into the repository itself? Then the code is much cleaner and because toList() is a suspended function, the thread is not blocked anyways.

interface UserRepo : CoroutineCrudRepository<User, Long> {
    suspend fun findByEmail(email: String): User?
    suspend fun findByFirstName(firstName: String): List<User>
}

Currently an exception is thrown when I try the 2nd example.

@mp911de
Copy link
Member

mp911de commented Jul 14, 2020

Sounds a good enhancement. We should be able to perform this change in Spring Data Commons entirely. Any objections @sdeleuze?

@mp911de mp911de added the for: external-project For an external project and not something we can fix label Jul 14, 2020
@sdeleuze
Copy link
Contributor

Sorry for the delay, supporting this kind of construct makes sense since it really depends on the use case for choosing between both and unlike Mono<List<User>>, the unwrapped version is pretty pleasant to use thanks to Coroutines. So +1 from my perspective.

@mp911de mp911de added type: enhancement A general enhancement and removed for: external-project For an external project and not something we can fix labels Sep 22, 2020
@mp911de mp911de added this to the 1.2 RC2 (2020.0.0) milestone Sep 22, 2020
@mp911de
Copy link
Member

mp911de commented Sep 22, 2020

Depends also on a change in Spring Data Commons (DATACMNS-1802).

@mp911de
Copy link
Member

mp911de commented Sep 22, 2020

That's in place now.

@mp911de mp911de closed this as completed Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants