Generalize over MySQL and Postgres
This commit is contained in:
parent
380f2a0c04
commit
1126aae479
16
Cargo.toml
16
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"]
|
||||
|
@ -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"]
|
||||
|
@ -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<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T, E>;
|
||||
@ -37,7 +34,7 @@ async fn create(
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn list(conn: Connection<Db>) -> Result<Json<Vec<i64>>> {
|
||||
async fn list(conn: Connection<Db>) -> Result<Json<Vec<i32>>> {
|
||||
let ids = Post::find()
|
||||
.all(&conn)
|
||||
.await
|
||||
@ -63,7 +60,7 @@ async fn read(conn: Connection<Db>, id: i64) -> Option<Json<post::Model>> {
|
||||
}
|
||||
|
||||
#[delete("/<id>")]
|
||||
async fn delete(conn: Connection<Db>, id: i64) -> Result<Option<()>> {
|
||||
async fn delete(conn: Connection<Db>, id: i32) -> Result<Option<()>> {
|
||||
let post: post::ActiveModel = Post::find_by_id(id)
|
||||
.one(&conn)
|
||||
.await
|
||||
@ -82,10 +79,14 @@ async fn destroy(conn: Connection<Db>) -> Result<()> {
|
||||
}
|
||||
|
||||
async fn run_migrations(rocket: Rocket<Build>) -> 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)
|
||||
}
|
||||
|
||||
|
@ -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<i64>,
|
||||
pub id: Option<i32>,
|
||||
pub title: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
24
src/driver/rocket_db.rs
Normal file
24
src/driver/rocket_db.rs
Normal file
@ -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<Self, Self::Error> {
|
||||
Ok(crate::Database {})
|
||||
}
|
||||
|
||||
async fn get(&self) -> Result<Self::Connection, Self::Error> {
|
||||
#[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())
|
||||
}
|
||||
}
|
@ -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<Self, Self::Error> {
|
||||
// let config = figment.extract::<Config>()?;
|
||||
// let mut opts = config.url.parse::<Options<D>>().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::Connection, Self::Error> {
|
||||
// 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(),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user