io.committed.spring.reactive.repositories.ReactiveRepositoryWrapper Maven / Gradle / Ivy
package io.committed.spring.reactive.repositories;
import org.reactivestreams.Publisher;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Converts a non reactive repository to a reactive repository.
*
* This does the wrap in a blocking way, in that that at the point of return the action has been
* executed.
*
* @param the type to save
* @param the id
* @param the repository
*/
public class ReactiveRepositoryWrapper>
implements ReactiveCrudRepository {
protected final R repo;
public ReactiveRepositoryWrapper(final R repo) {
this.repo = repo;
}
@Override
public Mono save(final S entity) {
return Mono.justOrEmpty(repo.save(entity));
}
@Override
public Flux saveAll(final Iterable entities) {
return Flux.fromIterable(repo.saveAll(entities));
}
@Override
public Flux saveAll(final Publisher entityStream) {
return saveAll(Flux.from(entityStream).toIterable());
}
@Override
public Mono findById(final I id) {
return Mono.justOrEmpty(repo.findById(id));
}
@Override
public Mono findById(final Publisher id) {
return findById(Mono.from(id).block());
}
@Override
public Mono existsById(final I id) {
return Mono.justOrEmpty(repo.existsById(id));
}
@Override
public Mono existsById(final Publisher id) {
return existsById(Mono.from(id).block());
}
@Override
public Flux findAll() {
return Flux.fromIterable(repo.findAll());
}
@Override
public Flux findAllById(final Iterable ids) {
return Flux.fromIterable(repo.findAllById(ids));
}
@Override
public Flux findAllById(final Publisher idStream) {
return findAllById(Flux.from(idStream).toIterable());
}
@Override
public Mono count() {
return Mono.just(repo.count());
}
@Override
public Mono deleteById(final I id) {
repo.deleteById(id);
return Mono.empty();
}
@Override
public Mono deleteById(final Publisher id) {
deleteById(Mono.from(id).block());
return Mono.empty();
}
@Override
public Mono delete(final T entity) {
repo.delete(entity);
return Mono.empty();
}
@Override
public Mono deleteAll(final Iterable entities) {
repo.deleteAll(entities);
return Mono.empty();
}
@Override
public Mono deleteAll(final Publisher entityStream) {
return deleteAll(Flux.from(entityStream).toIterable());
}
@Override
public Mono deleteAll() {
repo.deleteAll();
return Mono.empty();
}
}