Last active
December 15, 2015 20:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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