Skip to content

[doc] transactions #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@
* [Preloading Association](association.md#preloading-association)
* [Modifying Association](association.md#modifying-association)

* Others

* Transactions
* Logging
* [Transactions](transactions.md)
4 changes: 3 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<script>
window.$docsify = {
name: 'rel',
ga: 'UA-118567391-2',
repo: 'https://github.com/Fs02/rel',
auto2top: true,
themeColor: '#007d9c',
Expand All @@ -33,8 +34,9 @@
]
}
</script>
<script src="//cdn.jsdelivr.net/npm/docsify-tabs@1"></script>
<script src="//unpkg.com/docsify/lib/plugins/ga.min.js"></script>
<script src="//unpkg.com/prismjs/components/prism-go.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/docsify-tabs@1"></script>
<script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions docs/transactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Transactions

To declare a transaction, use `Transaction` method.
It accepts a function with `rel.Repository` argument and returns an error.

If any error occured within transaction, the transaction will be rolled back, and returns the error.
If the error is a runtime error or `panic` with string argument, it'll panic after rollback.

<!-- tabs:start -->

### **main.go**

```go
if err := repo.Transaction(func(repo rel.Repository) error {
repo.Update(&books, rel.Dec("stock"))
return repo.Update(&transaction, rel.Set("paid", true))
}); err != nil {
// handle error
}
```

### **main_test.go**

```go
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectUpdate(rel.Dec("stock")).ForType("main.Book")
repo.ExpectUpdate(rel.Set("paid", true)).ForType("main.Transaction")
})
```

<!-- tabs:end -->