zirv-sqlx
zirv-sqlx is a lightweight convenience wrapper for SQLx that simplifies global database pool management and transaction handling in Rust.
This library provides a set of easy-to-use macros for:
- Initializing and retrieving a global database pool.
- Handling transactions with minimal boilerplate.
The internal implementation leverages the functionality in src/db.rs
for pool initialization and retrieval and exposes a friendly macro API in src/lib.rs
.
Features
-
Global Database Pool Management:
-
init_db_pool!()
Initializes the global SQLx database pool asynchronously. Under the hood, it callsdb::init_db_pool
to set up a connection pool based on configuration values (like the database URL and maximum connections). -
get_db_pool!()
Retrieves a reference to the globally initialized database pool. This macro wraps a call todb::get_db_pool
and will panic if the pool has not yet been initialized.
-
-
Transaction Helpers:
-
start_transaction!()
Begins a new transaction using the global pool. If starting the transaction fails, the error is logged and returned. -
commit_transaction!()
Commits an active transaction. If the commit fails, the error is logged and returned. -
rollback_transaction!()
Rolls back an active transaction. If the rollback fails, the error is logged and returned.
-
Using these macros helps standardize your database operations and reduces repetitive code when integrating with SQLx.
Installation
Add zirv-sqlx as a dependency in your project's Cargo.toml
:
Usage
Example: Setting Up and Using the Database Pool
// Import the macros
use register_config;
use ;
// Define a configuration struct with default values
async
Example: Error Handling with Transactions
use ;
use MySqlPool;
async
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have ideas for improvements or new features.
License
This project is licensed under the MIT License. See the LICENSE file for details.