Rust Rocket - Obtaining database connection outside of controller

Viewed 275

Currently when using Rust Rocket framework it is necessary to get your database connection via the controller. Basically the connection is given to your handler from a pre-configured pool. Now we have to pass this connection down into any struct which needs a database connection.

If I would like to separate the concerns of reading from the data store, or potentially multiple data stores if caching is involved as well, then I will have to pass one, or potentially multiple, different connection structs from my handler into the lower layer.

While I am aware that I can encapsulate all of the connections into a single request guard, I am dissatisfied by lack of abstraction. I feel quite strongly that my handler should know nothing about the database in order to keep the concerns as separate as possible.

How would I proceed in Rust to obtain a connection from some shared pool of connections in an object, without usage of request guards and argument drilling?

Note: Terminology may be incorrect due to rather limited experience with Rocket

0 Answers
Related