Last active
June 5, 2019 07:26
-
-
Save peter-stratton/264ee7a4367d466e473efe35b93e97ca to your computer and use it in GitHub Desktop.
Slick and Postgresql Ammonite Session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load.ivy("org.postgresql" % "postgresql" % "9.4-1206-jdbc4") | |
load.ivy("com.typesafe.slick" %% "slick" % "3.1.1") | |
load.ivy("org.slf4j" % "slf4j-nop" % "1.6.4") | |
import slick.driver.PostgresDriver | |
import slick.driver.PostgresDriver.api._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import slick.jdbc.GetResult._ | |
import scala.concurrent.Future | |
import scala.concurrent.Await | |
import scala.concurrent.duration.Duration | |
val jdbcUrl = "jdbc:postgresql://localhost/mydatabase?user=username&password=userpass" | |
val jdbcDriverClass = "org.postgresql.Driver" | |
val db = Database.forURL(jdbcUrl, driver = jdbcDriverClass) | |
val createFoo: DBIO[Int] = sqlu"create table foo(name varchar)" | |
val dropFoo: DBIO[Int] = sqlu"drop table foo" | |
def insertFoo(name: String): DBIO[Int] = sqlu"insert into foo values ($name)" | |
val selectFoo: DBIO[Seq[String]] = sql"select name from foo".as[String] | |
Await.result(db.run(createFoo), Duration(5, "seconds")) | |
Await.result(db.run(insertFoo("air")), Duration(5, "seconds")) | |
Await.result(db.run(insertFoo("fire")), Duration(5, "seconds")) | |
Await.result(db.run(insertFoo("earth")), Duration(5, "seconds")) | |
Await.result(db.run(insertFoo("water")), Duration(5, "seconds")) | |
Await.result(db.run(selectFoo map {xs => xs foreach println }), Duration(5, "seconds")) | |
Await.result(db.run(dropFoo), Duration(5, "seconds")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment