{"action":"created","issue":{"url":"https://api.github.com/repos/treojs/treo/issues/32","labels_url":"https://api.github.com/repos/treojs/treo/issues/32/labels{/name}","comments_url":"https://api.github.com/repos/treojs/treo/issues/32/comments","events_url":"https://api.github.com/repos/treojs/treo/issues/32/events","html_url":"https://github.com/treojs/treo/issues/32","id":92959143,"number":32,"title":"Support multiple operations per transaction, with Treo sugar","user":{"login":"glsignal","id":1109291,"avatar_url":"https://avatars.githubusercontent.com/u/1109291?v=3","gravatar_id":"","url":"https://api.github.com/users/glsignal","html_url":"https://github.com/glsignal","followers_url":"https://api.github.com/users/glsignal/followers","following_url":"https://api.github.com/users/glsignal/following{/other_user}","gists_url":"https://api.github.com/users/glsignal/gists{/gist_id}","starred_url":"https://api.github.com/users/glsignal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/glsignal/subscriptions","organizations_url":"https://api.github.com/users/glsignal/orgs","repos_url":"https://api.github.com/users/glsignal/repos","events_url":"https://api.github.com/users/glsignal/events{/privacy}","received_events_url":"https://api.github.com/users/glsignal/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/treojs/treo/labels/feature","name":"feature","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-07-04T01:44:59Z","updated_at":"2015-07-08T07:14:34Z","closed_at":null,"body":"Currently (as I understand it), Treo opens a transaction for each atomic operation—`get`, `put` etc.—allowing for the possibility of data changes between read and write:\r\n\r\n```javascript\r\nvar facts = db.store('avocadoFacts');\r\n\r\nfacts.get(12).then( (realAvocadoFact) =>  {\r\n  // If I wanted to update that record here, that happens in a new transaction. It's possible that some change happens to the record in IndexedDB while I'm playing with it in memory.\r\n  realAvocadoFact.body = \"sometimes avocados have bad taste\";\r\n  facts.put(realAvocadoFact, () => {\r\n    /* My changes are done and persisted */\r\n  });\r\n});\r\n```\r\n\r\nWhile it's probably not a huge deal for many applications, I'm running into it fairly frequently (my data is not directly generated by a user interface) and so I'm finding myself falling back to IndexedDB's standard API; opening a big transaction to do my work, and hence losing a lot of the Treo sugar.\r\n> (I'll note that I'm working in a chrome extension too, so there are multiple JS environments executing at any given time, all with access to the same IndexedDB stores).\r\n\r\n```javascript\r\ndb.transaction(\"readwrite\", [\"avocadoFacts\"], (err, tx) => {\r\n  var facts = tx.objectStore(\"avocadoFacts\");\r\n  \r\n  facts.get(12).onsuccess = (evt) => {\r\n    var realAvocadoFact = evt.target.result; \r\n    realAvocadoFact.body = \"sometimes avocados have bad taste\";\r\n    facts.put(realAvocadoFact);\r\n  };\r\n\r\n  tx.oncomplete = () => {\r\n    /* My changes are done and persisted */\r\n  };\r\n});\r\n```\r\n\r\nI was wondering if there were any plans for a feature like chaining reads and writes together in a single transaction (effectively making an `update` call that's much less verbose than the IndexedDB equivalent), or any thoughts on how difficult it might be to carry over the transaction to arbitrary actions on data.\r\n\r\nThis is a little hairy and ill-defined, but for instance here's some code I'd like to be able to write:\r\n\r\n```javascript\r\nvar facts = db.store('avocadoFacts');\r\n\r\nfacts.openTransaction()\r\n  .get(12)\r\n  .put( (realAvocadoFact) =>  {\r\n    realAvocadoFact.body = \"sometimes avocados have bad taste\";\r\n    return realAvocadoFact; // return value is persisted and passed to any chained functions\r\n  })\r\n  .done( (realAvocadoFact) => {\r\n    /* My changes are done and persisted */\r\n  });\r\n});\r\n```\r\n\r\nTerrible API aside (very little thought has gone into it; I just want to open the discussion), is it clear what I'm getting at? The example above (updating a record) might be better as a plugin's job, but I'd be interested to hear your thoughts on making better use of transactions in situations like these, and addressing the root cause. Transactions as a concept are extremely useful, but I feel like they're being under utilised."},"comment":{"url":"https://api.github.com/repos/treojs/treo/issues/comments/119465749","html_url":"https://github.com/treojs/treo/issues/32#issuecomment-119465749","issue_url":"https://api.github.com/repos/treojs/treo/issues/32","id":119465749,"user":{"login":"alekseykulikov","id":158189,"avatar_url":"https://avatars.githubusercontent.com/u/158189?v=3","gravatar_id":"","url":"https://api.github.com/users/alekseykulikov","html_url":"https://github.com/alekseykulikov","followers_url":"https://api.github.com/users/alekseykulikov/followers","following_url":"https://api.github.com/users/alekseykulikov/following{/other_user}","gists_url":"https://api.github.com/users/alekseykulikov/gists{/gist_id}","starred_url":"https://api.github.com/users/alekseykulikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alekseykulikov/subscriptions","organizations_url":"https://api.github.com/users/alekseykulikov/orgs","repos_url":"https://api.github.com/users/alekseykulikov/repos","events_url":"https://api.github.com/users/alekseykulikov/events{/privacy}","received_events_url":"https://api.github.com/users/alekseykulikov/received_events","type":"User","site_admin":false},"created_at":"2015-07-08T07:14:34Z","updated_at":"2015-07-08T07:14:34Z","body":"Nice :)\r\nYes, treo's migration to promises was guided by ES7 async/await syntax and growing adoptions of ES6 Promises."}}