{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/701","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/701/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/701/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/701/events","html_url":"https://github.com/tgriesser/bookshelf/issues/701","id":69384720,"number":701,"title":"Best practice for optional transaction argument on model method?","user":{"login":"olalonde","id":416585,"avatar_url":"https://avatars.githubusercontent.com/u/416585?v=3","gravatar_id":"","url":"https://api.github.com/users/olalonde","html_url":"https://github.com/olalonde","followers_url":"https://api.github.com/users/olalonde/followers","following_url":"https://api.github.com/users/olalonde/following{/other_user}","gists_url":"https://api.github.com/users/olalonde/gists{/gist_id}","starred_url":"https://api.github.com/users/olalonde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olalonde/subscriptions","organizations_url":"https://api.github.com/users/olalonde/orgs","repos_url":"https://api.github.com/users/olalonde/repos","events_url":"https://api.github.com/users/olalonde/events{/privacy}","received_events_url":"https://api.github.com/users/olalonde/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-04-19T05:00:30Z","updated_at":"2015-04-23T04:55:52Z","closed_at":null,"body":"Is there any pattern/best practice to let users pass an optional transaction on a model method?\r\n\r\nHere's what I came up with so far:\r\n\r\n```javascript\r\n    customSave: function (t, commit) {\r\n      if (!t) return bookshelf.transaction(this.customSave.bind(this), true);\r\n      this.save(null, { transacting: t }).then(function () {\r\n            // do something\r\n      }).then(function () {\r\n          if (commit) return t.commit();\r\n      }).catch(function () {\r\n          return t.rollback();\r\n      });\r\n    }\r\n```\r\n\r\nMy goal is to execute customSave within a transaction if no transaction argument is passed but to use the transaction that was passed if it exists. Does that make sense?"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/95432509","html_url":"https://github.com/tgriesser/bookshelf/issues/701#issuecomment-95432509","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/701","id":95432509,"user":{"login":"rhys-vdw","id":822105,"avatar_url":"https://avatars.githubusercontent.com/u/822105?v=3","gravatar_id":"","url":"https://api.github.com/users/rhys-vdw","html_url":"https://github.com/rhys-vdw","followers_url":"https://api.github.com/users/rhys-vdw/followers","following_url":"https://api.github.com/users/rhys-vdw/following{/other_user}","gists_url":"https://api.github.com/users/rhys-vdw/gists{/gist_id}","starred_url":"https://api.github.com/users/rhys-vdw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rhys-vdw/subscriptions","organizations_url":"https://api.github.com/users/rhys-vdw/orgs","repos_url":"https://api.github.com/users/rhys-vdw/repos","events_url":"https://api.github.com/users/rhys-vdw/events{/privacy}","received_events_url":"https://api.github.com/users/rhys-vdw/received_events","type":"User","site_admin":false},"created_at":"2015-04-23T04:55:52Z","updated_at":"2015-04-23T04:55:52Z","body":"My pleasure. Thanks for prompting me to think about this problem. I just updated my code in my previous response, I think the concept is sound. Good luck."}}