From 8feca6be7b64f2bfbc0ae7af48e6d9b214073e07 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 12 Oct 2021 03:23:21 +0800 Subject: [PATCH] Edit --- examples/rocket_example/Cargo.toml | 3 ++- examples/rocket_example/src/pool.rs | 2 +- sea-orm-rocket/lib/src/database.rs | 4 ---- sea-orm-rocket/lib/src/pool.rs | 14 +++++++++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/rocket_example/Cargo.toml b/examples/rocket_example/Cargo.toml index c896fe2d..a4783cb0 100644 --- a/examples/rocket_example/Cargo.toml +++ b/examples/rocket_example/Cargo.toml @@ -27,7 +27,8 @@ features = ["macros", "runtime-tokio-native-tls"] default-features = false [dependencies.sea-orm-rocket] -path = "../../sea-orm-rocket/lib" +path = "../../sea-orm-rocket/lib" # remove this line in your own project +git = "https://github.com/SeaQL/sea-orm" [features] default = ["sqlx-postgres"] diff --git a/examples/rocket_example/src/pool.rs b/examples/rocket_example/src/pool.rs index 931a4712..afc8d48d 100644 --- a/examples/rocket_example/src/pool.rs +++ b/examples/rocket_example/src/pool.rs @@ -5,7 +5,7 @@ use sea_orm_rocket::{rocket::figment::Figment, Config, Database}; #[database("sea_orm")] pub struct Db(SeaOrmPool); -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SeaOrmPool { pub conn: sea_orm::DatabaseConnection, } diff --git a/sea-orm-rocket/lib/src/database.rs b/sea-orm-rocket/lib/src/database.rs index 6eb98385..b3095ac9 100644 --- a/sea-orm-rocket/lib/src/database.rs +++ b/sea-orm-rocket/lib/src/database.rs @@ -192,10 +192,6 @@ impl Initializer { impl<'a, D: Database> Connection<'a, D> { /// Returns the internal connection value. See the [`Connection` Deref /// column](crate#supported-drivers) for the expected type of this value. - /// - /// Note that `Connection` derefs to the internal connection type, so - /// using this method is likely unnecessary. See [deref](Connection#deref) - /// for examples. pub fn into_inner(self) -> &'a ::Connection { self.0 } diff --git a/sea-orm-rocket/lib/src/pool.rs b/sea-orm-rocket/lib/src/pool.rs index 9dd8b75a..cbe2bb85 100644 --- a/sea-orm-rocket/lib/src/pool.rs +++ b/sea-orm-rocket/lib/src/pool.rs @@ -5,13 +5,21 @@ use rocket::figment::Figment; /// This trait provides a generic interface to various database pooling /// implementations in the Rust ecosystem. It can be implemented by anyone, but /// this crate provides implementations for common drivers. +/// +/// This is adapted from the original `rocket_db_pools`. But on top we require +/// `Connection` itself to be `Sync`. Hence, instead of cloning or allocating +/// a new connection per request, here we only borrow a reference to the pool. +/// +/// In SeaORM, only *when* you are about to execute a SQL statement will a +/// connection be acquired from the pool, and returned as soon as the query finishes. +/// This helps a bit with concurrency if the lifecycle of a request is long enough. /// ``` #[rocket::async_trait] pub trait Pool: Sized + Send + Sync + 'static { - /// The connection type managed by this pool, returned by [`Self::get()`]. + /// The connection type managed by this pool. type Connection; - /// The error type returned by [`Self::init()`] and [`Self::get()`]. + /// The error type returned by [`Self::init()`]. type Error: std::error::Error; /// Constructs a pool from a [Value](rocket::figment::value::Value). @@ -28,7 +36,7 @@ pub trait Pool: Sized + Send + Sync + 'static { /// insufficient resources, or another database-specific error. async fn init(figment: &Figment) -> Result; - /// Borrow the inner connection + /// Borrow a database connection fn borrow(&self) -> &Self::Connection; }