title: How to work with transactions and dependent records on Ionic 2 and SQLite?
body: <p>I`m creating an Ionic 2 application with SQLite. I've already been able to execute commands, creating tables and inserting records on the database with success. Now I'm trying to insert some parent and detail records, and I'd like to do this inside a transaction, so that when any error occurs when inserting the child records, I won't have a broken parent record.</p>
  
  <p>Well, according to this link (<a href="https://github.com/litehelpers/Cordova-sqlite-storage" rel="nofollow noreferrer">https://github.com/litehelpers/Cordova-sqlite-storage</a>), I can use a transaction in the following way: </p>
  
  <pre><code>db.transaction(function(tx) {
      tx.executeSql("Insert into ParentTable(ParentName) values ('name')");
      tx.executeSql("Insert into ChildTable(ParentID, ChildName) values (0, 'value1')");
  }, function(error) {
      console.log('Transaction ERROR: ' + error.message);
  }, function() {
      console.log('Transaction OK');
  });
  </code></pre>
  
  <p>The problem is that I need to get the ParentID from the first insert to use in the second insert. The insertSQL command has a callback , so i write the following code: </p>
  
  <pre><code>db.transaction(function(tx) 
  {
      tx.executeSql("Insert into ParentTable(ParentName) values ('name')", 
          function(tx, rs) 
          {
              /* Location 1 */
              var parentID = rs.insertId;
              tx.executeSql("Insert into ChildTable(ParentID, ChildName) values (?, 'value1')", [parentID]);
          });
      /* Location 2 */
  }, function(error) {
      console.log('Transaction ERROR: ' + error.message);
  }, function() {
      console.log('Transaction OK');
  });
  </code></pre>
  
  <p>So, there lies my doubt. Since executeSql is assynchronous, Location 2 will execute before location 1. 
  The question is: Will the transaction be finished after location 2, when the <code>db.transaction</code> gets out of scope?
  If yes, Location 1 will be executed after the transaction finished, so how can I make it execute inside the transaction?</p>
  
  <p>If no, when it will be committed or rolled back?</p>