44
55----
66
7- A middleware which holds a pool of postgres connections, and automatically hands
8- each [ tide::Request] [ ] a connection, which may transparently be either a postgres transaction,
9- or a direct pooled connection.
7+ A [ Tide ] [ ] middleware which holds a pool of SQLx database connections, and automatically hands
8+ each [ tide::Request] [ ] a connection, which may transparently be either a database transaction,
9+ or a direct pooled database connection.
1010
11- By default, transactions are used for all http methods other than GET and HEAD.
11+ By default, transactions are used for all http methods other than ` GET ` and ` HEAD ` .
1212
13- When using this, use the ` PostgresRequestExt ` extenstion trait to get the connection.
13+ When using this, use the ` SQLxRequestExt ` extenstion trait to get the connection.
1414
15- ## Example
15+ ## Examples
1616
17+ ### Basic
1718``` rust
1819#[async_std:: main]
1920async fn main () -> anyhow :: Result <()> {
2021 use sqlx :: Acquire ; // Or sqlx::prelude::*;
22+ use sqlx :: postgres :: Postgres ;
2123
22- use tide_sqlx :: PostgresConnectionMiddleware ;
23- use tide_sqlx :: PostgresRequestExt ;
24+ use tide_sqlx :: SQLxMiddleware ;
25+ use tide_sqlx :: SQLxRequestExt ;
2426
2527 let mut app = tide :: new ();
26- app . with (PostgresConnectionMiddleware :: new (" postgres://localhost/geolocality " , 5 ). await ? );
28+ app . with (SQLxMiddleware :: < Postgres > :: new (" postgres://localhost/a_database " ). await ? );
2729
2830 app . at (" /" ). post (| req : tide :: Request <()>| async move {
29- let mut pg_conn = req . postgres_conn (). await ;
31+ let mut pg_conn = req . sqlx_conn :: < Postgres > (). await ;
3032
31- pg_conn . acquire (). await ? ; // Pass this to e.g. "fetch_optional()" from a sqlx::Query
33+ sqlx :: query (" SELECT * FROM users" )
34+ . fetch_optional (pg_conn . acquire (). await ? )
35+ . await ;
36+
37+ Ok ("" )
38+ });
39+ Ok (())
40+ }
41+ ```
42+
43+ ### From sqlx ` PoolOptions ` and with ` ConnectOptions `
44+ ``` rust
45+ #[async_std:: main]
46+ async fn main () -> anyhow :: Result <()> {
47+ use log :: LevelFilter ;
48+ use sqlx :: {Acquire , ConnectOptions }; // Or sqlx::prelude::*;
49+ use sqlx :: postgres :: {PgConnectOptions , PgPoolOptions , Postgres };
50+
51+ use tide_sqlx :: SQLxMiddleware ;
52+ use tide_sqlx :: SQLxRequestExt ;
53+
54+ let mut connect_opts = PgConnectOptions :: new ();
55+ connect_opts . log_statements (LevelFilter :: Debug );
56+
57+ let pg_pool = PgPoolOptions :: new ()
58+ . max_connections (5 )
59+ . connect_with (connect_opts )
60+ . await ? ;
61+
62+ let mut app = tide :: new ();
63+ app . with (SQLxMiddleware :: from (pg_pool ));
64+
65+ app . at (" /" ). post (| req : tide :: Request <()>| async move {
66+ let mut pg_conn = req . sqlx_conn :: <Postgres >(). await ;
67+
68+ sqlx :: query (" SELECT * FROM users" )
69+ . fetch_optional (pg_conn . acquire (). await ? )
70+ . await ;
3271
3372 Ok ("" )
3473 });
@@ -38,12 +77,11 @@ async fn main() -> anyhow::Result<()> {
3877
3978## Why you may want to use this
4079
41- Postgres transactions are very useful because they allow easy, assured rollback if something goes wrong.
80+ Database transactions are very useful because they allow easy, assured rollback if something goes wrong.
4281However, transactions incur extra runtime cost which is too expensive to justify for READ operations that _ do not need_ this behavior.
4382
4483In order to allow transactions to be used seamlessly in endpoints, this middleware manages a transaction if one is deemed desirable.
4584
46-
4785## License
4886
4987Licensed under the [ BlueOak Model License 1.0.0] ( LICENSE.md ) — _ [ Contributions via DCO 1.1] ( contributing.md#developers-certificate-of-origin ) _
0 commit comments