Apache Casbin SQLx Adapter (Incubating)
Apache Casbin (Incubating) is an effort undergoing incubation at the Apache Software Foundation (ASF), sponsored by the Apache Incubator PMC. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
casbin-sqlx-adapter is the SQLx adapter for casbin-rs. With this library, Casbin can load policy from a SQLx-supported database or save policy to it with fully asynchronous support.
Based on SQLx, the current supported databases are:
Notice
In order to unify the database table name in Casbin ecosystem, we decide to use casbin_rule instead of casbin_rules from version 0.4.0. If you are using old version sqlx-adapter in your production environment, please use following command and update sqlx-adapter version:
# MySQL & PostgreSQL & SQLite
casbin_rules RENAME TO casbin_rule;
Install
Requires Rust 1.94 or newer (the minimum supported by SQLx 0.9). The snippets
below track the latest 1.x release; the exact version is on the crates.io
badge above.
Add the following to Cargo.toml:
For MySQL:
= { = "1.9", = false, = ["mysql", "runtime-tokio", "tls-native-tls"]}
= { = "1.1.1", = ["macros"] }
For PostgreSQL:
= { = "1.9", = false, = ["postgres", "runtime-tokio", "tls-native-tls"]}
= { = "1.1.1", = ["macros"] }
For SQLite:
= { = "1.9", = false, = ["sqlite", "runtime-tokio", "tls-native-tls"]}
= { = "1.1.1", = ["macros"] }
Starting with 1.9.0, the registry package name changes from sqlx-adapter to
casbin-sqlx-adapter. Replace the dependency key in Cargo.toml as shown above.
The library name remains sqlx_adapter, so existing Rust imports and the
SqlxAdapter type do not change. Existing versions of sqlx-adapter remain
available. Release candidates are for review and are not published to crates.io;
the new package and documentation links become available after release approval
and publication.
Warning: tokio v1.0 or later is supported from sqlx-adapter v0.4.0, we recommend that you upgrade the relevant components to ensure that they work properly. The last version that supports tokio v0.2 is sqlx-adapter v0.3.0 , you can choose according to your needs.
Configure
No database is needed to build this crate. All queries are executed at runtime, so cargo build works offline and you never have to set DATABASE_URL or run cargo sqlx prepare in your own project. The steps below are only about the database your application talks to at runtime (and about running this repository's own test suite).
-
Set up database environment
One convenient option is using docker to get your database environment ready:
#!/bin/bash DIS= || { && ; } if [ || [; then ; elif [; then ; elif [ || [; then else && ; fi ; ; -
Create table
casbin_ruleSqlxAdapterissuesCREATE TABLE IF NOT EXISTSon startup, so this is optional — do it yourself only if your database user is not allowed to create tables.# PostgreSQL # MySQL ) ) ) ) ) ) ) ) ) ) ENGINE=InnoDB ; # SQLite -
Configure
env(only needed to run this repository's tests)Rename
sample.envto.envand putDATABASE_URL,POOL_SIZEinsideDATABASE_URL=postgres://casbin_rs:casbin_rs@localhost:5432/casbin # DATABASE_URL=mysql://casbin_rs:casbin_rs@localhost:3306/casbin # DATABASE_URL=sqlite:casbin.db POOL_SIZE=8Or you can export
DATABASE_URL,POOL_SIZE
Example
use *;
use Result;
use SqlxAdapter;
async
Features
postgresmysqlsqliteruntime-tokioruntime-async-stdtls-native-tlstls-rustls
The legacy combined runtime/TLS features remain available as backward-compatible aliases:
runtime-tokio-native-tlsruntime-tokio-rustlsruntime-async-std-native-tlsruntime-async-std-rustls
Attention: postgres, mysql, sqlite are mutual exclusive which means that you can only activate one of them.