Search code examples
javamavencouchdbcloudant

Cloudant API create database if not exists


I'm using Cloudant API to connect with CouchDB and this is the code to crate a partitioned DB:

CloudantClient client = ClientBuilder.url(new URL("http://127.0.0.1:5984")).username("root").password("root").build();
        System.out.println("Server Version: " + client.serverVersion());
        client.createPartitionedDB("test");

To create a Database "if not exists" I found this command:

Database db = client.database("test2", true);

Now my question is: is there a function like the last I wrote for partitioned database?

I hope everyone understands because my English is not very good


Solution

  • I don't think such a method exists there currently, but it's not very difficult to implement a new one that does what you are expecting. See https://github.com/cloudant/java-cloudant/blob/5bf9de122dd7dd07bc2bd0ac94f21dc579337e7b/cloudant-client/src/main/java/com/cloudant/client/org/lightcouch/CouchDatabaseBase.java#L62 for the implementation of client.database("test2", true) you've mentioned. Also, as java-cloudant is open source, your contributions are welcome so please open a PR if you feel like improving the library.