Last active
December 24, 2015 10:29
-
-
Save ambantis/6784092 to your computer and use it in GitHub Desktop.
Mapping a Scala Slick Table object to a Postgres View
This file contains 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
case class DbGFolder(id: String, | |
eTag: String, | |
url: String, | |
iconUrl: String, | |
title: String, | |
owner: String, | |
parents: Option[String], | |
children: Option[String], | |
scions: Option[String], | |
created: LocalDateTime, | |
modified: LocalDateTime) | |
object DbGFolders extends Table[DbGFolder]("gfolder_view") { | |
def id = column[String]("id") | |
def eTag = column[String]("e_tag") | |
def url = column[String]("url") | |
def iconUrl = column[String]("icon_url") | |
def title = column[String]("title") | |
def owner = column[String]("file_owner") | |
def parents = column[String]("parent_str") | |
def children = column[String]("child_str") | |
def scions = column[String]("scion_str") | |
def created = column[LocalDateTime]("created") | |
def modified = column[LocalDateTime]("modified") | |
def * = id ~ eTag ~ url ~ iconUrl ~ title ~ owner ~ parents.? ~ | |
children.? ~ scions.? ~ created ~ modified | |
<> (DbGFolder, DbGFolder.unapply _) | |
def findAllFor(root: String)(implicit s: Session): Map[String, GFolder] = { | |
val query = for { | |
(_, gFolder) <- DbScions.innerJoin(DbGFolders) | |
.on(_.scion === _.id) | |
.where(_._1.id === root) | |
} yield gFolder | |
query.list().map {v => | |
GFolder(v.id, | |
v.eTag, | |
v.url, | |
v.iconUrl, | |
v.title, | |
v.owner, | |
v.parents.map { parentStr => | |
parentStr.split(",").toSet }.getOrElse(Set()), | |
v.children.map{ childStr => | |
childStr.split(",").toSet }.getOrElse(Set()), | |
v.scions.map { scionStr => | |
scionStr.split(",").toSet }.getOrElse(Set()), | |
v.created, | |
v.modified) | |
}.groupBy(_.id).mapValues(_.head) | |
} | |
def findById(fileId: String)(implicit s: Session): Option[GFolder] = { | |
Query(DbGFolders).filter(_.id === fileId).list().headOption.map {v => | |
GFolder(v.id, | |
v.eTag, | |
v.url, | |
v.iconUrl, | |
v.title, | |
v.owner, | |
v.parents.map { parentStr => | |
parentStr.split(",").toSet }.getOrElse(Set()), | |
v.children.map{ childStr => | |
childStr.split(",").toSet }.getOrElse(Set()), | |
v.scions.map { scionStr => | |
scionStr.split(",").toSet }.getOrElse(Set()), | |
v.created, | |
v.modified) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment