Skip to content
Samy Dindane edited this page Feb 1, 2014 · 12 revisions

The code below will produce all the slick boilerplate code including the table object, the foreign keys, the indexes, the custom type mappings, the association table for many to many relationships, the Enumeration Type Mapper and embed the "parts" into the enclosing table object (useful for tables with more than 22 columns).

@Model object XDb {
  object UserRights extends Enumeration {
    type UserRights = Value
    val ADMIN = Value(1)
    val GUEST = Value(2)
  }
  import UserRights._

  case class Company(name: String, website: String)

  @Part case class Address(num: Int, @Type("varchar(1024)") road: String, zip: String)

  case class Member(@Index(true) login: String, 
                                  rights: UserRights, 
                                  addr: Address, 
                                  company: Company, 
                                  manager: Option[Member])

  case class Project(name: String, company: Company, members: List[Member])
}

It is also possible to create the Slick mapping through a (still in its infancy) internal DSL:

@Model object XDb extends Timestamps {
  object UserRights extends Enumeration {
    type UserRights = Value
    val ADMIN = Value(1)
    val GUEST = Value(2)
  }
  import UserRights._

  case class Company(name: String, website: String) extends Timestamps
  case class Address(num: Int, road: String, zip: String) extends Part
  case class Member(login: String, rights: UserRights, addr: Address, company: Company, manager: Option[Member]) {
    constraints {
      login is unique withType "varchar(100)"
      manager onDelete Cascade
    }
  }
  case class Project(name: String, company: Company, members: List[Member])
}

A sample app looks like this :

implicit val dbConnectionInfo = DbConnectionInfos(url = "jdbc:postgresql:SampleApp", 
                                                  user = "postgres", password = "e-z12B24", 
                                                  driverClassName = "org.postgresql.Driver")

object CompanyDao extends CompanyCrud(companyQuery)
@DBTransaction def queryDB() {
    CompanyDao.insert(Company(None, "typesafe", "http://www.typesafe.com"))
    val query = companyQuery.where(_.id === 1L)
    query.doUpdate(name = "TheName2", website = "TheSite2")
}
Clone this wiki locally