viernes, 19 de julio de 2013

Run play framework application from interactive console [Scala Play Framework 2.1]

UPDATE 22/Aug/2013: Lunatech published a more detailed explanation in it's blog article.
It's well documented that we have a interactive console in the Play Framewok

From it you can run, compile, execute test... But a really nice feature is that you can get a console inside your application. That's it, create objects from your model, call methods... interactively. To do this you just need run the console command.


Start your application in the interactive console

If you need to execute database commands from the interactive console, you will need to start your application.

You can do this way:

1. - Launch the play console with applyEvolutions.enabled
play -DapplyEvolutions.default=true console

2.-. Enter the console
console

3.- Start the application
new play.core.StaticApplication(new java.io.File("."))

Now you can execute any command, even the database ones


Example: Get a list of users with Slick

1.- Do the imports you need. In slick you will need something like this:
import play.api.Play.current
import play.api.db.slick.Config.driver.simple._
import play.api.db.slick.DB
import models._

2.- Do the queries:
scala> val userborn = for { u <- Users if u.id < 2 } yield (u.username, u.born)
userborn: scala.slick.lifted.Query[(scala.slick.lifted.Column[String], ...
scala> userborn.selectStatement
res5: String = select x2."username", x2."born" from "user" x2 where x2."id" < 2 
scala> DB.withSession { implicit session  => userborn.list.mkString }
res6: String = (Admin Surname,2003-07-19) 

No hay comentarios:

Publicar un comentario