todokr / domala

Domain Oriented Database MApping framework for scaLA

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Domala: Doma for Scala

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

Maven Central

  • 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.

Example

Config

object SampleConfig extends Config(
  dataSource = new LocalTransactionDataSource(
    "jdbc:h2:mem:sample;DB_CLOSE_DELAY=-1", "sa", null),
  dialect = new H2Dialect,
  naming = Naming.SNAKE_LOWER_CASE
) {
  Class.forName("org.h2.Driver")
}

Entity

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

Holder

@Holder
case class Name(value: String)

Embeddable

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

Dao

@Dao(config = SampleConfig)
trait PersonDao {

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

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

  @Update
  def update(person: Person): Result[Person]
}

Usage

implicit val config = SampleConfig

// 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 {
  dao.insert(entity)
  dao.selectById(1).foreach(e =>
    dao.update(e.copy(age = e.age.map(_ + 1)))
  )
}

Run sample

sbt
>sample/run

Play integretion sample

domala-play-sample

License

Apache License, Version 2.0

About

Domain Oriented Database MApping framework for scaLA

License:Apache License 2.0


Languages

Language:Scala 96.6%Language:Java 3.4%