aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/com/drivergrp/core/database.scala
blob: 5eb9d28c3a01d0ca795e76f432c16029f2b03788 (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
package com.drivergrp.core

import com.drivergrp.core.id.{Id, Name}

import scala.concurrent.Future


object database {

  import slick.backend.DatabaseConfig
  import slick.driver.JdbcProfile


  trait DatabaseModule {
    val profile: JdbcProfile
    val database: JdbcProfile#Backend#Database
  }

  trait ConfigDatabaseModule extends DatabaseModule {

    protected def databaseConfigKey: String

    private val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig(databaseConfigKey)

    val profile: JdbcProfile = dbConfig.driver
    val database: JdbcProfile#Backend#Database = dbConfig.db
  }

  trait DatabaseObject {
    def createTables(): Future[Unit]
    def disconnect(): Unit
  }

  trait IdColumnTypes {
    this: DatabaseModule =>

    import profile.api._

    implicit def idColumnType[T] =
      MappedColumnType.base[Id[T], Long]({ id => id: Long }, { id => Id[T](id) })

    implicit def nameColumnType[T] =
      MappedColumnType.base[Name[T], String]({ name => name: String }, { name => Name[T](name) })
  }
}