Skip to content

Instantly share code, notes, and snippets.

@jpzk
Created July 3, 2019 15:55
Show Gist options
  • Save jpzk/cba53a702261ef888f16ce65123f3c95 to your computer and use it in GitHub Desktop.
Save jpzk/cba53a702261ef888f16ce65123f3c95 to your computer and use it in GitHub Desktop.
import slick.jdbc.PostgresProfile
import slick.jdbc.PostgresProfile.api._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
class ExtractorConfig(tag: Tag) extends Table[(String, Long, String)](tag, "configs") {
def name = column[String]("name", O.PrimaryKey)
def offset = column[Long]("offset")
def hosts = column[String]("endpoints")
// Every table needs a * projection with the same type as the table's type parameter
def * = (name, offset, hosts)
}
object DBOps {
val configs = TableQuery[ExtractorConfig]
type DB = PostgresProfile.backend.DatabaseDef
def createSchema(db: DB) =
db.run(DBIO.seq(configs.schema.create,))
def insertConfig(db: DB) =
db.run(configs ++= Seq(("bitcoin", 0, "http://localhost:8080")))
def projOffset(name: String) =
configs
.filter {
_.name == name
}
.map(_.offset)
def getOffset(db: DB, name: String) =
db.run(projOffset(name).result).map(_.head)
def setOffset(db: DB, name: String, offset: Long) =
db.run(projOffset(name).update(offset))
}
object Main extends App {
val username = "tokenanalyst"
val password = "blockchainload"
val db = Database.forDriver(new org.postgresql.Driver,
"jdbc:postgresql://localhost:5432/postgres",
"tokenanalyst",
"blockchainload"
)
Await.result(DBOps.createSchema(db), Duration.Inf)
Await.result(DBOps.insertConfig(db), Duration.Inf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment