SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.94k stars 483 forks source link

Support connect database from db handler. #466

Closed ltoddy closed 2 years ago

ltoddy commented 2 years ago

e.g.

use async_mysql::DBClient

impl From<DBClient> for sea_orm::DatabaseConnection {}
billy1624 commented 2 years ago

Hey @ltoddy, is there any special reason for you to use mysql_async instead of sqlx?

ltoddy commented 2 years ago

No. Sometimes, the database used by developer is a cloud database. For example, mysql. The database client actually connects to the proxy, not the database itself . In other words, connecting to the database is through service discovery not IP address. Therefore, cloud service providers provide SDKs for service discovery, and SDK return the db handler.

ltoddy commented 2 years ago

其实意思就是,使用云服务商提供的数据库,是通过服务发现的方式来连接数据库的,用户是不知道数据库真是的ip地址的。 使用云服务商提供的sdk,只能得到一个db handler,比如他们基于async_mysql库自己做了一个库,支持上了服务发现的方式去连接。 这样子,就无法使用sea-orm了。 所以,如果sea-orm支持了通过一个数据库连接,来得到sea-orm的DBConnection对象,这样子就可以使用sea-orm来连接云数据库了。

billy1624 commented 2 years ago

Take Planetscale as an example, you have no fixed ip address access to the cloud db. But you can connect to it with a connection string mysql://jp388djdawnod:pscale_pw_sVkiE_m1111111111111111@gxvhbzcxfw01.us-east-2.psdb.cloud/<DATABASE_NAME>

REF: https://docs.planetscale.com/tutorials/connect-any-application

ltoddy commented 2 years ago

Your example uses the MySQL connection protocol header . But In my life, dsn like: "sd@/".

billy1624 commented 2 years ago

I guess you have to specify the db protocol at the front, db_protocal://sd@<service_name>/<db_name>

ltoddy commented 2 years ago

Not in front. The process of parsing the protocol is:

  1. service discovery read the DSN which like sd@<service_name>/<db_name>
  2. client send request to service discovery center by to get database proxy ip address
  3. client establish connection with database proxy.
  4. client send sql to db proxy, and then proxy send sql to db to execute it.
billy1624 commented 2 years ago

Seems you can perform the first two steps without sea-orm?

  1. service discovery read the DSN which like sd@<service_name>/<db_name>
  2. client send request to service discovery center by to get database proxy ip address

After you get the concrete IP then you can initialize the connection with sea-orm. e.g. protocol://username:password@host/database

  1. client establish connection with database proxy.
  2. client send sql to db proxy, and then proxy send sql to db to execute it.

Please correct me if I'm wrong

tyt2y3 commented 2 years ago

I think the best way to move forward is for you to request a support for SeaORM from THEM. Then instruct them to contact us. We might be able to work something out together as a commercial offering. Otherwise, we will not be able to support a cloud vendor's proprietary solution.