{"id":14155624,"url":"https://github.com/casbin-rs/sqlx-adapter","last_synced_at":"2025-04-12T17:46:26.084Z","repository":{"id":37087740,"uuid":"239451335","full_name":"casbin-rs/sqlx-adapter","owner":"casbin-rs","description":"Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs","archived":false,"fork":false,"pushed_at":"2025-01-17T16:04:23.000Z","size":219,"stargazers_count":54,"open_issues_count":0,"forks_count":29,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T20:12:06.557Z","etag":null,"topics":["abac","access-control","acl","adapter","auth","authentication","casbin","casbin-rs","mysql","orm","permission","postgres","rbac","rust","sqlite","sqlx","sqlx-adapter","storage-driver"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin-rs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casbin-rs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-10T07:21:58.000Z","updated_at":"2025-01-26T09:09:59.000Z","dependencies_parsed_at":"2023-11-28T02:26:05.452Z","dependency_job_id":"1c9906b6-a3a0-4ea4-b12f-633604ee5658","html_url":"https://github.com/casbin-rs/sqlx-adapter","commit_stats":{"total_commits":149,"total_committers":22,"mean_commits":"6.7727272727272725","dds":0.5167785234899329,"last_synced_commit":"af2bb692fdb615d4618ca849165b590cd2af8a0f"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fsqlx-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fsqlx-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fsqlx-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fsqlx-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin-rs","download_url":"https://codeload.github.com/casbin-rs/sqlx-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248609646,"owners_count":21132916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abac","access-control","acl","adapter","auth","authentication","casbin","casbin-rs","mysql","orm","permission","postgres","rbac","rust","sqlite","sqlx","sqlx-adapter","storage-driver"],"created_at":"2024-08-17T08:04:31.581Z","updated_at":"2025-04-12T17:46:26.057Z","avatar_url":"https://github.com/casbin-rs.png","language":"Rust","readme":"# sqlx-adapter\n\n[![Crates.io](https://img.shields.io/crates/v/sqlx-adapter.svg)](https://crates.io/crates/sqlx-adapter)\n[![Docs](https://docs.rs/sqlx-adapter/badge.svg)](https://docs.rs/sqlx-adapter)\n[![CI](https://github.com/casbin-rs/sqlx-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin-rs/sqlx-adapter/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/casbin-rs/sqlx-adapter/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin-rs/sqlx-adapter)\n\nsqlx-adapter is the [Sqlx](https://github.com/launchbadge/sqlx) adapter for [casbin-rs](https://github.com/casbin/casbin-rs). With this library, Casbin can load policy from Sqlx supported database or save policy to it with fully asynchronous support.\n\nBased on [Sqlx](https://github.com/launchbadge/sqlx), The current supported databases are:\n\n- [MySQL](https://www.mysql.com/)\n- [PostgreSQL](https://github.com/lib/pq)\n- [SQLite](https://www.sqlite.org)\n\n## Notice\n\nIn 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:\n\n````SQL\n# MySQL \u0026 PostgreSQL \u0026 SQLite\nALTER TABLE casbin_rules RENAME TO casbin_rule;\n````\n\n## Install\n\nAdd the following to `Cargo.toml`:\n\nFor MySQL:\n\n```toml\nsqlx-adapter = { version = \"1.7.0\", default-features = false, features = [\"mysql\", \"runtime-tokio-native-tls\"]}\ntokio = { version = \"1.1.1\", features = [\"macros\"] }\n```\n\nFor PostgreSQL:\n\n```toml\nsqlx-adapter = { version = \"1.7.0\", default-features = false, features = [\"postgres\", \"runtime-tokio-native-tls\"]}\ntokio = { version = \"1.1.1\", features = [\"macros\"] }\n```\n\nFor SQLite:\n\n```toml\nsqlx-adapter = { version = \"1.7.0\", default-features = false, features = [\"sqlite\", \"runtime-tokio-native-tls\"]}\ntokio = { version = \"1.1.1\", features = [\"macros\"] }\n```\n\n**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.\n\n## Configure\n\n1. Set up database environment\n   \n    You must prepare the database environment so that `Sqlx` can do static check with queries during compile time. One convenient option is using docker to get your database environment ready:\n    \n    ```bash\n    #!/bin/bash\n\n    DIS=$(lsb_release -is)\n\n    command -v docker \u003e /dev/null 2\u003e\u00261 || {\n        echo \"Please install docker before running this script.\" \u0026\u0026 exit 1;\n    }\n\n    if [ $DIS == \"Ubuntu\" ] || [ $DIS == \"LinuxMint\" ]; then\n        sudo apt install -y \\\n            libpq-dev \\\n            libmysqlclient-dev \\\n            postgresql-client \\\n            mysql-client-core;\n\n    elif [ $DIS == \"Deepin\" ]; then\n        sudo apt install -y \\\n            libpq-dev \\\n            libmysql++-dev \\\n            mysql-client \\\n            postgresql-client;\n    elif [ $DIS == \"ArchLinux\" ] || [ $DIS == \"ManjaroLinux\" ]; then\n        sudo pacman -S libmysqlclient \\\n            postgresql-libs \\\n            mysql-clients \\;\n    else\n        echo \"Unsupported system: $DIS\" \u0026\u0026 exit 1;\n    fi\n\n    docker run -itd \\\n        --restart always \\\n        -e POSTGRES_USER=casbin_rs \\\n        -e POSTGRES_PASSWORD=casbin_rs \\\n        -e POSTGRES_DB=casbin \\\n        -p 5432:5432 \\\n        -v /srv/docker/postgresql:/var/lib/postgresql \\\n        postgres:11;\n\n    docker run -itd \\\n        --restart always \\\n        -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \\\n        -e MYSQL_USER=casbin_rs \\\n        -e MYSQL_PASSWORD=casbin_rs \\\n        -e MYSQL_DATABASE=casbin \\\n        -p 3306:3306 \\\n        -v /srv/docker/mysql:/var/lib/mysql \\\n        mysql:8 \\\n        --default-authentication-plugin=mysql_native_password;\n\n    ```\n\n2. Create table `casbin_rule`\n\n    ```bash\n    # PostgreSQL\n    psql postgres://casbin_rs:casbin_rs@127.0.0.1:5432/casbin -c \"CREATE TABLE IF NOT EXISTS casbin_rule (\n        id SERIAL PRIMARY KEY,\n        ptype VARCHAR NOT NULL,\n        v0 VARCHAR NOT NULL,\n        v1 VARCHAR NOT NULL,\n        v2 VARCHAR NOT NULL,\n        v3 VARCHAR NOT NULL,\n        v4 VARCHAR NOT NULL,\n        v5 VARCHAR NOT NULL,\n        CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)\n        );\"\n\n    # MySQL\n    mysql -h 127.0.0.1 -u casbin_rs -pcasbin_rs casbin \n\n    CREATE TABLE IF NOT EXISTS casbin_rule (\n        id INT NOT NULL AUTO_INCREMENT,\n        ptype VARCHAR(12) NOT NULL,\n        v0 VARCHAR(128) NOT NULL,\n        v1 VARCHAR(128) NOT NULL,\n        v2 VARCHAR(128) NOT NULL,\n        v3 VARCHAR(128) NOT NULL,\n        v4 VARCHAR(128) NOT NULL,\n        v5 VARCHAR(128) NOT NULL,\n        PRIMARY KEY(id),\n        CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)\n        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n   \n   # SQLite\n   touch casbin.db\n   \n   sqlite3 casbin.db -cmd \"CREATE TABLE IF NOT EXISTS casbin_rule (\n       id INTEGER PRIMARY KEY,\n       ptype VARCHAR(12) NOT NULL,\n       v0 VARCHAR(128) NOT NULL,\n       v1 VARCHAR(128) NOT NULL,\n       v2 VARCHAR(128) NOT NULL,\n       v3 VARCHAR(128) NOT NULL,\n       v4 VARCHAR(128) NOT NULL,\n       v5 VARCHAR(128) NOT NULL,\n       CONSTRAINT unique_key_diesel_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5)\n       );\"\n    ```\n\n3. Configure `env`\n\n    Rename `sample.env` to `.env` and put `DATABASE_URL`, `POOL_SIZE`   inside\n\n    ```bash\n    DATABASE_URL=postgres://casbin_rs:casbin_rs@localhost:5432/casbin\n    # DATABASE_URL=mysql://casbin_rs:casbin_rs@localhost:3306/casbin\n    # DATABASE_URL=sqlite:casbin.db\n    POOL_SIZE=8\n    ```\n\n    Or you can export `DATABASE_URL`, `POOL_SIZE`\n\n    ```bash\n    export DATABASE_URL=postgres://casbin_rs:casbin_rs@localhost:5432/casbin\n    export POOL_SIZE=8\n    ```\n\n\n## Example\n\n```rust\nuse sqlx_adapter::casbin::prelude::*;\nuse sqlx_adapter::casbin::Result;\nuse sqlx_adapter::SqlxAdapter;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c()\u003e {\n    let m = DefaultModel::from_file(\"examples/rbac_model.conf\").await?;\n    \n    let a = SqlxAdapter::new(\"postgres://casbin_rs:casbin_rs@127.0.0.1:5432/casbin\", 8).await?;\n    let mut e = Enforcer::new(m, a).await?;\n    \n    Ok(())\n}\n\n```\n\n## Features\n\n- `postgres`\n- `mysql`\n- `sqlite`\n\n*Attention*: `postgres`, `mysql`, `sqlite` are mutual exclusive which means that you can only activate one of them.\n","funding_links":[],"categories":["sqlite"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Fsqlx-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin-rs%2Fsqlx-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Fsqlx-adapter/lists"}