Improve couchbase support

Expose an `auto-index` property that controls if views and indexes
should be created automatically.

Update the sample so that it uses this new property, lowering the manual
steps to make it working on a vanilla couchbase server.

See gh-3498
This commit is contained in:
Stephane Nicoll
2016-02-16 11:34:19 +01:00
parent ed04a5b12e
commit 64a5cad09a
10 changed files with 102 additions and 34 deletions

View File

@@ -2,13 +2,18 @@
This sample demonstrates how you can store a simple document using Spring Data Couchbase.
The sample expects couchbase to run on your machine with a bucket named `mybucket` and
`couchbase` for the password. You can customize these settings in `application.properties`.
The sample expects couchbase to run on your machine with a bucket named `default` and
no password. You can customize these settings in `application.properties`.
Before you use the sample, you need _at least_ to create an `all` view for the `User`: go
to the Couchbase servers admin console and visit the Views screen, then click `Create
Development View` and name it `all` with `user` as document name. On that view, you need
to change the code to:
This sample also configures the `auto-index` property and the `UserRepository` defines
the `all` view so you should be able to invoke `findAll` for this sample.
== Creating the view manually
If you don't want to rely on `auto-index` and better understand how this works behind the
scenes, you need to create an `all` view for the `User`. Go to the Couchbase servers
admin console and visit the Views screen, then click `Create Development View` and name
it `all` with `user` as document name. On that view, you need to change the code to:
```java
function (doc, meta) {

View File

@@ -35,18 +35,19 @@ public class SampleCouchbaseApplication implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
saveUsers();
this.userRepository.deleteAll();
User user = saveUser();
System.out.println(this.userRepository.findAll());
System.out.println(this.userRepository.findOne(user.getId()));
}
private void saveUsers() {
private User saveUser() {
User user = new User();
user.setId(UUID.randomUUID().toString());
user.setFirstName("Alice");
user.setLastName("Smith");
this.userRepository.save(user);
return this.userRepository.save(user);
}
}

View File

@@ -16,8 +16,10 @@
package sample.data.couchbase;
import org.springframework.data.couchbase.core.query.ViewIndexed;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
@ViewIndexed(designDoc = "user", viewName = "all")
public interface UserRepository extends CouchbaseRepository<User, String> {
}

View File

@@ -1,2 +1,2 @@
spring.data.couchbase.bucket.name=mybucket
spring.data.couchbase.bucket.password=couchbase
spring.data.couchbase.auto-index=true
spring.data.couchbase.bucket.name=default