Created
November 12, 2019 04:55
-
-
Save odenzo/268d276ee42a8f9dffd71a6b01de63c6 to your computer and use it in GitHub Desktop.
upsert
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
val get = quote { | |
query[Region].filter(_.code == lift(region)) | |
} | |
def insert(code: String) = quote { | |
query[Region].insert(lift(Region(code = code))).returning(_.id) | |
} | |
// HELP:: Better way? | |
val prog = for { | |
r <- run(get).map(_.head).recoverWith { | |
case NoSuchElementException => run(insert(region)).map(id => Region(id, region)) | |
} | |
} yield r | |
prog.transact(xa) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment