12 unstable releases (3 breaking)

new 0.3.0 Dec 1, 2024
0.2.3 Oct 19, 2024
0.2.0 Sep 28, 2024
0.1.1 Sep 8, 2024
0.0.8 Aug 25, 2024

#764 in Database interfaces

Download history 276/week @ 2024-08-17 166/week @ 2024-08-24 130/week @ 2024-08-31 280/week @ 2024-09-07 39/week @ 2024-09-14 3/week @ 2024-09-21 126/week @ 2024-09-28 146/week @ 2024-10-05 198/week @ 2024-10-12 187/week @ 2024-10-19 25/week @ 2024-10-26 12/week @ 2024-11-02 8/week @ 2024-11-09 12/week @ 2024-11-16 8/week @ 2024-11-23 178/week @ 2024-11-30

207 downloads per month
Used in spring-sqlx-migration-plu…

MIT license

68KB
1.5K SLoC

crates.io Documentation

Dependencies

spring-sqlx = { version = "<version>", features = ["mysql"] }

You can replace postgres, mysql, sqlitefeature to select the appropriate database driver.

optional features:

  • with-json
  • with-chrono
  • with-rust_decimal
  • with-bigdecimal
  • with-uuid
  • with-time

Configuration items

[sqlx]
uri = "postgres://root:123456@localhost:5432/pg_db"  # Database address
min_connections = 1          # Minimum number of connections in the connection pool, the default value is 1
max_connections = 10         # Maximum number of connections in the connection pool, the default value is 10
acquire_timeout = 30000      # Connection timeout, in milliseconds, default 30s
idle_timeout = 600000        # Connection idle time, in milliseconds, default 10min
connect_timeout = 1800000    # Maximum connection survival time, in milliseconds, default 30min

Components

After configuring the above configuration items, the plugin will automatically register a ConnectPool connection pool object. This object is an alias for sqlx::AnyPool.

pub type ConnectPool = sqlx::AnyPool;

Extract the Component registered by the plugin

The SqlxPlugin plugin automatically registers a Sqlx connection pool component for us. We can use Component to extract this connection pool from AppState. Component is an axum extractor.

use spring_sqlx::{sqlx::{self, Row}, ConnectPool};
use spring_web::get;
use spring_web::extractor::Component;
use spring_web::error::Result;
use anyhow::Context;

#[get("/version")]
async fn mysql_version(Component(pool): Component<ConnectPool>) -> Result<String> {
    let version = sqlx::query("select version() as version")
        .fetch_one(&pool)
        .await
        .context("sqlx query failed")?
        .get("version");
    Ok(version)
}

Complete code reference sqlx-example

Dependencies

~17–36MB
~545K SLoC