Skip to content

Instantly share code, notes, and snippets.

@odenzo
Created November 12, 2019 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odenzo/268d276ee42a8f9dffd71a6b01de63c6 to your computer and use it in GitHub Desktop.
Save odenzo/268d276ee42a8f9dffd71a6b01de63c6 to your computer and use it in GitHub Desktop.
upsert
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