Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add proxy connection type #1881

Merged
merged 54 commits into from Nov 19, 2023
Merged

feat: Add proxy connection type #1881

merged 54 commits into from Nov 19, 2023

Conversation

langyo
Copy link
Contributor

@langyo langyo commented Sep 27, 2023

PR Info

New Features

  • Add the proxy connection type.

Changes

  • Add a new feature called proxy.
  • GIve a demo to help us create a proxy connection quickly.

@langyo langyo marked this pull request as ready for review October 9, 2023 09:13
@langyo
Copy link
Contributor Author

langyo commented Oct 9, 2023

Although the test samples and example code are not rich enough, I think it is time to start reviewing the code.

If it can be merged, I'll do some additional work, such as adding CI configuration and writing more examples.

@langyo
Copy link
Contributor Author

langyo commented Oct 9, 2023

cc @tyt2y3 😉

uuid = { version = "1", default-features = false, optional = true }
ouroboros = { version = "0.17", default-features = false }
url = { version = "2.2", default-features = false }
thiserror = { version = "1", default-features = false }

[target.'cfg(not(wasm))'.dependencies]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This position temporarily disables sqlx because rustls, the upstream dependency of sqlx, cannot compile to any wasm target for the time being. The mainly cause is that rustls-dependent rings need to cross-compile code from C language.

Recently, ring may support compilation of the wasm platform by compiling old cipher suites with BoringC instead, but this will take a long time.

Copy link
Contributor Author

@langyo langyo Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langyo
Copy link
Contributor Author

langyo commented Oct 23, 2023

Now it's ready for review.

Here are three examples that utilize a proxy connection.

The example that utilizes SurrealDB might be able to enhance its robustness by employing the SQL parser, but it doesn't actually matter.

The other example, which uses Wasmtime, relies on the unstable WASI preview support, so its ABI may change in the future.

The third example, which employs GlueSQL, is currently using the legacy version (0.13) to avoid conflicts with the chrono library. Once the conflict is resolved, we can upgrade to the latest version (0.14).

@tyt2y3 😉

@langyo
Copy link
Contributor Author

langyo commented Oct 28, 2023

I've written a demo for my own project that uses this fork branch, take a look.

@tyt2y3
Copy link
Member

tyt2y3 commented Nov 8, 2023

That's very interesting indeed. Can you explain how to implement a ProxyDb?

celestia-island/tairitsu@f819768#diff-0f845934140e261f65d6abe821a10b9c11c6f84b68d9ab05aa3e81325dfa2fbb

And indeed how does GlueSQL come into play? How many features of SeaORM is usuable with GlueSQL?

I think it'd be better if we include an example implementation of ProxyDb as well.

@langyo
Copy link
Contributor Author

langyo commented Nov 9, 2023

That's very interesting indeed. Can you explain how to implement a ProxyDb?

celestia-island/tairitsu@f819768#diff-0f845934140e261f65d6abe821a10b9c11c6f84b68d9ab05aa3e81325dfa2fbb

And indeed how does GlueSQL come into play? How many features of SeaORM is usuable with GlueSQL?

I think it'd be better if we include an example implementation of ProxyDb as well.

The best practices for this are all included in the examples I attached to this fork:

https://github.com/langyo/sea-orm/tree/proxy-conn/examples/proxy_gluesql_example

https://github.com/langyo/sea-orm/tree/proxy-conn/examples/proxy_surrealdb_example

https://github.com/langyo/sea-orm/tree/proxy-conn/examples/proxy_wasmtime_example

It was already done two weeks ago...

To implement a basic proxy database, you only need to prepare a class that implements sea_orm::ProxyDatabaseTrait, and at least implement the query and execute functions. When creating a database later, use the sea_orm::Database::connect_proxy function instead of sea_orm::Database::connect function creation.

By the way, don’t forget to enable the proxy feature.

Here is an example:

struct ProxyDb {}

impl ProxyDatabaseTrait for ProxyDb {
    fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> {
        // ...

        Ok(vec![])
    }

    fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
        // ...

        Ok(ProxyExecResult {
            last_insert_id: 1,
            rows_affected: 1,
        })
    }
}

fn main() -> anyhow::Result<()> {
    let db = Database::connect_proxy(
        DbBackend::Sqlite,
        Arc::new(Mutex::new(Box::new(ProxyDb { }))),
    )
    .await?;

    Ok(())
}

For database engines that are not adapted to the protocols of MySQL, PostgreSQL and Sqlite (such as GlueSQL and SurrealDB), it is necessary to manually re-parse and construct SQL statements. After all, the SQL statements produced by SQLX cannot be used directly...

I am actually planning to introduce a new mechanism to be able to choose other database back-end engines besides SQLX, which includes constructing one on site, but this obviously should be solved by opening another PR.

So...I would do this first. I will implement the feature of the proxy database first, and I will open a new issue to implement the rest later. The current examples for SurrealDB and GlueSQL focus on demonstrating the capabilities of using proxy databases, rather than putting them into production immediately.

@tyt2y3
Copy link
Member

tyt2y3 commented Nov 15, 2023

Thank you for your explanation! I will read again and then probably merge it as is, if you wouldn't mind becoming the PIC of this feature. TBH I am not 100% clear of your big picture, but I am sure you are making something interesting and great! Let's see how it goes.

@langyo
Copy link
Contributor Author

langyo commented Nov 16, 2023

Thank you for your explanation! I will read again and then probably merge it as is, if you wouldn't mind becoming the PIC of this feature. TBH I am not 100% clear of your big picture, but I am sure you are making something interesting and great! Let's see how it goes.

Certainly! I appreciate your trust, and I'm glad to continue maintaining the feature if I have free time.

@tyt2y3 tyt2y3 changed the base branch from master to proxy November 19, 2023 12:49
@tyt2y3 tyt2y3 merged commit 2844d03 into SeaQL:proxy Nov 19, 2023
34 checks passed
@tyt2y3
Copy link
Member

tyt2y3 commented Nov 19, 2023

I will make a few edits on the proxy branch

Copy link

🎉 Released In 0.12.10 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants