|
| 1 | +[[couchbase.collections]] |
| 2 | += Collection Support |
| 3 | + |
| 4 | +Couchbase supports https://docs.couchbase.com/server/current/learn/data/scopes-and-collections.html[Scopes and Collections]. This section documents on how to use it with Spring Data Couchbase. |
| 5 | + |
| 6 | +The https://github.com/couchbaselabs/try-cb-spring[try-cb-spring] sample application is a working example of using Scopes and Collections in Spring Data Couchbase. |
| 7 | + |
| 8 | +The 2021 Couchbase Connect presentation on Collections in Spring Data can be found at https://www.youtube.com/watch?v=MrplTeEFItk[Presentation Only] and https://web.cvent.com/hub/events/1dce8283-986d-4de9-8368-94c98f60df01/sessions/9ee89a85-833c-4e0c-81b0-807864fa351b?goBackHref=%2Fevents%2F1dce8283-986d-4de9-8368-94c98f60df01%2Fsessions&goBackName=Add%2FView+Sessions&goBackTab=all[Presentation with Slide Deck] |
| 9 | + |
| 10 | +== Requirements |
| 11 | + |
| 12 | + - Couchbase Server 7.0 or above. |
| 13 | + - Spring Data Couchbase 4.3.1 or above. |
| 14 | + |
| 15 | +== Getting Started & Configuration |
| 16 | + |
| 17 | + |
| 18 | +=== Scope and Collection Specification |
| 19 | +There are several mechanisms of specifying scopes and collections, and these may be combined, or one mechanism may override another. |
| 20 | +First some definitions for scopes and collections. An unspecified scope indicates that the default scope is to be used, likewise, an |
| 21 | +unspecified collection indicates that the default collection is to be used. |
| 22 | +There are only three combinations of scopes and collections that are valid. (1) the default scope and the default collection; (2) the default |
| 23 | +scope and a non-default collection; and (3) a non-default scope and a non-default collection. It is not possible to have a non-default |
| 24 | +scope and a default collection as non-default scopes do not contain a default collections, neither can one be created. |
| 25 | + |
| 26 | +A scope can be specified in the configuration: |
| 27 | +[source,java] |
| 28 | +---- |
| 29 | +@Configuration |
| 30 | +static class Config extends AbstractCouchbaseConfiguration { |
| 31 | +
|
| 32 | + // Usual Setup |
| 33 | + @Override public String getConnectionString() { /* ... */ } |
| 34 | +
|
| 35 | + // optionally specify the scope in the Configuration |
| 36 | + @Override |
| 37 | + protected String getScopeName() { |
| 38 | + return "myScope"; // or a variable etc.; |
| 39 | + } |
| 40 | +
|
| 41 | +} |
| 42 | +---- |
| 43 | +Scopes and Collections can be specified as annotations on entity classes and repositories: |
| 44 | +[source,java] |
| 45 | +---- |
| 46 | +@Document |
| 47 | +@Scope("travel") |
| 48 | +@Collection("airport") |
| 49 | +public class Airport {... |
| 50 | +---- |
| 51 | + |
| 52 | +[source,java] |
| 53 | +---- |
| 54 | +@Scope("travel") |
| 55 | +@Collection("airport") |
| 56 | +public interface AirportRepository extends CouchbaseRepository<Airport, String> ... |
| 57 | +---- |
| 58 | + |
| 59 | +Scopes and Collections can be specified on templates using the inScope(scopeName) and inCollection(collectionName) fluent APIs: |
| 60 | +[source,java] |
| 61 | +---- |
| 62 | +List<Airport> airports = template.findByQuery(Airport.class).inScope("archived").all() |
| 63 | +---- |
| 64 | + |
| 65 | +Scopes and Collections can be specified on repositories that extend DynamicProxyable using the withScope(scopeName) and withCollection(collectionName) APIs: |
| 66 | +[source,java] |
| 67 | +---- |
| 68 | +public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository>{...} |
| 69 | +... |
| 70 | +List<Airport> airports = airportRepository.withScope("archived").findByName(iata); |
| 71 | +---- |
| 72 | + |
| 73 | +.The order of precedence is: |
| 74 | +. inScope()/inCollection() of the template fluent api |
| 75 | +. withScope()/withCollection() of the template/repository object |
| 76 | +. annotation of the repository method |
| 77 | +. annotation of the repository interface |
| 78 | +. annotation of the entity object |
| 79 | +. getScope() of the configuration |
| 80 | + |
0 commit comments