Reactive Couchbase repository

Introduction

本章描述了 Couchbase 的响应式存储库支持。这是对在 Couchbase repositories 中解释的核心存储库支持的构建。因此,请确保你透彻地理解该部分解释的基本概念。

This chapter describes the reactive repository support for couchbase. This builds on the core repository support explained in Couchbase repositories. So make sure you’ve got a sound understanding of the basic concepts explained there.

Reactive Composition Libraries

Couchbase Java SDK 3.x 已从 RxJava 转移到 Reactor,因此它与响应式的 Spring 生态系统非常相容。

The Couchbase Java SDK 3.x moved from RxJava to Reactor, so it blends in very nicely with the reactive spring ecosystem.

响应式的 Couchbase 存储库提供 project Reactor 封装类型,并且可以通过简单地从其中一个特定库的存储库接口进行扩展来使用:

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

Usage

让我们创建一个简单的实体以开始:

Let’s create a simple entity to start with:

Example 1. Sample Person entity
public class Person {

  @Id
  private String id;
  private String firstname;
  private String lastname;
  private Address address;

  // … getters and setters omitted
}

相应的存储库实现可能如下所示:

A corresponding repository implementation may look like this:

Example 2. Basic repository interface to persist Person entities
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);
}

对于 JavaConfig,请使用 @EnableReactiveCouchbaseRepositories 注释。该注释包含与命名空间元素完全相同的属性。如果未配置基本包,则该基础架构将扫描带有注释的配置类的包。

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.

另外要注意的是,如果你在 Spring Boot 设置中使用它,你可能会省略该注释,因为它会为你自动配置。

Also note that if you are using it in a spring boot setup you likely can omit the annotation since it is autoconfigured for you.

Example 3. JavaConfig for repositories
@Configuration
@EnableReactiveCouchbaseRepositories
class ApplicationConfig extends AbstractCouchbaseConfiguration {
	// ... (see configuration for details)
}

由于我们的域域存储库扩展了 ReactiveSortingRepository,因此它为你提供了 CRUD 操作以及用于对实体进行分类访问的方法。使用存储库实例只是将其通过依赖性注入到客户端的问题。

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.

Example 4. Sorted access to Person entities
public class PersonRepositoryTests {

    @Autowired
    ReactivePersonRepository repository;

    @Test
    public void sortsElementsCorrectly() {
      Flux<Person> persons = repository.findAll(Sort.by(new Order(ASC, "lastname")));
      assertNotNull(perons);
    }
}

Repositories and Querying

Spring Data 的 Reactive Couchbase 附带了由阻塞 `` 完全提供的查询支持。

Spring Data’s Reactive Couchbase comes with full querying support already provided by the blocking Repositories and Querying