blob: 255507efd08c705dc6edcc3480c687487e352d1f (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package xyz.driver.core
import slick.backend.DatabaseConfig
import slick.dbio.{DBIOAction, NoStream}
import slick.driver.JdbcProfile
import xyz.driver.core.time.Time
import scala.concurrent.Future
object database {
trait Database {
val profile: JdbcProfile
val database: JdbcProfile#Backend#Database
}
object Database {
def fromConfig(databaseName: String): Database = {
val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig(databaseName)
new Database {
val profile: JdbcProfile = dbConfig.driver
val database: JdbcProfile#Backend#Database = dbConfig.db
}
}
}
type Schema = {
def create: DBIOAction[Unit, NoStream, slick.dbio.Effect.Schema]
def drop: DBIOAction[Unit, NoStream, slick.dbio.Effect.Schema]
}
trait IdColumnTypes {
val database: Database
import database.profile.api._
implicit def idColumnType[T] =
MappedColumnType.base[Id[T], Long](id => id: Long, Id[T](_))
implicit def nameColumnType[T] =
MappedColumnType.base[Name[T], String](name => name: String, Name[T](_))
implicit lazy val timeColumnType = MappedColumnType.base[Time, Long](time => time.millis, Time(_))
}
trait DatabaseObject extends IdColumnTypes {
// implicit val exec: ExecutionContext
def createTables(): Future[Unit]
def disconnect(): Unit
// def ensureTableExist(schemas: Seq[Schema]): Future[Unit] =
// for {
// dropping <- Future.sequence(schemas.map { schema =>
// database.database.run(schema.drop).recover { case _: Throwable => () }
// })
// creation <- Future.sequence(schemas.map { schema =>
// database.database.run(schema.create).recover { case _: Throwable => () }
// })
// } yield ()
}
abstract class DatabaseObjectAdapter extends DatabaseObject {
def createTables(): Future[Unit] = Future.successful(())
def disconnect(): Unit = {}
}
}
|