6/24/2012

Scalagen - A Source Code Generator for ORMs

Scalagen is a source code generator for ORMs. In the current version of Scalagen supports ScalaQuery and Anorm.

It's possible to use as a sbt plug-in. I show how to use Scalagen as a sbt plug-in for ScalaQuery. Scalagen generates table definition objects and case classes which correnspond to them.

In project/plugins.sbt, add:

resolvers += ("amateras snapshot" at "http://amateras.sourceforge.jp/mvn-snapshot/")

addSbtPlugin("jp.sf.amateras.scalagen" % "scalagen-sbtplugin" % "0.1-SNAPSHOT")

libraryDependencies ++= Seq(
  // for ScalaQuery
  "jp.sf.amateras.scalagen" %% "scalagen-scalaquery" % "0.1-SNAPSHOT",
  // for Anorm
  //"jp.sf.amateras.scalagen" %% "scalagen-anorm" % "0.1-SNAPSHOT",
  // JDBC driver for your database
  "org.hsqldb" % "hsqldb" % "2.2.8"
)

In build.sbt, add:

seq(jp.sf.amateras.scalagen.ScalagenPlugin.scalagenSettings: _*)

scalagenConfiguration := jp.sf.amateras.scalagen.Settings(
  // for ScalaQuery
  generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(),
  // for Anorm
  //generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(),
  driver = "org.hsqldb.jdbcDriver",
  url = "jdbc:hsqldb:hsql://localhost/",
  username = "sa",
  password = "",
  catalog = null,
  schemaPattern = null,
  tablePattern = null
)

Execute sbt scalagen, source files are generated into src/main/scala/models.

See more details about Scalagen at the following URL:
https://github.com/takezoe/scalagen