Monday, August 13, 2012

MongoDB and Play2 at ease: using Play-Salat plugin and Embed Mongo plugin

A while ago I've blogged about MongoDB with Play2 using Salat, here.

This post was describing how to integrate Salat easily with Play2 and gave some advice on actions to care of.

Preface

Play 2 gained in popularity and an amazing plugin as emerge for this purpose: play2-salat.

This plugin offers a lot of configuration to hit running instances including replicasets! It integrates very well by applying the advice I talked about in my post, but not only. It defines binders to enable us using Casbah stuffs in our routing and action definition (ObjectId, and so on).

This post is not dedicated to explain how to use it, I'd recommend you to browse the project page (play2-salat), plus the wiki that points to relevant URLs.

Goal

This post is dedicated to developers teams that follows (or not...) the convention of Continuous Delivery, especially the Single Command Environment pattern. That is, the environment must be set up in one single command... in Play2 => play run OR sbt run

Context

Create an application that uses MongoDB as (one of its) persistence backend service, use play2-sala to have access to the `ORM` for our object and easy collections connections.
When runnning in production, of course, a MongoDB instance runs somewhere that can be configured (or a replicatset).

But in Dev?

Embed Instance

When another developer is cloning the related repo, knowing that it's a play application, he's best will would be to enter the directory and launch the application. > BANG <
No running instance...

So I created a Play2 plugin that uses this amazing work which retrieves a mongodb installer, installs it and enable us to launch/stop it... Keep in mind that MongoDB is not JVM based!

Adding this plugin to the application, setup the dev configuration to starts an embed MongoDB and Play2-Salat to target it, will gives the satisfaction to our developer... Moreover if he is a Designer (the only kind of guy that add values to any app ^^) who don't care about MongoDB, at all!

How To

Add the plugin dependencies (used in PlayProject):

     //MY OWN REPO where is deployed the following plugin
     val skalaCloudbeesSnapshots = "Ska La SNAPSHOTS" at "https://repository-andy-petrella.forge.cloudbees.com/snapshot/"

    //THE NEW PLUGIN => EMBED
    lazy val embedMongoPlayPlugin    = "be.nextlab" %% "play-plugins-embed-mongodb" % "0.0.1-SNAPSHOT" changing()

    //THE WORTH ONE
    lazy val salatPlayPlugin         = "se.radley" %% "play-plugins-salat" % "1.0.8"

    //DECLARE the deps
    val appDependencies = Seq(
        embedMongoPlayPlugin,
        salatPlayPlugin
    )

A bit of configuration (application-dev.conf)
embed.mongodb.start=true
embed.mongodb.version=V2_1_1
embed.mongodb.port=27017

mongodb.default.db=meinGot
mongodb.default.host=localhost
mongodb.default.port=27017

And the most only thing that requires a bit of explanation (in conf/play.plugins)
600:se.radley.plugin.salat.SalatPlugin
550:be.nextlab.play.mongodb.EmbedMongoDBPlugin
See? Yes, the Play2-Salat plugin MUST be started AFTER the embed plugin... of course (what an explanation huh).

Code

The one-single-file-of-33-lines plugin can be forked here.



That's All, Folks!