Skip to content

Instantly share code, notes, and snippets.

@tk3
Last active April 2, 2020 15:16
Show Gist options
  • Save tk3/5697c5e0a2706ceda6f193ce1dd61eef to your computer and use it in GitHub Desktop.
Save tk3/5697c5e0a2706ceda6f193ce1dd61eef to your computer and use it in GitHub Desktop.
Slick samples
name := "SlickSample"
version := "0.1"
scalaVersion := "2.13.1"
libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % "7.2.30",
"com.typesafe.slick" %% "slick" % "3.3.2",
"org.slf4j" % "slf4j-nop" % "1.7.25",
"com.h2database" % "h2" % "1.4.200"
)
import slick.driver.H2Driver.api._
import slick.lifted.{ProvenShape, ForeignKeyQuery}
class Authors(tag: Tag) extends Table[(Long, String, String)](tag, "AUTHORS") {
def id: Rep[Long] = column[Long]("ID", O.PrimaryKey)
def name: Rep[String] = column[String]("NAME")
def biography: Rep[String] = column[String]("BIOGRAPHY")
override def * : ProvenShape[(Long, String, String)] = (id, name, biography)
}
object Authors extends TableQuery(new Authors(_))
class Publishers(tag: Tag) extends Table[(Long, String, String)](tag, "PUBLISHERS") {
def id: Rep[Long] = column[Long]("ID", O.PrimaryKey)
def name: Rep[String] = column[String]("NAME")
def address: Rep[String] = column[String]("ADDRESS")
override def * : ProvenShape[(Long, String, String)] = (id, name, address)
}
object Publishers extends TableQuery(new Publishers(_))
class Books(tag: Tag) extends Table[(Long, String, Long, Long, String, Double, Long)](tag, "BOOKS") {
def id: Rep[Long] = column[Long]("ID", O.PrimaryKey)
def title: Rep[String] = column[String]("TITLE")
def authorId: Rep[Long] = column[Long]("AUTHOR_ID")
def publisherId: Rep[Long] = column[Long]("PUBLISHER_ID")
def isbn: Rep[String] = column[String]("ISBN")
def price: Rep[Double] = column[Double]("PRICE")
def pages: Rep[Long] = column[Long]("PAGES")
override def * : ProvenShape[(Long, String, Long, Long, String, Double, Long)] =
(id, title, authorId, publisherId, isbn, price, pages)
def author: ForeignKeyQuery[Authors, (Long, String, String)] =
foreignKey("AUTHOR_FK", authorId, TableQuery[Authors])(_.id)
def publisher: ForeignKeyQuery[Publishers, (Long, String, String)] =
foreignKey("PUBLISHER_FK", publisherId, TableQuery[Publishers])(_.id)
}
object Books extends TableQuery(new Books(_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment