TouchDB Ektorp Views

davinic edited this page Mar 5, 2013 · 8 revisions

It may not be obvious at first how you can use TouchDB Views (implemented in Java) with the standard Ektorp API. Below is small example of how it can be done.

  1. First set up your TDServer as described in the page Getting Started

  2. Next, define a view with design document name and view name you want.

    String dDocName = "ddoc";
    String viewName = "people";
    TDView view = db.getViewNamed(String.format("%s/%s", dDocName, viewName));
  1. Next, add the map/reduce implementation blocks for this view.
    view.setMapReduceBlocks(new TDViewMapBlock() {

        public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
            String type = (String)document.get("type");
            if("person".equals(type)) {
                emitter.emit(null, document.get("_id"));
    }, new TDViewReduceBlock() {
            public Object reduce(List<Object> keys, List<Object> values, boolean rereduce) {
                    return null;
    }, "1.0");
  1. Now you can query this view using the normal Ektorp API:
    ViewQuery viewQuery = new ViewQuery().designDocId("_design/" + dDocName).viewName(viewName);
    //viewQuery.descending(true); //use this to reverse the sorting order of the view
    ViewResult viewResult = couchDbConnector.queryView(viewQuery);
  1. If you follow these steps, custom CouchDbDocument subclasses and the repository support classes should also work.

CouchDbRepositorySupport Example

Currently TOuchDB-Android supports limited functionality when using Ektorp Views, according to a message posted by Marty Schoch:

CouchDbRepositorySupport - You can use a subset of this functionality. (I haven't used it, but we have some successful reports of it working) The subset of functionality that does not work yet revolves around the designDocumentFacotry. This is what Ektorp uses to automatically generate map/reduce views to access the data. We'd like to add support for this in the future. Here is a related issue we're tracking

Here are some steps to use the CouchDbRepositorySupport class described by David K Pham:

  1. Properly describe my model so Ektorp knows how to map rows to it. Further explanation can be found here
  2. Setup an all view for the model in the database early on in the application lifecycle before you try to retrieve all the documents for that model:
TDView allView = db.getViewNamed("EmailAccount/all");
allView.setMapReduceBlocks(new TDViewMapBlock() {
	public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
		String type = document.get("type");

                if ("email_account".equals(type)) {
                        emitter.emit(null, document.get("_id"));	
}, null, "1.0");
  1. Create a CouchDbRepositorySupport class:
public class EmailAccountRepository extends CouchDbRepositorySupport<EmailAccount> {
        public EmailAccountRepository(CouchDbConnector couchDBConnector) {
                super(EmailAccount.class, couchDBConnector);
  1. Now you can retrieve all the documents for that model:
EmailAccountRepository emailAccountRepository = new EmailAccountRepository(CouchDBManager.getCouchDBConnector());

List<EmailAccount> emailAccounts = emailAccountRepository.getAll();

Caution: Any functionality of the CouchDbRepositorySupport class that relies on the design document factory to generate design documents at runtime is not supported.

@Views annotation

Currently this is not supported.