SQLx Utils
SQLx Utils provides a comprehensive set of utilities for working with the SQLx library in a structured and efficient way. It simplifies database interactions through type-safe filters, repository patterns, and powerful batch operations.
Features
- Type-safe SQL Filters: Define reusable filter structs with SQL-like syntax
- Repository Pattern: Implement CRUD operations with minimal boilerplate
- Transaction Support: Execute operations within transactions for data consistency
- Batch Operations: Efficiently process large datasets in chunks
- Connection Pool Management: Simplified access to database pools
- Multi-database Support: Works with SQLx's supported database backends
- Comprehensive Tracing: Built-in instrumentation for debugging and monitoring
Installation
Add SQLx Utils to your Cargo.toml
:
[]
= "1.1.1"
By default, the crate enables the any
database feature. To use a specific database:
[]
= { = "1.1.0", = false, = ["postgres"] }
Available database features:
any
(default): Works with any SQLx-supported databasepostgres
: PostgreSQL specificmysql
: MySQL specificsqlite
: SQLite specific
Quick Start
Setting up the Connection Pool
use *;
async
Defining a Filter
use *;
sql_filter!
// Usage:
let filter = new
.id
.name;
Creating a Repository
use *;
// 1. Define your model
// 2. Create a repository
repository!
// 3. Implement operations
repository_insert!
repository_update!
repository_delete!
Using the Repository
use *;
async
Working with Transactions
async
Advanced Usage
Filter Composition
let admin_filter = new.role;
let active_filter = new.status;
// Combine filters
let active_admins = admin_filter.and;
Batch Operations
// Process users in batches of 100
let users: = get_many_users;
USER_REPO..await?;
Custom Repository Methods
Implementation Notes
- Static Repositories: The
repository!
macro creates a static instance usingLazyLock
, accessible via the uppercase name (e.g.,USER_REPO
). - Zero-sized Type (ZST) Repositories: By adding
!zst
to the repository macro, you can create repositories with zero runtime cost. - Debugging Filters: Enable the
filter_debug_impl
feature to automatically implementDebug
for all generated filters. - Error Logging: The
log_err
feature adds error logging to all repository operations. - Insert with IDs: The
insert_duplicate
feature allows inserting records with existing IDs.
Available Repository Traits
SQLx Utils provides several repository traits that can be implemented for your models:
- Repository: Base trait for all repositories
- InsertableRepository: For inserting new records
- UpdatableRepository: For updating existing records
- SaveRepository: For intelligently inserting or updating based on ID presence
- DeleteRepository: For removing records
- SelectRepository: For querying records
- FilterRepository: For querying with type-safe filters
- TransactionRepository: For working with transactions
Development Status
SQLx Utils is in active development. The API may evolve between minor versions as I refine the interface based on user feedback.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.