The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Rust Diesel Single File MySQL Example

TODO: Pull subtitle into page object
Code
use self::schema::widgets;
use self::schema::widgets::dsl::*;
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;

mod schema;

fn main() {
    println!("Hello world");
    let conn = &mut establish_connection();

    let new_widget = NewWidget {
        content: Some("quick brown fox"),
    };

    diesel::insert_into(widgets::table)
        .values(&new_widget)
        .execute(conn)
        .expect("Error inserting new post");

    let results = widgets.load::<Widget>(conn).expect("Error loading posts");
    for widget in results {
        println!("{} {}", widget.id, widget.content.unwrap());
    }
}

pub fn establish_connection() -> MysqlConnection {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    MysqlConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to database"))
}

#[derive(Queryable)]
pub struct Widget {
    pub id: String,
    pub content: Option<String>,
}

#[derive(Insertable)]
#[diesel(table_name = widgets)]
pub struct NewWidget<'a> {
    pub content: Option<&'a str>,
}



Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Rust Diesel Single File MySQL Example

-- code
-- rust

use self::schema::widgets;
use self::schema::widgets::dsl::*;
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;

mod schema;

fn main() {
    println!("Hello world");
    let conn = &mut establish_connection();

    let new_widget = NewWidget {
        content: Some("quick brown fox"),
    };

    diesel::insert_into(widgets::table)
        .values(&new_widget)
        .execute(conn)
        .expect("Error inserting new post");

    let results = widgets.load::<Widget>(conn).expect("Error loading posts");
    for widget in results {
        println!("{} {}", widget.id, widget.content.unwrap());
    }
}

pub fn establish_connection() -> MysqlConnection {
    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    MysqlConnection::establish(&database_url)
        .unwrap_or_else(|_| panic!("Error connecting to database"))
}

#[derive(Queryable)]
pub struct Widget {
    pub id: String,
    pub content: Option<String>,
}

#[derive(Insertable)]
#[diesel(table_name = widgets)]
pub struct NewWidget<'a> {
    pub content: Option<&'a str>,
}





-- categories
-- Diesel 
-- MySQL 
-- Rust 

-- metadata
-- date: 2023-05-23 12:25:23
-- id: 2qcyeiaw
-- site: aws
-- type: post
-- status: draft