Skip to content

Instantly share code, notes, and snippets.

@nemoo
Created April 16, 2024 12:22
Show Gist options
  • Save nemoo/36071f5c8b05ecce38a26e12ebdd65bb to your computer and use it in GitHub Desktop.
Save nemoo/36071f5c8b05ecce38a26e12ebdd65bb to your computer and use it in GitHub Desktop.
Scala cli slick blocking postgres examples
//> using dep com.github.takezoe::blocking-slick:0.0.15-RC1
//> using dep org.postgresql:postgresql:42.7.3
// To run, first start a database: docker run -it -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=playslickexample postgres
import com.github.takezoe.slick.blocking.BlockingPostgresDriver.blockingApi.*
import slick.jdbc.JdbcBackend
case class Project(id: Long, name: String)
val Projects: TableQuery[ProjectsTable] = TableQuery[ProjectsTable]
class ProjectsTable(tag: Tag) extends Table[Project](tag, "project") {
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
def name = column[String]("name")
def * = (id, name) <> (Project.apply.tupled, Project.unapply)
}
@main
def main =
println("Hello World!")
val db = Database.forURL(
url="jdbc:postgresql://localhost:5432/playslickexample",
driver="org.postgresql.Driver",
user="postgres",
password="secret",
)
def allProjects(implicit session: JdbcBackend#Session): List[Project] =
Projects.list
db.withSession { implicit session =>
// Projects.schema.create
val project = Project(0, "Hello")
val id = (Projects returning Projects.map(_.id)).insert(project)
// val res = Projects.list
val res = allProjects
println(res.mkString("\n"))
println(s"End of World! Just created id: $id")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment