aboutsummaryrefslogtreecommitdiff
path: root/server/src/main/scala/Repository.scala
blob: 003ac92e30d03a2d3b3cea0428342e1f1456aa33 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package triad

import java.time.Instant

import slick.jdbc.{JdbcProfile, SQLiteProfile}

class Repository(val profile: JdbcProfile, url: String, driver: String) {
  val database: profile.backend.DatabaseDef =
    profile.api.Database.forURL(url, driver)

  import profile.api._

  implicit val instantColumnType = MappedColumnType.base[Instant, Long](
    { i =>
      i.toEpochMilli()
    }, { l =>
      Instant.ofEpochMilli(l)
    }
  )

  class Messages(tag: Tag) extends Table[Message](tag, "messages") {
    def id = column[String]("id")
    def content = column[String]("content")
    def author = column[String]("author")
    def timestamp = column[Instant]("timestamp")
    def * =
      (id, content, author, timestamp) <> ({ cols =>
        Message(cols._2, cols._3, cols._4)
      }, { message: Message =>
        Some((message.id, message.content, message.author, message.timestamp))
      })
    def pk = primaryKey("pk", id)
  }

  val Messages = TableQuery[Messages]

  def initAction = DBIO.seq(
    Messages.schema.create,
    Messages += Message("first!", "John Smith")
  )

}

object Repository {

  def sqlite(name: String) =
    new Repository(SQLiteProfile, s"jdbc:sqlite:$name", "org.sqlite.JDBC")

}