#mysql #async #wasi

mysql_async_wasi

Tokio based asynchronous MySql client library

7 releases

0.31.6 Nov 11, 2023
0.31.5 Sep 7, 2023
0.31.4 Aug 17, 2023
0.31.3 Jun 6, 2023
0.30.0 Oct 1, 2022

#145 in Database interfaces

Download history 176/week @ 2023-08-14 67/week @ 2023-08-21 61/week @ 2023-08-28 127/week @ 2023-09-04 167/week @ 2023-09-11 65/week @ 2023-09-18 54/week @ 2023-09-25 22/week @ 2023-10-02 56/week @ 2023-10-09 77/week @ 2023-10-16 112/week @ 2023-10-23 101/week @ 2023-10-30 91/week @ 2023-11-06 173/week @ 2023-11-13 59/week @ 2023-11-20 69/week @ 2023-11-27

433 downloads per month
Used in mega_etl

MIT/Apache

370KB
7.5K SLoC

mysql_async for WebAssembly

Tokio based asynchronous MySql client library for The Rust Programming Language. This is a fork from the original mysql_async with support for WebAssembly compilation target. That allows async MySql apps to run inside the WasmEdge Runtime as a lightweight and secure alternative to natively compiled apps in Linux container.

For more details and usage examples, please see the upstream mysql_async source and this example.

Note: We do not yet support SSL / TLS connections to databases in this WebAssembly client.

Installation

[dependencies]
mysql_async_wasi = "<desired version>"

Crate Features

Default feature set is wide – it includes all default mysql_common features as well as miniz_oxide flate2 backend.

List Of Features

  • minimal – enables only necessary features (at the moment the only necessary feature is zlib flate2 backend). Enables:

    • flate2/zlib

    Example:

    [dependencies]
    mysql_async_wasi = { version = "*", default-features = false, features = ["minimal"]}
    

    *Note: it is possible to use another flate2 backend by directly choosing it:

    [dependencies]
    mysql_async_wasi = { version = "*", default-features = false }
    flate2 = { version = "*", default-features = false, features = ["rust_backend"] }
    
  • default – enables the following set of crate's and dependencies' features:

    • flate2/rust_backend
    • mysql_common/bigdecimal03
    • mysql_common/rust_decimal
    • mysql_common/time03
    • mysql_common/uuid
    • mysql_common/frunk
  • default-rustls – same as default but with rustls-tls.

    Example:

    [dependencies]
    mysql_async_wasi = { version = "*", default-features = false, features = ["default-rustls"] }
    
  • native-tls-tls – enables native-tls-based TLS support (conflicts with rustls-tls)

    Example:

    [dependencies]
    mysql_async_wasi = { version = "*", default-features = false, features = ["native-tls-tls"] }
    
    
  • rustls-tls – enables native-tls-based TLS support (conflicts with native-tls-tls)

    Example:

    [dependencies]
    mysql_async_wasi = { version = "*", default-features = false, features = ["rustls-tls"] }
    
    

TLS/SSL Support

SSL support comes in two flavors:

  1. Based on rustls – TLS backend written in Rust (see the rustls-tls crate feature).

  2. Based on native-tls – this is the option that usually works without pitfalls (see the native-tls-tls crate feature).

    Please also note a few things about rustls:

    • it will fail if you'll try to connect to the server by its IP address, hostname is required;
    • it, most likely, won't work on windows, at least with default server certs, generated by the MySql installer.

Example

use mysql_async::prelude::*;

#[derive(Debug, PartialEq, Eq, Clone)]
struct Payment {
    customer_id: i32,
    amount: i32,
    account_name: Option<String>,
}

#[tokio::main]
async fn main() -> Result<()> {
    let payments = vec![
        Payment { customer_id: 1, amount: 2, account_name: None },
        Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) },
        Payment { customer_id: 5, amount: 6, account_name: None },
        Payment { customer_id: 7, amount: 8, account_name: None },
        Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) },
    ];

    let database_url = /* ... */
    # get_opts();

    let pool = mysql_async::Pool::new(database_url);
    let mut conn = pool.get_conn().await?;

    // Create a temporary table
    r"CREATE TEMPORARY TABLE payment (
        customer_id int not null,
        amount int not null,
        account_name text
    )".ignore(&mut conn).await?;

    // Save payments
    r"INSERT INTO payment (customer_id, amount, account_name)
      VALUES (:customer_id, :amount, :account_name)"
        .with(payments.iter().map(|payment| params! {
            "customer_id" => payment.customer_id,
            "amount" => payment.amount,
            "account_name" => payment.account_name.as_ref(),
        }))
        .batch(&mut conn)
        .await?;

    // Load payments from the database. Type inference will work here.
    let loaded_payments = "SELECT customer_id, amount, account_name FROM payment"
        .with(())
        .map(&mut conn, |(customer_id, amount, account_name)| Payment { customer_id, amount, account_name })
        .await?;

    // Dropped connection will go to the pool
    drop(conn);

    // The Pool must be disconnected explicitly because
    // it's an asynchronous operation.
    pool.disconnect().await?;

    assert_eq!(loaded_payments, payments);

    // the async fn returns Result, so
    Ok(())
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~11–32MB
~514K SLoC