Adding in diesel with basic tutorial for library in db
This commit is contained in:
parent
ea00c7d932
commit
0942faec90
|
@ -1 +1,3 @@
|
|||
/target
|
||||
db.sqlite
|
||||
.env
|
|
@ -74,6 +74,12 @@ dependencies = [
|
|||
"system-deps",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-expr"
|
||||
version = "0.15.2"
|
||||
|
@ -99,6 +105,44 @@ dependencies = [
|
|||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "diesel"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7a532c1f99a0f596f6960a60d1e119e91582b24b39e2d83a190e61262c3ef0c"
|
||||
dependencies = [
|
||||
"diesel_derives",
|
||||
"libsqlite3-sys",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "diesel_derives"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "74398b79d81e52e130d991afeed9c86034bb1b7735f46d2f5bf7deb261d80303"
|
||||
dependencies = [
|
||||
"diesel_table_macro_syntax",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "diesel_table_macro_syntax"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
|
||||
dependencies = [
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenvy"
|
||||
version = "0.15.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
|
||||
|
||||
[[package]]
|
||||
name = "field-offset"
|
||||
version = "0.3.6"
|
||||
|
@ -612,6 +656,17 @@ version = "0.2.146"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b"
|
||||
|
||||
[[package]]
|
||||
name = "libsqlite3-sys"
|
||||
version = "0.26.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.10"
|
||||
|
@ -896,7 +951,10 @@ dependencies = [
|
|||
name = "sample-amp"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"dotenvy",
|
||||
"gtk4",
|
||||
"libsqlite3-sys",
|
||||
"relm4",
|
||||
"relm4-components",
|
||||
"relm4-icons",
|
||||
|
@ -1045,6 +1103,33 @@ dependencies = [
|
|||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ea9e1b3cf1243ae005d9e74085d4d542f3125458f3a81af210d901dcd7411efd"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"serde",
|
||||
"time-core",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "372950940a5f07bf38dbe211d7283c9e6d7327df53794992d293e534c733d09b"
|
||||
dependencies = [
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.28.2"
|
||||
|
@ -1149,6 +1234,12 @@ version = "1.0.9"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version-compare"
|
||||
version = "0.1.1"
|
||||
|
|
|
@ -9,4 +9,7 @@ edition = "2021"
|
|||
gtk = { version = "0.6.6", package = "gtk4" }
|
||||
relm4 = { version = "0.6.0", features = ["libadwaita"] }
|
||||
relm4-components = "0.6.0"
|
||||
relm4-icons = { version = "0.6.0", features = ["plus", "minus"] }
|
||||
relm4-icons = { version = "0.6.0", features = ["plus", "minus"] }
|
||||
diesel = { version = "2.1.0", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
|
||||
libsqlite3-sys = { version = "0.26.0", features = ["bundled"] }
|
||||
dotenvy = "0.15"
|
|
@ -0,0 +1,9 @@
|
|||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations"
|
|
@ -0,0 +1 @@
|
|||
DROP TABLE libraries
|
|
@ -0,0 +1,4 @@
|
|||
CREATE TABLE libraries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
name VARCHAR NOT NULL
|
||||
)
|
|
@ -0,0 +1,18 @@
|
|||
use sample_amp::*;
|
||||
use std::io::{stdin, stdout, Write};
|
||||
|
||||
fn main() {
|
||||
let connection = &mut establish_connection();
|
||||
|
||||
let mut name = String::new();
|
||||
|
||||
print!("Enter a name for the collection > ");
|
||||
stdout().flush().unwrap();
|
||||
|
||||
stdin().read_line(&mut name).unwrap();
|
||||
|
||||
let name = name.trim_end();
|
||||
|
||||
let library = create_library(connection, name);
|
||||
println!("Saved new library: {}, id {}", name, library.id)
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
use self::models::*;
|
||||
use diesel::prelude::*;
|
||||
use sample_amp::*;
|
||||
|
||||
fn main() {
|
||||
use self::schema::libraries::dsl::*;
|
||||
|
||||
let connection = &mut establish_connection();
|
||||
let results = libraries
|
||||
.limit(5)
|
||||
.select(Library::as_select())
|
||||
.load(connection)
|
||||
.expect("Error loading libraries");
|
||||
|
||||
println!("Displaying {} libraries", results.len());
|
||||
for lib in results {
|
||||
println!("{}", lib.name)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
use diesel::prelude::*;
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
|
||||
use crate::models::{NewLibrary, Library};
|
||||
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
pub fn establish_connection() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
pub fn create_library(conn: &mut SqliteConnection, name: &str) -> Library {
|
||||
use crate::schema::libraries;
|
||||
|
||||
let new_library = NewLibrary { name };
|
||||
|
||||
diesel::insert_into(libraries::table)
|
||||
.values(&new_library)
|
||||
.returning(Library::as_returning())
|
||||
.get_result(conn)
|
||||
.expect("Error saving new post")
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use diesel::prelude::*;
|
||||
use super::schema::libraries;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = libraries)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Library {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = libraries)]
|
||||
pub struct NewLibrary<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
libraries (id) {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue