longevity

A Persistence Framework for Scala and NoSQL

View project on GitHub

query filters

The main part of your query is going to be the query filter, commonly known as the “where clause”. We saw a simple example of a query filter in the previous section:

BlogPost.props.blogUri eqs blog.blogUri

This is a relational filter, which is the basic building block of most query filters. It consists of three elements: a property, a relational operator, and a value that matches the type of the property. The property typically belongs to the persistent type being queried, but if you are using polymorphic persistents, you can use properties from the PolyPType as well.

The relational operators are:

  • eqs
  • neq
  • lt
  • lte
  • gt
  • gte

Relational filters can be combined into conditional filters with conditional operators and and or. For instance, looking up all the blog posts for a blog published in the last week:

import longevity.persistence.PState
import scala.concurrent.Future

val blog: Blog = getBlogFromSomewhere()

val recentPosts: Future[Seq[PState[BlogPost]]] = blogPostRepo.retrieveByQuery {
  import com.github.nscala_time.time.Imports._
  import BlogPost.queryDsl._
  import BlogPost.props._
  blogUri eqs blog.blogUri and postDate gt DateTime.now - 1.week
}

If you want to retrieve every persistent in your collection, you can use the special query filter filterAll:

blogPostRepo.retrieveByQuery(BlogPost.queryDsl.filterAll)

Keys and indexes will aid query performance in an intuitive manner. For finer details on just how your query will run, please see the chapter on how your domain model is translated to your NoSQL backend.

prev: using the query dsl
up: queries
next: ordered queries