Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export POOL_SIZE=8

## Example

### Using `new()` with a connection string

```rust
use diesel_adapter::casbin::prelude::*;
use diesel_adapter::DieselAdapter;
Expand All @@ -68,3 +70,26 @@ async fn main() -> Result<()> {
Ok(())
}
```

### Using `with_pool()` with an existing connection pool

If your application already manages a connection pool, you can share it with the Casbin enforcer using `with_pool()`:

```rust
use diesel::r2d2::{ConnectionManager, Pool};
use diesel_adapter::{casbin::prelude::*, Connection, DieselAdapter};

#[tokio::main]
async fn main() -> Result<()> {
// Build (or reuse) your application's existing pool.
// DATABASE_URL can be e.g. "postgres://user:pass@localhost:5432/mydb"
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = ConnectionManager::<Connection>::new(database_url);
let pool = Pool::builder().max_size(8).build(manager).unwrap();

let mut m = DefaultModel::from_file("examples/rbac_model.conf").await?;
let a = DieselAdapter::with_pool(pool)?;
let mut e = Enforcer::new(m, a).await?;
Ok(())
}
```
50 changes: 50 additions & 0 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct DieselAdapter {
pub const TABLE_NAME: &str = "casbin_rule";

impl DieselAdapter {
/// Creates a new `DieselAdapter` by building an r2d2 connection pool from
/// the given database `url` with the specified `pool_size`.
pub fn new<U: Into<String>>(url: U, pool_size: u32) -> Result<Self> {
let manager = ConnectionManager::new(url);
let pool = Pool::builder()
Expand All @@ -34,6 +36,24 @@ impl DieselAdapter {
Self::with_pool(pool)
}

/// Creates a new `DieselAdapter` from an existing r2d2 connection pool.
///
/// This is useful when you want to share a connection pool that is already
/// managed by your application with the Casbin enforcer.
///
/// # Example
///
/// ```rust,no_run
/// use diesel::r2d2::{ConnectionManager, Pool};
/// use diesel_adapter::{Connection, DieselAdapter};
///
/// let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
/// let manager = ConnectionManager::<Connection>::new(database_url);
/// let pool = Pool::builder().max_size(8).build(manager).unwrap();
///
/// // Share this pool between your application and DieselAdapter
/// let adapter = DieselAdapter::with_pool(pool).unwrap();
/// ```
pub fn with_pool(pool: Pool<ConnectionManager<adapter::Connection>>) -> Result<Self> {
let conn = pool
.get()
Expand Down Expand Up @@ -437,6 +457,36 @@ mod tests {
v.into_iter().map(|x| x.to_owned()).collect()
}

#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_with_pool() {
let pool = {
#[cfg(feature = "postgres")]
{
let manager = ConnectionManager::new(
"postgres://casbin_rs:casbin_rs@127.0.0.1:5432/casbin",
);
Pool::builder().max_size(8).build(manager).unwrap()
}

#[cfg(feature = "mysql")]
{
let manager = ConnectionManager::new(
"mysql://casbin_rs:casbin_rs@127.0.0.1:3306/casbin",
);
Pool::builder().max_size(8).build(manager).unwrap()
}

#[cfg(feature = "sqlite")]
{
let manager = ConnectionManager::new("casbin.db");
Pool::builder().max_size(8).build(manager).unwrap()
}
};

assert!(DieselAdapter::with_pool(pool).is_ok());
}

#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_adapter() {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ mod actions;
pub use casbin;

pub use adapter::DieselAdapter;
pub use actions::Connection;
pub use error::Error;
Loading