I’m new to Project Reactor and R2DBC. How to receive and merge Flux<Child>
with Mono<Parent>
properly using Spring Data R2DBC reactive repositories?
Parent:
JavaScript
x
@Table("parent")
public class Parent{
@Id
private Long id;
private String name;
@Transient
private Flux<Child> children;
ParentRepository:
JavaScript
interface ParentRepository extends ReactiveCrudRepository<Parent, Long> {
@Query()
Mono<Parent> findOneByName(String name);
Child:
JavaScript
@Table("child")
public class Child{
@Id
private Long id;
ChildRepository:
JavaScript
interface ChildRepository extends ReactiveCrudRepository<Child, Long> {
@Query()
Flux<Child> findAllByParentId(Long parentId);
ParentPersistenceAdapter:
JavaScript
public Mono<Parent> findParent(String parentName) {
return parentRepository.findOneByName(parentName)
//how to invoke childRepository.findAllByParentId()
//and set this Flux to Parent's Mono
}
My solution is:
JavaScript
public Mono<Parent> findParent(String parentName) {
return parentRepository.findOneByName(parentName)
.map(parent -> {
Flux<Child> children = childRepository.findAllByParentId(parent.getId())
children.subscribe();
return parent.setChildren(children );
});
}
Advertisement
Answer
Assuming the existence of a withChildren(Flux<Child> children)
type method, you can just do:
JavaScript
parentRepository.findById(parentId)
.map(p -> p.withChildren(childRepository.findAllByParentId(parentId)));
However, this is a bit odd – you wouldn’t usually have a Flux
on a DAO like that as you’d need to subscribe to it and manage the content separately. You’d more normally have a List<Child>
instead. For that situation, you can collect the child stream as a list, zip()
the corresponding Mono
publishers together, then combine them into your final Parent
object.
So assuming a withChildren(List<Child> children)
method:
JavaScript
Mono.zip(parentRepository.findById(parentId),
childRepository.findAllByParentId(parentId).collectList(),
(t1,t2) -> t1.withChildren(t2));