Domain Oriented Database Mapping Framework for Scala
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Add example test to CI Dec 25, 2017
core/src Refactor AsyncAction Feb 22, 2018
example/src Remove domala.Embeddable annotation Jan 30, 2018
meta/src Port package private classes from doma Feb 14, 2018
notes Update a specification document Feb 5, 2018
paradise/src Port package private classes from doma Feb 14, 2018
project Update Doma v2.19.2 Jul 28, 2018
.gitignore first commit Aug 30, 2017
LICENSE Initial commit Aug 30, 2017 Update README Feb 5, 2018
build.sbt Update Doma v2.19.2 Jul 28, 2018

Domala: Doma for Scala

Domala is a database access library for Scala. This wraps Doma2.

Maven Central CircleCI

  • Domala uses scalameta to generate code and validate sql mappings at compile time.

  • Select statements are write by yourself. It is automatically mapped to Option[Entity], Seq[Entity], Stream[Entity], Seq[Map[String, Any]], and more.

  • Other statements are automatically generated from Entity. It can also write SQL.

Setup build

when use annotation macro

All functions are available under the setting below.

lazy val metaMacroSettings: Seq[Def.Setting[_]] = Seq(
  addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M10" cross CrossVersion.full),
  scalacOptions += "-Xplugin-require:macroparadise",
  scalacOptions in (Compile, console) ~= (_ filterNot (_ contains "paradise")) // macroparadise plugin doesn't work in repl yet.

lazy val yourProject = project.settings(
  // required to validate SQL files at compile time
  compile in Compile := ((compile in Compile) dependsOn (copyResources in Compile)).value,
  libraryDependencies ++= Seq(
    "com.github.domala" %% "domala" % "0.1.0-beta.9",
    "com.github.domala" %% "domala-paradise" % "0.1.0-beta.9" % Provided,
    "org.scalameta" %% "scalameta" % "1.8.0" % Provided,    
    // ... your other library dependencies
  // ... your other project settings

when not use annotation macro

@Dao, @Entity, @Holder can not be used under the setting below.

lazy val yourProject = project.settings(
  libraryDependencies ++= Seq(
    "com.github.domala" %% "domala" % "0.1.0-beta.9"
    // ... your other library dependencies
  // ... your other project settings



A value holder

case class Name(value: String)


Partial class to embed in entity

case class Address(city: String, street: String)


Classes whose instances can be stored in a database

@Entity can be omitted, but some functions are restricted.

case class Person(
  id: Int,
  name: Name,
  age: Option[Int],
  address: Address,
  departmentId: Option[Int],
  version: Option[Int] = None


Trait of data access object

trait PersonDao {

  @Select(sql = """
select *
from person
where id = /*id*/0
  def selectById(id: Int): Option[Person]

  def insert(person: Person): Result[Person]

  def update(person: Person): Result[Person]

SQL can also be described in an external file.

When use external file, the file name must be as follows, and put it on the class path.

META-INF/Dao package name/Dao class name/Dao method name.sql

package example
trait PersonDao {
  def selectByName(name: String): Seq[Person]

External SQL file for this method is as follows.


select *
from person where
/*%if name != null */
  name like /* @prefix(name) */'%' escape '$'


A configuration of a database

object ExampleConfig extends LocalTransactionConfig(
  dataSource = new LocalTransactionDataSource(
    "jdbc:h2:mem:example;DB_CLOSE_DELAY=-1", "sa", null),
  dialect = new H2Dialect,
  naming = Naming.SNAKE_LOWER_CASE
) {


implicit val config = ExampleConfig

// Dao implementation is auto generated.
val dao: PersonDao = PersonDao.impl 

val entity = Person(
  id = 1,
  name = Name("SMITH"),
  age = Some(10),
  address = Address("TOKYO", "YAESU"),
  departmentId = Some(1)

// `Required` is Executes the transaction.
// If processing in block ends normally it commit.
// If Exception occurs it rollback.
Required {
  dao.selectById(1).foreach(e =>
    dao.update(e.copy(age = + 1)))

Run example



Annotation macro doesn't work in repl yet. So, compile separately with sbt ~console or use the following SQL interpolations.

import domala._
// load jdbc driver

// A configuration of database
// domala.jdbc.AdHocConfig works with AutoCommit mode.
implicit val config = jdbc.AdHocConfig("jdbc:h2:mem:example;DB_CLOSE_DELAY=-1")

// `script"..." ` builds a script query statement
script"create table emp(id int serial primary key, name varchar(20))".execute()

// `update"..." ` builds a `INSERT`, `UPDATE`, `DELETE` query statement
Seq("Scott", "Allen").map { name =>
    insert into emp(name) values($name)

// `select"..." ` builds a `SELECT` query statement
select"select * from emp order by id".getMapList
// => List(Map("ID" -> 1, "NAME" -> "Scott"), Map("ID" -> 2, "NAME" -> "Allen"))

select"select id from emp".getList[Int]
// => List(1, 2)

case class ID[E] (value: Int) extends AnyVal
case class Emp(id: ID[Emp], name: String) 
select"select * from emp order by id".getList[Emp]
// => List(Emp(ID(1),Scott), Emp(ID(2),Allen))

// EntityManager is assemble and execute SQL automatically from a entity type
jdbc.EntityManager.insert(Emp(ID(3), "Smith"))

select"select * from emp order by id".getList[Emp]
// => List(Emp(ID(1),Scott), Emp(ID(2),Allen), Emp(ID(3),Smith))

Play integration sample



Apache License, Version 2.0