aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Example.scala
blob: 1e419a0aa9c1cebeadba5ac9438b70b5de20a9f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
object Tables extends {
  // or just use object demo.Tables, which is hard-wired to the driver stated during generation
  val profile = slick.driver.H2Driver
} with demo.Tables


import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.postfixOps

import Tables._
import Tables.profile.api._
import scala.concurrent.ExecutionContext.Implicits.global


object Example extends App {
  // connection info for a pre-populated throw-away, in-memory db for this demo, which is freshly initialized on every run
  val url = "jdbc:h2:mem:test;INIT=runscript from 'src/main/sql/create.sql'"
  val db = Database.forURL(url, driver = "org.h2.Driver")

  // Using generated code. Our Build.sbt makes sure they are generated before compilation.
  val q = Companies.join(Computers).on(_.id === _.manufacturerId)
    .map { case (co, cp) => (co.name, cp.name) }

  Await.result(db.run(q.result).map { result =>
    println(result.groupBy { case (co, cp) => co }
      .mapValues(_.map { case (co, cp) => cp })
      .mkString("\n")
    )
  }, 60 seconds)
}