CouchDB Custom Query Params in Lift

19 August 2010

One of the new features of Lift is the ability to use custom parameters when making a CouchDB query using the withParams method of Queryable. This allows you to do things like have your couch query output docs sorted by one field while limiting the output to docs that contain a specific value.

Let’s say you store all of your homework assignments in couch and you want your query to output all assignments sort by date. Your couch design might look like this:

function(doc) { 
	if (doc.type == 'Homework'){
		emit(doc.date, doc)
	};
}

If you want to limit the number of assignments that get returned you can query with the parameter “limit”:

var tempParam:Map[String, Any] = Map[String, Any]("limit"->JInt(3))
var viewReturn = Homework.queryView("HomeworkDbViews", "FindAllHomeworkDesign", _.withParams(tempParam))

This adds some nice flexibility to using CouchDB with Lift.

For more information on the querying options available with CouchDB check out the CouchDB wiki For more details check out the Queryable API

Note: In Lift 2.0 withParams is a protected method so you need to be using the 2.1 Snapshot

Fork me on GitHub