Categorygithub.com/mef13/casbin-sqlx-adapter
modulepackage
0.3.0
Repository: https://github.com/mef13/casbin-sqlx-adapter.git
Documentation: pkg.go.dev

# README

casbin-sqlx-adapter

This is fork of the original project casbin-sqlx-adapter

sqlx adapter for Casbin https://github.com/casbin/casbin

Based on sqlx, and tested in MySQL and Postgres.

Installation

go get github.com/mef13/casbin-sqlx-adapter

Usage example

opts := &AdapterOptions{
    DriverName: "mysql",
    DataSourceName: "root:1234@tcp(127.0.0.1:3306)/yourdb",
    TableName: "casbin_rule",
    // or reuse an existing connection:
    // DB: myDBConn,
}

a := NewAdapterFromOptions(opts)
// Casbin v2 may return an error
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
    panic(err)
}

Notice

The v2 version of Casbin has some break change, check for the detail. If you are still using v1 version, use 0.1.x of this project.

The implement is kind of different from the official one. In this implement you should create the database and table on your own.

In my opinion, in a general PRODUCTION environment, the business code can rarely create a database, create a table or drop a table.

Thank

Thanks to casbin-sqlx-adapter for his original project.

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

# Functions

NewAdapter is the constructor for Adapter Deprecated: Use NewAdapterFromOptions instead.
NewAdapterByDB is the constructor for Adapter with existed connection Deprecated: Use NewAdapterFromOptions instead.
NewAdapterFromOptions is the constructor for Adapter with existed connection.

# Structs

Adapter represents the sqlx adapter for policy storage.
AdapterOptions contains all possible configuration options.
CasbinRule ...