title: Slick 3 Transactions with logic in Scala
body: <p>I have a question about Slick 3 and Transactions </p>
  
  <p>I have read the documentation</p>
  
  <p><a href="http://slick.typesafe.com/doc/3.1.0/dbio.html" rel="noreferrer">http://slick.typesafe.com/doc/3.1.0/dbio.html</a></p>
  
  <p>and the other Slick 3 transaction questions</p>
  
  <p><a href="https://stackoverflow.com/questions/30327617/slick-3-transactions">Slick 3 Transactions</a></p>
  
  <p><a href="https://stackoverflow.com/questions/31103813/executing-non-database-actions-in-a-transaction-in-slick-3?rq=1">Executing non-database actions in a transaction in Slick 3</a></p>
  
  <p>But they didn't help me</p>
  
  <p>I need to read some values from the database, run some logic in Scala, and then depending on the result, modify the database.  I want the entire operation to be atomic.</p>
  
  <p>My code looks something like this:</p>
  
  <pre><code>database.run(TableQuery[X].filter(blah).result).map { x =&gt;
    database.run { 
      if( someLogicNotInSQL(x) ) 
        TableQuery[Y].insert(someFoo) 
      else 
        TableQuery[Y].insert(someBah)
    } 
  }
  </code></pre>
  
  <p>How do I get the value of the query, run some logic in Scala, and then run another action (e.g. insert) all as one atomic transaction.</p>
  
  <p>Thanks
  Peter</p>