Skip to content

Commit

Permalink
wip: play-scala-slick-example
Browse files Browse the repository at this point in the history
Currently stuck on typing error with `TableQuery`
I need to make sure that the error is not caused by my version of `play-slick`
  • Loading branch information
PeuTit authored and mkurz committed Feb 24, 2024
1 parent 1ef7d53 commit 132232b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 17 deletions.
39 changes: 33 additions & 6 deletions play-scala-slick-example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
lazy val scala213 = "2.13.10"
lazy val scala3 = "3.3.0-RC3"

lazy val root = (project in file("."))
.settings(
name := "play-scala-slick-examples",
version := "1.0-SNAPSHOT",
scalaVersion := "2.13.12",
scalaVersion := scala3,
crossScalaVersions := Seq(scala213, scala3),
)
.aggregate(
basicSample,
computerDatabaseSample,
personSample
personSample,
)

def sampleProject(name: String) =
Project(s"$name-sample", file("samples") / name)
.enablePlugins(PlayScala)
//.enablePlugins(PlayNettyServer).disablePlugins(PlayAkkaHttpServer) // uncomment to use the Netty backend
.settings(
scalaVersion := "2.13.12",
scalaVersion := scala3,
crossScalaVersions := Seq(scala213, scala3),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
"-Werror"
),
scalacOptions ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
Seq(
"-Xsource:3",
)
case Some((3, _)) =>
Seq(
"-explain",
)
case _ => Nil
}
},
libraryDependencies ++= Seq(
guice,
"com.typesafe.play" %% "play-slick" % "5.2.0",
"com.typesafe.play" %% "play-slick-evolutions" % "5.2.0",
"com.typesafe.play" %% "play-slick" % "5.3.0-RC1",
"com.typesafe.play" %% "play-slick-evolutions" % "5.3.0-RC1",
"com.h2database" % "h2" % "2.2.224",
specs2 % Test,
),
excludeDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) =>
Seq(
ExclusionRule("com.typesafe.play", "ssl-config-core_2.13"),
ExclusionRule("com.typesafe.play", "play_2.13"),
)
case _ => Nil
}
},
(Global / concurrentRestrictions) += Tags.limit(Tags.Test, 1)
)
.settings((Test / javaOptions) += "-Dslick.dbs.default.connectionTimeout=30 seconds")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class Application @Inject() (
mapping(
"name" -> text(),
"color" -> text()
)(Cat.apply)(Cat.unapply)
)(Cat.apply)(c => Some(c.name, c.color))
)

val dogForm: Form[Dog] = Form(
mapping(
"name" -> text(),
"color" -> text()
)(Dog.apply)(Dog.unapply)
)(Dog.apply)(d => Some(d.name, d.color))
)

def index = Action.async { implicit request =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class CatDAO @Inject() (protected val dbConfigProvider: DatabaseConfigProvider)(
def name = column[String]("NAME", O.PrimaryKey)
def color = column[String]("COLOR")

def * = (name, color) <> (Cat.tupled, Cat.unapply)
def * = (name, color) <> ((Cat.apply _).tupled, Cat.unapply)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class DogDAO @Inject() (@NamedDatabase("mydb") protected val dbConfigProvider: D
def name = column[String]("NAME", O.PrimaryKey)
def color = column[String]("COLOR")

def * = (name, color) <> (Dog.tupled, Dog.unapply)
def * = (name, color) <> ((Dog.apply _).tupled, Dog.unapply)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Application @Inject() (
"introduced" -> optional(date("yyyy-MM-dd")),
"discontinued" -> optional(date("yyyy-MM-dd")),
"company" -> optional(longNumber)
)(Computer.apply)(Computer.unapply)
)(Computer.apply)(c => Some(c.id, c.name, c.introduced, c.discontinued, c.companyId))
)

// -- Actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait CompaniesComponent { self: HasDatabaseConfigProvider[JdbcProfile] =>
class Companies(tag: Tag) extends Table[Company](tag, "COMPANY") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def name = column[String]("NAME")
def * = (id.?, name) <> (Company.tupled, Company.unapply _)
def * = (id.?, name) <> ((Company.apply _).tupled, Company.unapply _)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ class ComputersDAO @Inject() (protected val dbConfigProvider: DatabaseConfigProv
class Computers(tag: Tag) extends Table[Computer](tag, "COMPUTER") {

implicit val dateColumnType: ComputersDAO.this.profile.BaseColumnType[java.util.Date] = MappedColumnType.base[Date, Long](d => d.getTime, d => new Date(d))
//implicit val dateColumnType: profile.BaseColumnType[Date] = MappedColumnType.base[Date, Long](d => d.getTime, d => new Date(d))

def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def name = column[String]("NAME")
def introduced = column[Option[Date]]("INTRODUCED")
def discontinued = column[Option[Date]]("DISCONTINUED")
def companyId = column[Option[Long]]("COMPANY_ID")

def * = (id.?, name, introduced, discontinued, companyId) <> (Computer.tupled, Computer.unapply _)
def * = (id.?, name, introduced, discontinued, companyId) <> ((Computer.apply).tupled, Computer.unapply _)
}

private val computers = TableQuery[Computers]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

@import helper._

@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }

@main {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

@import helper._

@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }

@main {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class PersonController @Inject()(repo: PersonRepository,
mapping(
"name" -> nonEmptyText,
"age" -> number.verifying(min(0), max(140))
)(CreatePersonForm.apply)(CreatePersonForm.unapply)
)(CreatePersonForm.apply)(cpf => Some(cpf.name, cpf.age))
}

/**
* The index action.
*/
def index = Action { implicit request =>
def index: Action[AnyContent] = Action { implicit request =>
Ok(views.html.index(personForm))
}

Expand Down

0 comments on commit 132232b

Please sign in to comment.