mysql-connector

Crates.iomysql-connector
lib.rsmysql-connector
version0.0.2
sourcesrc
created_at2024-06-03 05:32:44.390762
updated_at2024-06-06 15:38:29.553799
descriptionSimple MySQL connector that allows exchanging the underlying connection
homepage
repositoryhttps://github.com/riseupgroup/mysql-connector
max_upload_size
id1259725
size172,111
owners (github:riseupgroup:owners)

documentation

README

mysql-connector

Simple MySQL connector for Rust that allows exchanging the underlying connection.

Features

  • tcpstream (enabled by default): implements the Stream trait for tokio's TcpStream.
  • serde: implements serde::Serialize and serde::Deserialize for some types.

Example

use std::sync::Arc;

use mysql_connector::{
    macros::*, model::*, types::AuthPlugin, Connection, ConnectionOptions, TcpStream,
};

#[derive(Debug, ModelData, FromQueryResult, ActiveModel, IntoQuery, Model)]
#[mysql_connector(table = "user", primary = "id", auto_increment = "true")]
pub struct User {
    id: u32,
    name: String,
    email: Option<String>,
}

#[tokio::main]
async fn main() {
    dotenv::dotenv().unwrap();
    let mut conn = <Connection<TcpStream>>::connect(Arc::new(ConnectionOptions {
        user: "user".into(),
        password: std::env::var("PASSWORD").unwrap(),
        db_name: Some("db".into()),
        host: Some("localhost".into()),
        secure_auth: false,
        auth_plugin: Some(AuthPlugin::Native),
        ..Default::default()
    }))
    .await
    .unwrap();

    conn.execute_query(
        "CREATE TABLE `user` (
            `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            `name` VARCHAR(255) NOT NULL,
            `email` VARCHAR(255)
        )",
    )
    .await
    .unwrap();

    User {
        id: 0,
        name: String::from("foo"),
        email: Some(String::from("foo@example.com")),
    }
    .into_active_model()
    .insert(&mut conn)
    .await
    .unwrap();

    User {
        id: 0,
        name: String::from("bar"),
        email: None,
    }
    .into_active_model()
    .insert(&mut conn)
    .await
    .unwrap();

    let users: Vec<User> = conn
        .query(&User::build_query())
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    println!("{users:?}");

    conn.disconnect().await.unwrap();
}
Commit count: 11

cargo fmt