From 0ceee5ed4bae240b8c8e94d2fd7424d9d0b67ec7 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 9 Oct 2019 17:10:43 -0400 Subject: Migrate build to mill --- server/src/Repository.scala | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 server/src/Repository.scala (limited to 'server/src/Repository.scala') diff --git a/server/src/Repository.scala b/server/src/Repository.scala new file mode 100644 index 0000000..003ac92 --- /dev/null +++ b/server/src/Repository.scala @@ -0,0 +1,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") + +} -- cgit v1.2.3