diff --git a/Cargo.toml b/Cargo.toml index bc90db55..2313f891 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,5 @@ [workspace] -members = [ - ".", - "sea-orm-macros", - "sea-orm-codegen", -] +members = [".", "sea-orm-macros", "sea-orm-codegen"] [package] name = "sea-orm" @@ -40,8 +36,12 @@ sqlx-core = { version = "^0.5", optional = true } sqlx-macros = { version = "^0.5", optional = true } serde_json = { version = "^1", optional = true } uuid = { version = "0.8", features = ["serde", "v4"], optional = true } -rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["json"], optional = true } -rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["sqlx_mysql"], optional = true } +rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = [ + "json", +], optional = true } +rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = [ + "sqlx_mysql", +], optional = true } [dev-dependencies] smol = { version = "^1.2" } @@ -93,4 +93,4 @@ runtime-actix-rustls = ["sqlx/runtime-actix-rustls", "runtime-actix"] runtime-tokio = [] runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "runtime-tokio"] runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "runtime-tokio"] -rocket-mysql = ["rocket", "rocket_db_pools"] +rocket-db = ["rocket", "rocket_db_pools"] diff --git a/examples/rocket_example/Cargo.toml b/examples/rocket_example/Cargo.toml index feb77d43..d9f5d738 100644 --- a/examples/rocket_example/Cargo.toml +++ b/examples/rocket_example/Cargo.toml @@ -5,9 +5,11 @@ edition = "2018" publish = false [workspace] [dependencies] -rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["json"] } -rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = ["sqlx_mysql"] } -sea-orm = { path = "../../", features = ["sqlx-all", "rocket-mysql"] } +rocket = { git = "https://github.com/SergioBenitez/Rocket.git", features = [ + "json", +] } +rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket.git", features = [] } +sea-orm = { path = "../../", features = ["rocket-db"] } sea-query = { version = "^0.12.8" } serde_json = { version = "^1" } @@ -19,3 +21,8 @@ futures-util = { version = "^0.3" } version = "0.5.1" default-features = false features = ["macros", "offline", "migrate"] + +[features] +default = ["sqlx-postgres"] +sqlx-postgres = ["sea-orm/sqlx-postgres"] +sqlx-mysql = ["sea-orm/sqlx-mysql"] diff --git a/examples/rocket_example/src/sqlx/mod.rs b/examples/rocket_example/src/sqlx/mod.rs index 48fe8ab3..1b43ea67 100644 --- a/examples/rocket_example/src/sqlx/mod.rs +++ b/examples/rocket_example/src/sqlx/mod.rs @@ -1,17 +1,14 @@ use rocket::fairing::{self, AdHoc}; use rocket::response::status::Created; use rocket::serde::json::Json; -use rocket::{futures, Build, Rocket}; - +use rocket::{Build, Rocket}; use rocket_db_pools::{sqlx, Connection, Database}; - use sea_orm::entity::*; -use sea_orm::{DatabaseBackend, Statement}; mod setup; #[derive(Database, Debug)] -#[database("blog")] +#[database("rocket_example")] struct Db(sea_orm::Database); type Result> = std::result::Result; @@ -37,7 +34,7 @@ async fn create( } #[get("/")] -async fn list(conn: Connection) -> Result>> { +async fn list(conn: Connection) -> Result>> { let ids = Post::find() .all(&conn) .await @@ -63,7 +60,7 @@ async fn read(conn: Connection, id: i64) -> Option> { } #[delete("/")] -async fn delete(conn: Connection, id: i64) -> Result> { +async fn delete(conn: Connection, id: i32) -> Result> { let post: post::ActiveModel = Post::find_by_id(id) .one(&conn) .await @@ -82,10 +79,14 @@ async fn destroy(conn: Connection) -> Result<()> { } async fn run_migrations(rocket: Rocket) -> fairing::Result { - let conn = sea_orm::Database::connect("mysql://root:@localhost/rocket_example") - .await - .unwrap(); - let create_post_table = setup::create_post_table(&conn); + #[cfg(feature = "sqlx-mysql")] + let db_url = "mysql://root:@localhost/rocket_example"; + #[cfg(feature = "sqlx-postgres")] + let db_url = "postgres://root:root@localhost/rocket_example"; + + let conn = sea_orm::Database::connect(db_url).await.unwrap(); + + let _create_post_table = setup::create_post_table(&conn).await; Ok(rocket) } diff --git a/examples/rocket_example/src/sqlx/post.rs b/examples/rocket_example/src/sqlx/post.rs index 05ec80fe..bfba82aa 100644 --- a/examples/rocket_example/src/sqlx/post.rs +++ b/examples/rocket_example/src/sqlx/post.rs @@ -15,7 +15,7 @@ impl EntityName for Entity { #[serde(crate = "rocket::serde")] pub struct Model { #[serde(skip_deserializing, skip_serializing_if = "Option::is_none")] - pub id: Option, + pub id: Option, pub title: String, pub text: String, } diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 5786d299..9b30e6d4 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -9,13 +9,8 @@ mod sqlx_postgres; #[cfg(feature = "sqlx-sqlite")] mod sqlx_sqlite; -#[cfg(feature = "rocket-mysql")] -mod rocket_mysql; - #[cfg(feature = "mock")] pub use mock::*; -#[cfg(feature = "rocket-mysql")] -pub use rocket_mysql::*; #[cfg(feature = "sqlx-dep")] pub use sqlx_common::*; #[cfg(feature = "sqlx-mysql")] @@ -24,3 +19,8 @@ pub use sqlx_mysql::*; pub use sqlx_postgres::*; #[cfg(feature = "sqlx-sqlite")] pub use sqlx_sqlite::*; + +#[cfg(feature = "rocket-db")] +mod rocket_db; +#[cfg(feature = "rocket-db")] +pub use rocket_db::*; diff --git a/src/driver/rocket_db.rs b/src/driver/rocket_db.rs new file mode 100644 index 00000000..512f1fad --- /dev/null +++ b/src/driver/rocket_db.rs @@ -0,0 +1,24 @@ +use rocket::figment::Figment; +use rocket_db_pools::{Config, Error}; + +#[rocket::async_trait] +impl rocket_db_pools::Pool for crate::Database { + type Error = crate::DbErr; + + type Connection = crate::DatabaseConnection; + + async fn init(figment: &Figment) -> Result { + Ok(crate::Database {}) + } + + async fn get(&self) -> Result { + #[cfg(feature = "sqlx-mysql")] + let db_url = "mysql://root:@localhost/rocket_example"; + #[cfg(feature = "sqlx-postgres")] + let db_url = "postgres://root:root@localhost/rocket_example"; + + println!("db_url: {:#?}", db_url); + + Ok(crate::Database::connect(db_url).await.unwrap()) + } +} diff --git a/src/driver/rocket_mysql.rs b/src/driver/rocket_mysql.rs deleted file mode 100644 index ef90f1ee..00000000 --- a/src/driver/rocket_mysql.rs +++ /dev/null @@ -1,39 +0,0 @@ -use rocket::figment::Figment; -use rocket_db_pools::{Config, Error}; - -#[rocket::async_trait] -impl rocket_db_pools::Pool for crate::Database { - type Error = crate::DbErr; - - type Connection = crate::DatabaseConnection; - - async fn init(figment: &Figment) -> Result { - // let config = figment.extract::()?; - // let mut opts = config.url.parse::>().map_err(Error::Init)?; - // opts.disable_statement_logging(); - // specialize(&mut opts, &config); - - // sqlx::pool::PoolOptions::new() - // .max_connections(config.max_connections as u32) - // .connect_timeout(Duration::from_secs(config.connect_timeout)) - // .idle_timeout(config.idle_timeout.map(Duration::from_secs)) - // .min_connections(config.min_connections.unwrap_or_default()) - // .connect_with(opts) - // .await - // .map_err(Error::Init) - Ok(crate::Database {}) - } - - async fn get(&self) -> Result { - // self.acquire().await.map_err(Error::Get) - // let con = crate::Database::connect("sqlite::memory:").await; - - // Ok(crate::Database::connect("sqlite::memory:").await.unwrap()) - // "mysql://root:@localhost" - Ok( - crate::Database::connect("mysql://root:@localhost/rocket_example") - .await - .unwrap(), - ) - } -}