Skip to content

Commit 0cca5f5

Browse files
Copilotnomeguy
andcommitted
Add comprehensive transaction support documentation and examples
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent a8b5cce commit 0cca5f5

File tree

7 files changed

+970
-0
lines changed

7 files changed

+970
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Casbin is a powerful and efficient open-source access control library for Golang
3939
- [Get started](#get-started)
4040
- [Policy management](#policy-management)
4141
- [Policy persistence](#policy-persistence)
42+
- [Transaction support](#transaction-support)
4243
- [Policy consistence between multiple nodes](#policy-consistence-between-multiple-nodes)
4344
- [Role manager](#role-manager)
4445
- [Benchmarks](#benchmarks)
@@ -200,6 +201,29 @@ We also provide a [web-based UI](https://casbin.org/docs/admin-portal) for model
200201

201202
https://casbin.org/docs/adapters
202203

204+
## Transaction support
205+
206+
Casbin provides built-in support for transactional policy updates through the `TransactionalEnforcer`. This allows you to ensure atomic consistency between Casbin policy operations and business database operations.
207+
208+
```go
209+
// Create a transactional enforcer
210+
enforcer, _ := casbin.NewTransactionalEnforcer("model.conf", transactionalAdapter)
211+
212+
// Use transactions to ensure atomicity
213+
err := enforcer.WithTransaction(ctx, func(tx *casbin.Transaction) error {
214+
// Update business data in your database
215+
db.UpdateUserRole(userId, "admin")
216+
217+
// Update Casbin policies in the same transaction
218+
tx.AddGroupingPolicy(userId, "admin")
219+
tx.AddPolicy("admin", "resource", "write")
220+
221+
return nil // Commits both changes atomically
222+
})
223+
```
224+
225+
See [TRANSACTION_GUIDE.md](TRANSACTION_GUIDE.md) for comprehensive documentation and [examples/transaction](examples/transaction) for working examples.
226+
203227
## Policy consistence between multiple nodes
204228

205229
https://casbin.org/docs/watchers

0 commit comments

Comments
 (0)