DATACOUCH-295 - Add documentation for reactive support
Original pull request: #145.
This commit is contained in:
118
src/main/asciidoc/reactiverepository.adoc
Normal file
118
src/main/asciidoc/reactiverepository.adoc
Normal file
@@ -0,0 +1,118 @@
|
||||
[[couchbase.reactiverepository]]
|
||||
= Reactive Couchbase repository
|
||||
|
||||
[[couchbase.reactiverepository.intro]]
|
||||
== Introduction
|
||||
|
||||
This chapter describes the reactive repository support for couchbase. This builds on the core repository support explained in <<couchbase.repository>>.
|
||||
So make sure you’ve got a sound understanding of the basic concepts explained there.
|
||||
|
||||
[[couchbase.reactiverepository.libraries]]
|
||||
== Reactive Composition Libraries
|
||||
|
||||
Couchbase Java SDK 2.x has taken a reactive programming approach since its inception using an early reactive extension to JVM, https://github.com/ReactiveX/RxJava/tree/1.x/[RxJava1].
|
||||
It provides RxJava1 observable sequence API to compose asynchronous database operations.
|
||||
|
||||
Reactive Couchbase repositories provide project Reactor wrapper types and can be used by simply extending from one of the library-specific repository interfaces:
|
||||
|
||||
* ReactiveCrudRepository
|
||||
|
||||
* ReactiveSortingRepository
|
||||
|
||||
Spring-data-couchbase converts RxJava 1 observables to reactor types by using reactive-streams adapters from https://github.com/ReactiveX/[RxJavaReactiveStreams]
|
||||
for convenience since these conversions can easily clutter application code. This transformation happens on same thread. It also provides direct access to RxJava1 observable
|
||||
sequence API from SDK through RxJavaCouchbaseOperations methods.
|
||||
|
||||
[[couchbase.reactiverepository.usage]]
|
||||
== Usage
|
||||
To access domain entities stored in a Couchbase bucket you can leverage our sophisticated repository support that eases implementing those quite significantly.
|
||||
To do so, simply create an interface for your repository:
|
||||
|
||||
.Sample Person entity
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private Address address;
|
||||
|
||||
// … getters and setters omitted
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
We have a quite simple domain object here.
|
||||
|
||||
.Basic repository interface to persist Person entities
|
||||
====
|
||||
[source]
|
||||
----
|
||||
public interface ReactivePersonRepository extends ReactiveSortingRepository<Person, Long> {
|
||||
|
||||
Flux<Person> findByFirstname(String firstname);
|
||||
|
||||
Flux<Person> findByFirstname(Publisher<String> firstname);
|
||||
|
||||
Flux<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable);
|
||||
|
||||
Mono<Person> findByFirstnameAndLastname(String firstname, String lastname);
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
For JavaConfig use the `@EnableReactiveCouchbaseRepositories` annotation. The annotation carries the very same attributes like the namespace element. If no base package is configured the infrastructure will scan the package of the annotated configuration class.
|
||||
|
||||
.JavaConfig for repositories
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
@EnableReactiveCouchbaseRepositories
|
||||
class ApplicationConfig extends AbstractReactiveCouchbaseConfiguration {
|
||||
|
||||
@Override
|
||||
protected List<String> getBootstrapHosts() {
|
||||
return Collections.singletonList("127.0.0.1");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return "default";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
As our domain repository extends `ReactiveSortingRepository` it provides you with CRUD operations as well as methods for sorted access to the entities. Working with the repository instance is just a matter of dependency injecting it into a client.
|
||||
|
||||
.Sorted access to Person entities
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
public class PersonRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
ReactivePersonRepository repository;
|
||||
|
||||
@Test
|
||||
public void sortsElementsCorrectly() {
|
||||
Flux<Person> persons = repository.findAll(Sort.by(new Order(ASC, "lastname")));
|
||||
assertNotNull(perons);
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
[[couchbase.reactiverepository.querying]]
|
||||
== Repositories and Querying
|
||||
|
||||
Spring Data's Reactive Couchbase comes with full querying support already provided by the blocking <<couchbase.repository.querying>>
|
||||
Reference in New Issue
Block a user