Application able to CRUD
This commit is contained in:
commit
d2cb5b3031
19 changed files with 3268 additions and 0 deletions
20
src/lib.rs
Normal file
20
src/lib.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
||||
|
||||
pub mod models;
|
||||
|
||||
pub async fn create_db(db_url: &str) -> sqlx::Result<SqlitePool> {
|
||||
if !Sqlite::database_exists(db_url).await.unwrap_or(false) {
|
||||
println!("Creating database {}", db_url);
|
||||
match Sqlite::create_database(db_url).await {
|
||||
Ok(_) => println!("create db success"),
|
||||
Err(e) => panic!("Cannot create db: {e:?}"),
|
||||
};
|
||||
} else {
|
||||
println!("Database already exists")
|
||||
}
|
||||
|
||||
let db = SqlitePool::connect(db_url).await?;
|
||||
sqlx::migrate!().run(&db).await?;
|
||||
|
||||
Ok(db)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue