Using MongoDB in Scala with Salat


In our last post, we had seen how easily we could access MongoDB with the Casbah driver. In this post, we would have a sneak peak at Salat.

Salat is a bidirectional, serialization library which helps us work with MongoDB’s DBObject. To quicly get started with Salat, you would need to include the following dependency in your Build.scala We would also need the dependency for the Casbah driver. The Build.scala would look like

object ApplicationBuild extends Build {

    val appName         = "MyCoolPlay"
    val appVersion      = "1.0"

    val appDependencies = Seq(
      "com.mongodb.casbah" %% "casbah" % "2.1.5-1",
      "com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
      // Add your project dependencies here,
    )

    val main = PlayProject(appName, appVersion, appDependencies).settings(defaultScalaSettings:_*).settings(
      // Add your own project settings here      
    )
}

Salat works with case classes. Let us define our model as something like this

case class Stream(@Key("_id") id: Int, name: String, streamType: String, creator: String)

Now, we create our DAO which extends the SalatDAO. The SalatDAO is a helper wrapper which eradicates the need to have a lot of boiler plate code. It returns a MongoCursor that extends Iterable typed to our case class. We can limit, skip, sort and then call toList on our results.

For our case, we define the StreamDAO as

object StreamDAO extends SalatDAO[Stream, Int](collection = MongoConnection()("test")("stream"))

Now, let us write a test to see that we are able to access the database using the DAO

import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import com.sun.org.apache.xalan.internal.xsltc.compiler.ForEach
import org.scalatest.BeforeAndAfter
import com.mongodb.casbah.commons.MongoDBObject

@RunWith(classOf[JUnitRunner])
class SmallStreamTest extends FunSuite with BeforeAndAfter {

  val stream1 = Stream(100, "al1pha", "class", "vikas")

  before {
    StreamDAO.insert(stream1)
  }

  test("Insert and retrieve a stream") {

    val streams = StreamDAO.find(MongoDBObject("name" -> ".*".r))
    assert(streams.size === 1)

  }
  after {
    StreamDAO.remove(stream1)
  }
}

As you can see, we can insert our object using StreamDAO.insert(stream1) and retrieve it using one of the many convenience methods provided by StreamDAO i.e StreamDAO.find(MongoDBObject(“name” -> “.*”.r))

Here we are using a regular expression “.*” to match all the names.

In our next post, we will look at accessing this functionality via Play framework so that we can display it using a Play controller.

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. Knoldus does niche Reactive and Big Data product development on Scala, Spark and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s