Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mmikalsen/casbun
bun adapter for casbin
https://github.com/mmikalsen/casbun
bun casbin
Last synced: 3 days ago
JSON representation
bun adapter for casbin
- Host: GitHub
- URL: https://github.com/mmikalsen/casbun
- Owner: mmikalsen
- License: mit
- Created: 2025-01-30T23:47:23.000Z (15 days ago)
- Default Branch: main
- Last Pushed: 2025-02-11T00:30:33.000Z (4 days ago)
- Last Synced: 2025-02-11T00:47:36.539Z (4 days ago)
- Topics: bun, casbin
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CasBun
CasBun is a [Bun](https://bun.uptrace.dev/) ORM adapter for [Casbin](https://casbin.org/).
With this library, Casbin can load policy from bun supported database or save policy to it.## Installation
```
go get github.com/mmikalsen/casbun
```## Simple Example
```go
package mainimport (
"github.com/mmikalsen/casbun"
"github.com/casbin/casbin/v2"
)func main() {
sqldb, _ := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared"")
db := bun.NewDB(sqldb, sqlitedialect.New())a, _ := casbun.NewAdapter(ctx, db)
e, _ := casbin.NewEnforcer("model.conf", a)// check the permission.
_, _ = e.Enforce("alice", "data1", "read")// save the policy back to DB.
_ = e.SavePolicy()
}
```## Context Adapter
`casbun` supports adapter with context, the following is a timeout control implemented using context```go
a, _ = casbun.NewAdapter(ctx, db)
// Limited time 300s
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
err := a.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
if err != nil {
panic(err)
}
```## Credits
This adapter is a rewrite of the original
[junishimura/casbin-bun-adapter](https://github.com/JunNishimura/casbin-bun-adapter)
, with a focus on reducing unnecessary dependencies, preserving core
functionality, and improving the integration with Bun.## License
casbin-bun-adapter is released under [MIT License](https://github.com/mmikalsen/casbun/blob/main/LICENSE).