Skip to content

Instantly share code, notes, and snippets.

@sam
Last active December 15, 2015 20:58
Show Gist options
  • Save sam/5322017 to your computer and use it in GitHub Desktop.
Save sam/5322017 to your computer and use it in GitHub Desktop.
Basic database interaction using Slick's "sql" String interpolator and returning a tuple for the row.
package models.slick
import com.typesafe.config.ConfigFactory
object DatabaseConnection {
import scala.slick.driver.PostgresDriver.simple._
// Note, I've got a Play-like Scala wrapper for Typesafe's Config library as well, which is a bit cleaner than this.
implicit lazy val database = ConfigFactory.load("conf/database.conf").getConfig("db.default") match { case config =>
Database.forURL(
config.getString("url"),
config.getString("user"),
config.getString("password"),
driver = config.getString("driver")
)
}
}
package models.slick
object Photos {
import scala.slick.jdbc._
import StaticQuery._
def all(implicit database:Database) = database withSession { implicit session:Session =>
sql"""
SELECT p.uuid,
p.slug,
p.width,
p.height,
cp.channel_id
FROM photos p
JOIN channels_photos cp ON p.uuid = cp.photo_uuid
WHERE p.deleted_at IS NULL
""".as[(String, String, Int, Int, Int)].list
}
}
object SomeApp extends App {
import models.slick._
import DatabaseConnection._
Photos.all.foreach { case (uuid, slug, width, height, channelId) =>
println(s"$uuid: $slug (${width}x${height})")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment