sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Support creating a `sea_orm::Database` from any `sqlx::Executor` for example from an `sqlx::any::AnyPool`.

Open 05storm26 opened this issue 4 years ago • 2 comments

Currently sqlx allows you to create a connection pool with custom pool options based on e.g.: the kind of database (extracted from the url) or anything else.

e.g.:

    let mut connect_options = sqlx::any::AnyConnectOptions::from_str(&database_url).expect("Incorrect database url!");

    let mut pool_options = sqlx::any::AnyPoolOptions::new();

    if std::matches!(connect_options.kind(), AnyKind::Sqlite) {
        let sqlite_connect_options = SqliteConnectOptions::from_str(&database_url).unwrap()
            .create_if_missing(true);
        connect_options = sqlite_connect_options.into();
        pool_options = pool_options.max_connections(1);
    }

    let kind = connect_options.kind();
    log::info!("Connecting to the database...");
    let mut pool = pool_options.connect_with(connect_options).await.expect("Unable to connect to the database!");

(I know that the sqlite create_if_missing option could be configured from the database url as well).

The user should have the ability to create and setup their own connection using sqlx. SeaQL should allow creating sea_orm::Database not just from a database url string but also from any sqlx::Executor.

I understand that SeaQL needs to know the kind of database to know which flavor of SQL to generate, but

  1. This could be something that we could allow the user to specify along with giving an sqlx::Executor.
  2. In the next release sqlx::AnyPool will have an any_kind method for getting the database kind, so that will be usefull and can be used to get the underlying database type.

05storm26 avatar Dec 22 '21 22:12 05storm26

Hey @05storm26, I wonder what is the use case for you to convert any sqlx::Executor into sea_orm::Database?

billy1624 avatar Dec 29 '21 14:12 billy1624

Seems Pool::any_kind() have been released today. https://github.com/launchbadge/sqlx/releases/tag/v0.5.10

billy1624 avatar Dec 30 '21 02:12 billy1624

SeaORM will not be able to support AnyPool

tyt2y3 avatar Feb 21 '23 10:02 tyt2y3