Skip to content

Instantly share code, notes, and snippets.

@nemoo
Created April 16, 2024 12:22
Show Gist options
  • Select an option

  • Save nemoo/36071f5c8b05ecce38a26e12ebdd65bb to your computer and use it in GitHub Desktop.

Select an option

Save nemoo/36071f5c8b05ecce38a26e12ebdd65bb to your computer and use it in GitHub Desktop.

Revisions

  1. nemoo created this gist Apr 16, 2024.
    54 changes: 54 additions & 0 deletions test.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    //> 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")
    }