Multistage commits for transactions 

This is a very basic fix for #844.

When transaction is committed it does this:
  1. Splits new records and their relations in transaction into two stages: "parents" and "children"
  2. Commits "parents" to the adapter
  3. Waits until all parents' ids are materialized
  4. Commits "children" and all other updated and deleted records


I see several problems with this code:

 * Only two stages. Ideally this should handle arbitrary complex relations but it would require topological sorting and breaking cycles.

 * Waiting for ids. #858 could help this, but from my tests it looks like promises will be resolved before record ids are materialized. I'm not sure.

 * Documentation and refactoring for the whole thing.

Could someone please provide feedback on this? Is this the right direction to go or there is some other solution? What else needs to be done?