#http #async #web #gotham #diesel


A Gotham Middleware that provides access to a Diesel connection via an R2D2 pool to allow other Middleware and Handlers to interact with a database

4 releases

new 0.2.0 Sep 15, 2020
0.1.2 May 20, 2020
0.1.1 May 20, 2020
0.1.0 Jul 27, 2019

#32 in HTTP server

46 downloads per month
Used in gotham_restful



Gotham Diesel Middleware

The gotham diesel middleware provides offers a convenient API for interacting with Diesel from Gotham.


This middleware introduces a Repo struct, which is used as a layer between Diesel and Gotham to ensure that database interaction can be easily chained alongside other asynchronous operations. This structure is fairly straightfoward and offers an easy way to interact with Diesel from inside Gotham:

// create a new repo, in this case just using a SQLite setup
let repo: Repo<SqliteConnection> = Repo::new("products.db");

// create a middleware pipeline from our middleware
let pipeline = single_middleware(DieselMiddleware::new(repo));

// construct a basic chain from our pipeline
let (chain, pipelines) = single_pipeline(pipeline);

// build a router with the chain & pipeline
gotham::start("", build_router(chain, pipelines, |route| {

From there you gain simple access to Repo on the request state, just like when using other middlewares. You can then use the Repo to execute database calls:

// borrow the repo from the state
let repo = Repo::borrow_from(&state);

// execute database calls
repo.run(move |conn| {

repo.run returns a Future, allowing you to seamlessly sprinkle your database calls amongst other asynchronous handler code. The Repo type manages the synchronous calls of the underlying connections using tokio_threadpool::blocking, which allows blocking operations to run without blocking the tokio reactor. Although not true async, this allows multiple concurrent database requests to be handled, with a default of 100 concurrent blocking operations. For further details see tokio_threadpool::blocking documentation.

For a complete example, see the example in the main repository.


To customize aspects of the connection pool, you can construct a repo from an r2d2::Builder, setting any attributes available on there:

let repo = Repo::from_pool_builder(database_url,

Isolated test transactions

When used in tests, the middleware can use isolated test transactions to allow tests to run in parallel. In test transactions, queries from separate connections do not interfere with each other and are rolled back when the connection is dropped at the end of each test.

fn do_something() {
    let repo = Repo::with_test_transactions(DATABASE_URL);
    // Run some test code that accesses the repo.
    // This test will be isolated, and at the end the transaction rolled back.

See full example in the main repository linked above for more details.


~284K SLoC