Connecting
A Database is the connection pool for one database. Connect once, at startup, and share the value with the routes, for example in Rocket's managed state or behind an Arc. A Database is not Clone.
Connect
Pass the connection URL to Database::connect. It has the form mysql://user:password@host/database:
use orbit_orm::Database;
let db = Database::connect("mysql://user:password@127.0.0.1/app").await?;
A URL that the server rejects, such as a wrong password, returns an Error. See Errors.
Ping
ping runs SELECT 1 and returns an error when the database does not answer. It is a simple way to check the connection at startup:
db.ping().await?;
The pool
The pool keeps up to 5 connections. A query that finds none free waits up to 10 seconds, then fails. These limits are fixed in this version, and there is no setting for them.
The raw pool
db.pool() gives the sqlx MySqlPool, for queries that Orbit does not wrap yet. Inside a transaction, tx.connection() gives the transaction's connection. See Transactions.
Close
close closes every connection in the pool:
db.close().await;
Orbit