I'm trying to wrap my head around reactive programming, specifically with Spring and Spring Data R2DBC. One thing that would help me understand it better is to do a find/modify/save or "upsert" of an object. A traditional interaction might look like this:
Book save(Book book) {
Book existing = repository.findByIsbn(book.getIsbn())
if (existing != null) {
return repository.save(found.copyMutableValuesFrom(book));
}
return repository.save(book);
}
How might this look with Monos? I understand how to do a straight find, or a straight save, but a more complicated find/update/save or upsert is eluding me.
Thanks.
It will be more or less like this for your requirement.
@Transactional
Mono<Book> save(Book book){
return repository.findByIsbn(book.getIsbn())
.flatMap(found -> repository.save(found.copyMutableValuesFrom(book)))
.switchIfEmpty(repository.save(book));
}
Mono<Book>
when you call findById(something like Optional<Book>
- if it is your custom method, make it return Mono<Book>
)It returns a Mono<Book>