How to use gorm couchdb

coryhacking edited this page Sep 13, 2010 · 13 revisions
Clone this wiki locally

Install

To install gorm-couchdb you just need to do

grails install-plugin gorm-couchdb 

and grails will pull it from the repository.

Configuration

To configure your application to use gorm-couchdb you can go into your Datasource.groovy file and this:

couchdb {
    host = "localhost"
    port = 5984
    database = "mydb"
    username = ""
    password = ""
}

Host, port, username, and password are all optional at this point, only configure those if your db is somewhere else. You can override the database in the individual development, test, or production sections for specific databases you’ve setup for those environments.

If you’re also using Hibernate, you should also set the configClass in your dataSource to look something like this:

import grails.plugins.couchdb.cfg.CouchDomainConfiguration

dataSource {
configClass = CouchDomainConfiguration.class

}

use CouchAnnotationConfiguration.class if you’re using Hibernate annotations.

Once you’ve finished configuring gorm-couchdb you are ready to go!

Using

First you will need to mark your objects as CouchDB objects. To do this you will need to include the following annotation at the top of your class:

grails.plugins.couchdb.CouchEntity

This annotation can also take a “type” parameter, we’ve defined this so that you can easily define what kind of document you are storing in CouchDB, by default it uses the class name (lower-cased) as the type name. But you can configure this by doing this instead
@CouchEntity(type="project-task")

Where “project-task” is the type you would like to use to differentiate this in your CouchDB database

To turn the “type” column off altogether simply declare an empty value for type.

@CouchEntity(type="")

Optional

By default gorm-couchdb injects the id and version properties into your class, but if you should want to use a custom mapping for these you can use the following annotations:

@Id
String taskId

@Version
String taskVersion

CouchDB Views

It is possible to store your views in your grails project for pushing into CouchDB. You just have to set up a directory structure with all your design documents and views in them, like this:

grails-app/
   conf/
      couchdb/
         foo/
            byName.map.js
            byName.reduce.js

To enable this just add a view (or reduce) into grails-app/conf/coucdb and gorm-couchdb will push this view into your database whenever it is changed.

Finders

We haven’t enabled dynamic finders yet. So, you need to specify the design document and view name. The implemented finders include:

queryView(String viewName, Map options)
queryViewAndKeys(String viewName, List keys, Map options)