* Refactor `ConnectionTrait` * Refactoring * Build index & foreign key statements * Fix imports * Fixup * Rocket example with migration * async-std compatible with the tokio 1.0 runtime * Use reexported dependency * Compile without selecting any db backend * Updating sea-orm-cli dep * sea-orm-cli migrate commands * cargo fmt * Test [cli] * Refactoring * Clap app name should be "sea-orm-cli" * Correctly capture MIGRATION_DIR * Rename README * Add `sea-orm-cli migrate init` command * Update README * Try restructured sea-query dependency (SeaQL/sea-schema#41) * Set `DATABASE_URL` environment variable
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use async_trait::async_trait;
|
|
use entity::sea_orm;
|
|
use sea_orm::ConnectOptions;
|
|
use sea_orm_rocket::{rocket::figment::Figment, Config, Database};
|
|
use std::time::Duration;
|
|
|
|
#[derive(Database, Debug)]
|
|
#[database("sea_orm")]
|
|
pub struct Db(SeaOrmPool);
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SeaOrmPool {
|
|
pub conn: sea_orm::DatabaseConnection,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl sea_orm_rocket::Pool for SeaOrmPool {
|
|
type Error = sea_orm::DbErr;
|
|
|
|
type Connection = sea_orm::DatabaseConnection;
|
|
|
|
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
|
|
let config = figment.extract::<Config>().unwrap();
|
|
let mut options: ConnectOptions = config.url.into();
|
|
options
|
|
.max_connections(config.max_connections as u32)
|
|
.min_connections(config.min_connections.unwrap_or_default())
|
|
.connect_timeout(Duration::from_secs(config.connect_timeout));
|
|
if let Some(idle_timeout) = config.idle_timeout {
|
|
options.idle_timeout(Duration::from_secs(idle_timeout));
|
|
}
|
|
let conn = sea_orm::Database::connect(options).await?;
|
|
|
|
Ok(SeaOrmPool { conn })
|
|
}
|
|
|
|
fn borrow(&self) -> &Self::Connection {
|
|
&self.conn
|
|
}
|
|
}
|