switch-to-seaorm #1
							
								
								
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -1573,6 +1573,7 @@ dependencies = [ | |||||||
|  "axum", |  "axum", | ||||||
|  "barrel", |  "barrel", | ||||||
|  "dotenvy", |  "dotenvy", | ||||||
|  |  "entity", | ||||||
|  "mime_guess", |  "mime_guess", | ||||||
|  "minijinja", |  "minijinja", | ||||||
|  "minijinja-autoreload", |  "minijinja-autoreload", | ||||||
|  | |||||||
| @ -16,6 +16,7 @@ use_barrel = ["barrel", "sqlformat"] | |||||||
| default = ["use_barrel"] | default = ["use_barrel"] | ||||||
| 
 | 
 | ||||||
| [dependencies] | [dependencies] | ||||||
|  | entity = { path = "./entity" } | ||||||
| sea-orm = { version = "0.12.10", features = [ | sea-orm = { version = "0.12.10", features = [ | ||||||
|     "runtime-tokio-native-tls", |     "runtime-tokio-native-tls", | ||||||
|     "sqlx-postgres", |     "sqlx-postgres", | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								Justfile
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								Justfile
									
									
									
									
									
								
							| @ -1,3 +1,5 @@ | |||||||
|  | set dotenv-load := true | ||||||
|  | 
 | ||||||
| default: | default: | ||||||
|     @echo "# Miniweb Project" |     @echo "# Miniweb Project" | ||||||
|     @just --list |     @just --list | ||||||
| @ -33,3 +35,6 @@ dev-install: | |||||||
| dev-reset: | dev-reset: | ||||||
|     sea-orm-cli migrate reset |     sea-orm-cli migrate reset | ||||||
| 
 | 
 | ||||||
|  | # Creates Entities from Database
 | ||||||
|  | db-create-entities: | ||||||
|  |     sea-orm-cli generate entity -u $DATABASE_URL -o entity_generated/src --lib | ||||||
|  | |||||||
| @ -1 +1,5 @@ | |||||||
| pub mod user; | pub mod user; | ||||||
|  | pub mod permission; | ||||||
|  | 
 | ||||||
|  | pub use user::Entity as User; | ||||||
|  | pub use permission::Entity as Permission; | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ pub struct Model { | |||||||
|     pub id: i32, |     pub id: i32, | ||||||
|     pub username: String, |     pub username: String, | ||||||
|     #[sea_orm(column_type = "Text")] |     #[sea_orm(column_type = "Text")] | ||||||
|     pub description: String, |     pub description: Option<String>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| use dotenvy::dotenv; | use dotenvy::dotenv; | ||||||
| use miniweb::auth::models::{NewUser, User}; | use entity; | ||||||
| use sea_orm::{DatabaseConnection, Database, DbConn}; | use sea_orm::{DatabaseConnection, Database, DbConn, ActiveModelTrait, DbErr, Set}; | ||||||
| use std::env; | use std::env; | ||||||
| use std::io::stdin; | use std::io::stdin; | ||||||
| 
 | 
 | ||||||
| @ -11,14 +11,13 @@ pub async fn establish_connection() -> DatabaseConnection { | |||||||
|     db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) |     db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn create_user(conn: &DbConn, username: &str) -> User { | pub async fn create_user(conn: &DbConn, username: &str) -> Result<entity::user::Model, DbErr> { | ||||||
|     /* |     let user = entity::user::ActiveModel { | ||||||
|     diesel::insert_into(users::table) |         username: Set(username.to_owned()), | ||||||
|         .values(&new_user) |         ..Default::default() | ||||||
|         .returning(User::as_returning()) |     }; | ||||||
|         .get_result(conn) |     let user = user.insert(conn).await?; | ||||||
|         .expect("Error saving new user") |     Ok(user) | ||||||
|          */ |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -32,6 +31,6 @@ async fn main() { | |||||||
|     stdin().read_line(&mut username).unwrap(); |     stdin().read_line(&mut username).unwrap(); | ||||||
|     let username = username.trim_end(); // Remove the trailing newline
 |     let username = username.trim_end(); // Remove the trailing newline
 | ||||||
| 
 | 
 | ||||||
|     let user = create_user(&connection, username); |     let user = create_user(&connection, username).await.expect("Error creating user"); | ||||||
|     println!("\nSaved user {} with id {}", username, user.id); |     println!("\nSaved user {} with id {}", username, user.id); | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,22 +1,22 @@ | |||||||
| use dotenvy::dotenv; | use dotenvy::dotenv; | ||||||
| use miniweb::auth::models::User; | use entity; | ||||||
|  | use sea_orm::{DatabaseConnection, Database, EntityTrait}; | ||||||
| use std::env; | use std::env; | ||||||
| 
 | 
 | ||||||
| pub fn establish_connection() -> PgConnection { | pub async fn establish_connection() -> DatabaseConnection { | ||||||
|     dotenv().ok(); |     dotenv().ok(); | ||||||
| 
 |  | ||||||
|     let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); |     let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); | ||||||
|     PgConnection::establish(&database_url) |     let db = Database::connect(&database_url).await; | ||||||
|         .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) |     db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn main() { |  | ||||||
|     let connection = &mut establish_connection(); |  | ||||||
| 
 | 
 | ||||||
|     let results = users | #[tokio::main] | ||||||
|         .select(User::as_select()) | async fn main() { | ||||||
|         .load(connection) |     let connection = establish_connection().await; | ||||||
|         .expect("Error loading posts"); | 
 | ||||||
|  |     let results = entity::User::find() | ||||||
|  |         .all(&connection).await.expect("Error loading users."); | ||||||
| 
 | 
 | ||||||
|     println!("Displaying {} users", results.len()); |     println!("Displaying {} users", results.len()); | ||||||
|     for user in results { |     for user in results { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user