diff --git a/.env b/.env new file mode 100644 index 0000000..c50537d --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://elnafo:test@localhost/elnafo diff --git a/.gitignore b/.gitignore index 8ea0ee8..6f8a6c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target result* +/temp +/.direnv diff --git a/Cargo.lock b/Cargo.lock index b7026a0..f9cd99d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,125 @@ version = 3 [[package]] -name = "rust-example" +name = "bitflags" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "db" version = "0.1.0" +dependencies = [ + "diesel", + "dotenvy", +] + +[[package]] +name = "diesel" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62c6fcf842f17f8c78ecf7c81d75c5ce84436b41ee07e03f490fbb5f5a8731d8" +dependencies = [ + "bitflags", + "byteorder", + "diesel_derives", + "itoa", + "pq-sys", +] + +[[package]] +name = "diesel_derives" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8337737574f55a468005a83499da720f20c65586241ffea339db9ecdfd2b44" +dependencies = [ + "diesel_table_macro_syntax", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "diesel_table_macro_syntax" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" +dependencies = [ + "syn", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + +[[package]] +name = "elnafo" +version = "0.1.0" +dependencies = [ + "db", + "diesel", +] + +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + +[[package]] +name = "pq-sys" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c0052426df997c0cbd30789eb44ca097e3541717a7b8fa36b1c464ee7edebd" +dependencies = [ + "vcpkg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/Cargo.toml b/Cargo.toml index d461ec5..f193e97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,3 @@ -[package] -name = "elnafo" -version = "0.1.0" -edition = "2021" - [workspace] -members = ["crates/server"] +resolver = "2" +members = [ "crates/db","crates/server"] diff --git a/README.md b/README.md index 17df126..0a890df 100644 --- a/README.md +++ b/README.md @@ -1,8 +1 @@ -# Basic Rust Example - -* Creating/updating `Cargo.lock` -```shell -touch Cargo.lock -nix develop -cargo check -``` +# elnafo diff --git a/crates/db/.env b/crates/db/.env new file mode 100644 index 0000000..c50537d --- /dev/null +++ b/crates/db/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://elnafo:test@localhost/elnafo diff --git a/crates/db/Cargo.toml b/crates/db/Cargo.toml new file mode 100644 index 0000000..53003dc --- /dev/null +++ b/crates/db/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "db" +version = "0.1.0" +edition = "2021" + +[dependencies] +diesel = { version = "2.1.4", features = ["postgres"] } +dotenvy = "0.15.7" diff --git a/crates/db/diesel.toml b/crates/db/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/crates/db/diesel.toml @@ -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" diff --git a/crates/db/migrations/.keep b/crates/db/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/crates/db/migrations/00000000000000_diesel_initial_setup/down.sql b/crates/db/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..516e2a5 --- /dev/null +++ b/crates/db/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,7 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); + diff --git a/crates/db/migrations/00000000000000_diesel_initial_setup/up.sql b/crates/db/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..416e022 --- /dev/null +++ b/crates/db/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,38 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + + diff --git a/crates/db/migrations/2024-03-04-191840_create_users/down.sql b/crates/db/migrations/2024-03-04-191840_create_users/down.sql new file mode 100644 index 0000000..4ef4d0b --- /dev/null +++ b/crates/db/migrations/2024-03-04-191840_create_users/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE IF EXISTS "users"; diff --git a/crates/db/migrations/2024-03-04-191840_create_users/up.sql b/crates/db/migrations/2024-03-04-191840_create_users/up.sql new file mode 100644 index 0000000..93b2f27 --- /dev/null +++ b/crates/db/migrations/2024-03-04-191840_create_users/up.sql @@ -0,0 +1,9 @@ +-- Your SQL goes here +CREATE TABLE "users"( + "id" SERIAL NOT NULL PRIMARY KEY, + "login" TEXT NOT NULL, + "hashed_password" TEXT NOT NULL, + "name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "is_admin" BOOL NOT NULL +); diff --git a/crates/db/src/lib.rs b/crates/db/src/lib.rs new file mode 100644 index 0000000..5caaec2 --- /dev/null +++ b/crates/db/src/lib.rs @@ -0,0 +1,51 @@ +pub mod models; +pub mod schema; + +use diesel::prelude::*; +use dotenvy::dotenv; +use std::env; + +use crate::models::{NewUser, User}; + +pub fn establish_connection() -> PgConnection { + dotenv().ok(); + + let db_url = env::var("DATABASE_URL").expect("Missed DATABASE_URL"); + + PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)) +} + +pub fn create_user( + connection: &mut PgConnection, + login: &str, + hashed_password: &str, + name: &str, + email: &str, + is_admin: bool, +) -> User { + use crate::schema::users; + + let new_user = NewUser { + login, + hashed_password, + name, + email, + is_admin, + }; + + diesel::insert_into(users::table) + .values(&new_user) + .returning(User::as_returning()) + .get_result(connection) + .expect("Error creating new user") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + unimplemented!(); + } +} diff --git a/crates/db/src/models.rs b/crates/db/src/models.rs new file mode 100644 index 0000000..81ea1ff --- /dev/null +++ b/crates/db/src/models.rs @@ -0,0 +1,24 @@ +use crate::schema; +use diesel::prelude::*; + +#[derive(Queryable, Selectable)] +#[diesel(table_name = schema::users)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct User { + pub id: i32, + pub login: String, + pub hashed_password: String, + pub name: String, + pub email: String, + pub is_admin: bool, +} + +#[derive(Insertable)] +#[diesel(table_name = schema::users)] +pub struct NewUser<'a> { + pub login: &'a str, + pub hashed_password: &'a str, + pub name: &'a str, + pub email: &'a str, + pub is_admin: bool, +} diff --git a/crates/db/src/schema.rs b/crates/db/src/schema.rs new file mode 100644 index 0000000..a1a5577 --- /dev/null +++ b/crates/db/src/schema.rs @@ -0,0 +1,12 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> Int4, + login -> Text, + hashed_password -> Text, + name -> Text, + email -> Text, + is_admin -> Bool, + } +} diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 6c9d648..ff336d9 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -1,10 +1,13 @@ [package] -name = "server" +name = "elnafo" version = "0.1.0" edition = "2021" +authors = ["L-Nafaryus