Skip to content

Commit 2751c88

Browse files
committed
feat: implement persist.BatchAdapter interface
1 parent 5183beb commit 2751c88

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

adapter.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ type Adapter struct {
8888
muInitialize sync.Once
8989
}
9090

91+
var (
92+
_ persist.Adapter = (*Adapter)(nil)
93+
_ persist.BatchAdapter = (*Adapter)(nil)
94+
)
95+
9196
// finalizer is the destructor for Adapter.
9297
func finalizer(a *Adapter) {
9398
sqlDB, err := a.db.DB()
@@ -709,6 +714,16 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
709714
return a.db.Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error
710715
}
711716

717+
// AddPoliciesCtx adds multiple policy rules to the storage.
718+
func (a *Adapter) AddPoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error {
719+
var lines []CasbinRule
720+
for _, rule := range rules {
721+
line := a.savePolicyLine(ptype, rule)
722+
lines = append(lines, line)
723+
}
724+
return a.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error
725+
}
726+
712727
// Transaction perform a set of operations within a transaction.
713728
func (a *Adapter) Transaction(e casbin.IEnforcer, fc func(casbin.IEnforcer) error, opts ...*sql.TxOptions) error {
714729
// ensure the transactionMu is initialized

0 commit comments

Comments
 (0)