This library is the Sea ORM middleware for Poem. This library is designed to make it easier for users to no longer need to manually begin transactions, or explicitly pass database connection parameters.
```rust use poemseaorm_middleware::{LocalKeyExt, TxnMiddleware, TXN};
// define entity
pub struct Model { #[seaorm(primarykey, auto_increment = false)] pub id: Uuid, pub name: String, }
async fn hello(Path(name): Path
let user = match Entity::find()
.filter(Column::Name.eq(name.clone()))
.one(txn)
.await
.unwrap()
{
Some(user) => user,
None => return format!("not found: {name}"),
};
format!("hello: {}", user.name)
}
async fn main() -> Result<(), std::io::Error> { // create database connection let db = Database::connect("mysql://root:123456@localhost:3306/db") .await .unwrap();
// create transaction middleware
let txn_middleware = TxnMiddleware::new(db);
let app = Route::new()
.at("/hello/:name", get(hello))
// add middleware
.with(txn_middleware);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
} ```
Check examples, to see the full example.