♻️ (seaorm): Separa módulo migration de db
- `db::*` queda como API de consultas (connection, fetch_*). - `migration::*` sube a primer nivel con su propia documentación. - `DBCONN` y `run_now` se trasladan a la raíz de la extensión. - Actualiza README.md y docs para reflejar la nueva estructura.
This commit is contained in:
parent
796ae5ce81
commit
026448e511
12 changed files with 279 additions and 217 deletions
|
|
@ -70,7 +70,7 @@ async fn main() -> std::io::Result<()> {
|
|||
**Escribe las migraciones** usando la API de SeaORM:
|
||||
|
||||
```rust,no_run
|
||||
use pagetop_seaorm::db::*;
|
||||
use pagetop_seaorm::migration::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
|
|
@ -103,12 +103,75 @@ enum Users {
|
|||
|
||||
use pagetop::prelude::*;
|
||||
|
||||
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||
use url::Url;
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
include_locales!(LOCALES_SEAORM);
|
||||
|
||||
pub mod config;
|
||||
|
||||
pub mod db;
|
||||
|
||||
pub mod migration;
|
||||
|
||||
pub(crate) use futures::executor::block_on as run_now;
|
||||
|
||||
pub(crate) static DBCONN: LazyLock<DatabaseConnection> = LazyLock::new(|| {
|
||||
trace::info!(
|
||||
"Connecting to database \"{}\" using a pool of {} connections",
|
||||
&config::SETTINGS.database.db_name,
|
||||
&config::SETTINGS.database.max_pool_size
|
||||
);
|
||||
|
||||
let db_uri = match config::SETTINGS.database.db_type.as_str() {
|
||||
"mysql" | "postgres" => {
|
||||
let mut tmp_uri = Url::parse(
|
||||
format!(
|
||||
"{}://{}/{}",
|
||||
&config::SETTINGS.database.db_type,
|
||||
&config::SETTINGS.database.db_host,
|
||||
&config::SETTINGS.database.db_name
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.unwrap();
|
||||
tmp_uri
|
||||
.set_username(config::SETTINGS.database.db_user.as_str())
|
||||
.unwrap();
|
||||
// https://github.com/launchbadge/sqlx/issues/1624
|
||||
tmp_uri
|
||||
.set_password(Some(config::SETTINGS.database.db_pass.as_str()))
|
||||
.unwrap();
|
||||
if let Some(port) = config::SETTINGS.database.db_port {
|
||||
tmp_uri.set_port(Some(port)).unwrap();
|
||||
}
|
||||
tmp_uri
|
||||
}
|
||||
"sqlite" => Url::parse(
|
||||
format!(
|
||||
"{}://{}",
|
||||
&config::SETTINGS.database.db_type,
|
||||
&config::SETTINGS.database.db_name
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.unwrap(),
|
||||
_ => panic!(
|
||||
"Unrecognized database type \"{}\"",
|
||||
config::SETTINGS.database.db_type
|
||||
),
|
||||
};
|
||||
|
||||
run_now(Database::connect::<ConnectOptions>({
|
||||
let mut db_opt = ConnectOptions::new(db_uri.to_string());
|
||||
db_opt.max_connections(config::SETTINGS.database.max_pool_size);
|
||||
db_opt
|
||||
}))
|
||||
.unwrap_or_else(|_| panic!("Failed to connect to database"))
|
||||
});
|
||||
|
||||
/// Implementa la extensión.
|
||||
pub struct SeaORM;
|
||||
|
||||
|
|
@ -122,6 +185,6 @@ impl Extension for SeaORM {
|
|||
}
|
||||
|
||||
fn initialize(&self) {
|
||||
std::sync::LazyLock::force(&db::DBCONN);
|
||||
std::sync::LazyLock::force(&DBCONN);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue