Skip to content

Commit 831fcaa

Browse files
committed
DATACOUCH-518 - Update basic template.
1 parent 64b838a commit 831fcaa

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

src/main/asciidoc/index.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ include::{spring-data-commons-docs}/repositories.adoc[]
2323
include::repository.adoc[]
2424
include::reactiverepository.adoc[]
2525
include::template.adoc[]
26-
include::ansijoins.adoc[]
26+
// (daschl) disabled the ansijoins docs since it is being overhauled for 4.x
27+
// include::ansijoins.adoc[]
2728
:leveloffset: -1
2829

2930
[[appendix]]

src/main/asciidoc/template.adoc

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,56 @@
22
= Template & direct operations
33

44
The template provides lower level access to the underlying database and also serves as the foundation for repositories.
5-
Any time a repository is too high-level for you needs chances are good that the templates will serve you well.
5+
Any time a repository is too high-level for you needs chances are good that the templates will serve you well. Note that
6+
you can always drop into the SDK directly through the beans exposed on the `AbstractCouchbaseConfiguration`.
67

78
[[template.ops]]
89
== Supported operations
910

10-
The template can be accessed through the `couchbaseTemplate` bean out of your context.
11+
The template can be accessed through the `couchbaseTemplate` and `reactiveCouchbaseTemplate` beans out of your context.
1112
Once you've got a reference to it, you can run all kinds of operations against it.
1213
Other than through a repository, in a template you need to always specify the target entity type which you want to get converted.
1314

14-
To mutate documents, you'll find `save`, `insert` and `update` methods exposed.
15-
Saving will insert or update the document, insert will fail if it has been created already and update only works against documents that have already been created.
15+
The templates use a fluent-style API which allows you to chain in optional operators as needed. As an example, here is
16+
how you store a user and then find it again by its ID:
1617

17-
Since Couchbase Server has different levels of persistence (by default you'll get a positive response if it has been acknowledged in the managed cache), you can provide higher durability options through the overloaded `PersistTo` and/or `ReplicateTo` options.
18-
The behaviour is part of the Couchbase Java SDK, please refer to the official documentation for more details.
19-
20-
Removing documents through the `remove` methods works exactly the same.
21-
22-
If you want to load documents, you can do that through the `findById` method, which is the fastest and if possible your tool of choice.
23-
The find methods for views are `findByView` which converts it into the target entity, but also `queryView` which exposes lower level semantics.
24-
Similarly, find methods using N1QL are provided in `findByN1QL` and `queryN1QL`.
25-
Additionally, since N1QL allows you to select specific fields in documents (or even across documents using joins), `findByN1QLProjection` will allow you to skip full `Document` conversion and map these fields to an ad-hoc class.
26-
27-
WARNING: If it is detected at runtime that the cluster doesn't support N1QL, these methods will throw a `UnsupportedCouchbaseFeatureException`.
28-
29-
If you really need low-level semantics, the `couchbaseBucket` is also always in scope through `getCouchbaseBucket()`.
30-
31-
[[couchbase.template.xml]]
32-
== Xml Configuration
33-
34-
The template can be configured via xml, including setting a custom `TranslationService`.
35-
36-
.XML Based Template Declaration
18+
.Fluent template access
3719
====
38-
[source,xml]
20+
[source,java]
3921
----
40-
<?xml version="1.0" encoding="UTF-8"?>
41-
<beans xmlns="http://www.springframework.org/schema/beans"
42-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
43-
xmlns:couchbase="http://www.springframework.org/schema/data/couchbase"
44-
xsi:schemaLocation="http://www.springframework.org/schema/data/couchbase https://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd
45-
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
22+
// Create an Entity
23+
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
4624
47-
<couchbase:env/>
48-
<couchbase:cluster/>
49-
<couchbase:clusterInfo/>
50-
<couchbase:bucket/>
25+
// Upsert it
26+
couchbaseTemplate.upsertById(User.class).one(user);
5127
52-
<couchbase:template translation-service-ref="myCustomTranslationService"/>
28+
// Retrieve it again
29+
User found = couchbaseTemplate.findById(User.class).one(user.getId());
30+
----
31+
====
5332

54-
<bean id="myCustomTranslationService" class="org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService"/>
33+
If you wanted to use a custom durability requirement for the `upsert` operation you can chain it in:
5534

56-
</beans>
35+
.Upsert with durability
36+
====
37+
[source,java]
38+
----
39+
User modified = couchbaseTemplate
40+
.upsertById(User.class)
41+
.withDurability(DurabilityLevel.MAJORITY)
42+
.one(user);
5743
----
5844
====
5945

60-
NOTE: In the example above most tags assume their default values, that is a localhost cluster and bucket "default".
61-
In production you would have to also provide specifics to these tags.
46+
In a similar fashion, you can perform a N1QL operation:
6247

48+
.N1QL query on the template
49+
====
50+
[source,java]
51+
----
52+
final List<User> foundUsers = couchbaseTemplate
53+
.findByQuery(User.class)
54+
.consistentWith(QueryScanConsistency.REQUEST_PLUS)
55+
.all();
56+
----
57+
====

0 commit comments

Comments
 (0)