This commit is contained in:
Chris Tsang 2021-10-12 03:23:21 +08:00
parent 4a1b8fabc5
commit 8feca6be7b
4 changed files with 14 additions and 9 deletions

View File

@ -27,7 +27,8 @@ features = ["macros", "runtime-tokio-native-tls"]
default-features = false default-features = false
[dependencies.sea-orm-rocket] [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] [features]
default = ["sqlx-postgres"] default = ["sqlx-postgres"]

View File

@ -5,7 +5,7 @@ use sea_orm_rocket::{rocket::figment::Figment, Config, Database};
#[database("sea_orm")] #[database("sea_orm")]
pub struct Db(SeaOrmPool); pub struct Db(SeaOrmPool);
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct SeaOrmPool { pub struct SeaOrmPool {
pub conn: sea_orm::DatabaseConnection, pub conn: sea_orm::DatabaseConnection,
} }

View File

@ -192,10 +192,6 @@ impl<D: Database> Initializer<D> {
impl<'a, D: Database> Connection<'a, D> { impl<'a, D: Database> Connection<'a, D> {
/// Returns the internal connection value. See the [`Connection` Deref /// Returns the internal connection value. See the [`Connection` Deref
/// column](crate#supported-drivers) for the expected type of this value. /// column](crate#supported-drivers) for the expected type of this value.
///
/// Note that `Connection<D>` 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 <D::Pool as Pool>::Connection { pub fn into_inner(self) -> &'a <D::Pool as Pool>::Connection {
self.0 self.0
} }

View File

@ -5,13 +5,21 @@ use rocket::figment::Figment;
/// This trait provides a generic interface to various database pooling /// This trait provides a generic interface to various database pooling
/// implementations in the Rust ecosystem. It can be implemented by anyone, but /// implementations in the Rust ecosystem. It can be implemented by anyone, but
/// this crate provides implementations for common drivers. /// 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] #[rocket::async_trait]
pub trait Pool: Sized + Send + Sync + 'static { 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; type Connection;
/// The error type returned by [`Self::init()`] and [`Self::get()`]. /// The error type returned by [`Self::init()`].
type Error: std::error::Error; type Error: std::error::Error;
/// Constructs a pool from a [Value](rocket::figment::value::Value). /// 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. /// insufficient resources, or another database-specific error.
async fn init(figment: &Figment) -> Result<Self, Self::Error>; async fn init(figment: &Figment) -> Result<Self, Self::Error>;
/// Borrow the inner connection /// Borrow a database connection
fn borrow(&self) -> &Self::Connection; fn borrow(&self) -> &Self::Connection;
} }