Akita
🎯 Features
- 🚀 High Performance: Pure Rust implementation, zero runtime overhead
- 🎯 Easy to Use: Intuitive API, quick to learn
- 🔧 Flexible Query: Powerful query builder with type safety
- 📦 Multi-Database: Native support for MySQL, PostgreSQL, SQLite, Oracle, SQL Server, and any MySQL-compatible databases (TiDB, MariaDB, etc.)
- 🔌 Dual Runtime: Both synchronous and asynchronous operation modes
- 🛡️ Type Safe: Full Rust type system support with compile-time checking
- 🔄 Transaction: Complete ACID transaction management with savepoint support
- ⚡ Connection Pool: Built-in high-performance connection pooling (r2d2 for sync, deadpool for async)
- 🎨 Annotation Driven: Simplify entity definition with derive macros
- 🔌 Interceptors: Extensible interceptor system for AOP (Aspect-Oriented Programming)
- 📊 Pagination: Built-in smart pagination with total count, plus cursor-based pagination for large datasets
- 🔍 Complex Query: Support for joins, subqueries, and complex SQL operations
- 🛠️ Raw SQL: Direct SQL execution when needed
- 🔒 SQL Injection Protection: Built-in SQL injection detection and prevention
- 📝 Logical Delete: Automatic soft delete with interceptor support
- 🔄 Optimistic Locking: Version-based optimistic locking for concurrent updates
- 🏢 Multi-Tenancy: Built-in tenant isolation support
- ⏰ Auto Fill: Automatic field population (timestamps, UUIDs, etc.)
- 💾 Caching: Query result caching with pluggable cache providers
- 📈 Performance Monitoring: Slow query detection and performance metrics
- 🔗 Lambda Wrapper: Compile-time safe column references
- 📋 SubQuery Builder: Type-safe subquery construction
📦 Installation
Add this to your Cargo.toml:
For MySQL (Synchronous)
[]
= { = "0.6", = ["mysql-sync"] }
For MySQL (Asynchronous)
[]
= { = "0.6", = ["mysql-async"] }
For PostgreSQL:
[]
= { = "0.6", = ["postgres-sync"] }
For Oracle:
[]
= { = "0.6", = ["oracle-sync"] }
SqlServer:
[]
= { = "0.6", = ["mssql-sync"] }
For SQLite:
[]
= { = "0.6", = ["sqlite-sync"] }
For TiDB and MySQL-compatible Databases:
TiDB, MariaDB, and other MySQL-compatible databases can use the MySQL features:
[]
= { = "0.6", = ["mysql-sync"] } # or "mysql-async"
= "0.4"
🚀 Quick Start
1. Define Your Entity
use *;
use ;
use Value;
2. Initialize Akita
Synchronous Mode
use *;
use Duration;
Asynchronous Mode
use *;
use Duration;
async
3. Basic Operations
Synchronous Operations
Asynchronous Operations
⬆️Database Compatibility Matrix
| Database | Sync Feature | Async Feature | Protocol | Sync Implementation | Async Implementation | Status | Notes |
|---|---|---|---|---|---|---|---|
| MySQL | mysql-sync |
mysql-async |
MySQL | mysql crate |
mysql_async crate |
✅ Production Ready | Native Rust implementations |
| PostgreSQL | postgres-sync |
postgres-async |
PostgreSQL | tokio-postgres (blocking) |
tokio-postgres (async) |
✅ Production Ready | Both use tokio-postgres under the hood |
| SQLite | sqlite-sync |
sqlite-async |
SQLite | rusqlite crate |
sqlx with async runtime |
✅ Production Ready | Different implementation strategies |
| Oracle | oracle-sync |
oracle-async |
Oracle | oracle crate (blocking) |
oracle crate + async runtime |
✅ Production Ready | Oracle driver with async wrapper |
| SQL Server | mssql-sync |
mssql-async |
TDS | tiberius (blocking) |
tiberius (async) |
✅ Production Ready | Tiberius driver support |
| TiDB | mysql-sync |
mysql-async |
MySQL | Same as MySQL | Same as MySQL | ✅ Production Ready | 100% MySQL compatible |
| MariaDB | mysql-sync |
mysql-async |
MySQL | Same as MySQL | Same as MySQL | ✅ Production Ready | 100% MySQL compatible |
| OceanBase | mysql-sync |
mysql-async |
MySQL | Same as MySQL | Same as MySQL | ✅ Production Ready | MySQL compatible mode |
Implementation Details Summary
PostgreSQL Implementation
- Sync: Uses
tokio-postgreswith blocking wrapper - Async: Direct
tokio-postgresasync client - Both share same underlying library
Oracle Implementation
- Sync: Native
oraclecrate (synchronous driver) - Async:
oraclecrate wrapped with async runtime - Same driver, different execution model
SQLite Implementation
- Sync:
rusqlitecrate (synchronous SQLite) - Async:
sqlxwith async SQLite support - Different libraries, same protocol
SQL Server Implementation
- Sync:
tiberiuswith blocking API - Async:
tiberiusnative async API - Same library, different APIs
Feature Comparison
| Feature | MySQL/TiDB | PostgreSQL | SQLite | Oracle | SQL Server |
|---|---|---|---|---|---|
| ACID Transactions | ✅ | ✅ | ✅ | ✅ | ✅ |
| Connection Pool | ✅ | ✅ | ✅ | ✅ | ✅ |
| Native Async | ✅ | ✅ | ⚠️ (via sqlx) | ⚠️ (wrapped) | ✅ |
| Sync via Async Runtime | ❌ | ✅ (blocking) | ❌ | ❌ | ✅ (blocking) |
| JSON Support | ✅ (JSON) | ✅ (JSONB) | ✅ (JSON1) | ✅ | ⚠️ (limited) |
| Full-text Search | ✅ | ✅ | ✅ (FTS5) | ✅ | ✅ |
| Spatial Data | ✅ | ✅ | ✅ (R*Tree) | ✅ | ✅ |
| Stored Procedures | ✅ | ✅ | ❌ | ✅ | ✅ |
| Replication | ✅ | ✅ | ❌ | ✅ | ✅ |
| Distributed | TiDB ✅ | ❌ | ❌ | ❌ | ❌ |
| Protocol | MySQL | PostgreSQL | SQLite | Oracle | TDS |
Key Implementation Notes
- PostgreSQL: Both sync and async use
tokio-postgres, sync is just a blocking wrapper - Oracle: Sync is native driver, async is wrapper around same driver
- SQLite: Different libraries for sync (
rusqlite) and async (sqlx) - SQL Server:
tiberiusprovides both sync (blocking) and async APIs - MySQL: Separate sync (
mysql) and async (mysql_async) crates - TiDB/MariaDB/OceanBase: Use MySQL drivers with full compatibility
📚 Detailed Usage
Query Builder
Akita provides a powerful, type-safe query builder that works across all supported databases:
Complex Queries with Database Optimizations
Database-Specific Features
MySQL/TiDB Specific Features
PostgreSQL Specific Features
SQLite Specific Features
Raw SQL Queries with Database Portability
Transactions with Database-Specific Features
Interceptors with Database Awareness
Akita supports powerful interceptor system that can adapt to different databases:
use *;
use Arc;
use Duration;
// Custom interceptor
Built-in Interceptors
Akita provides several built-in interceptors for common use cases:
Field Fill Interceptor
Automatically populates fields like create_time, update_time, create_by, etc.
use ;
let interceptor = new
.with_handler
.with_handler
.with_handler;
Soft Delete Interceptor
Automatically rewrites DELETE statements to UPDATE with a deleted flag.
use ;
let config = default
.with_column
.ignore_table;
let interceptor = new;
Pagination Interceptor
Automatically adds pagination to SELECT queries.
use ;
let interceptor = new
.with_default_page_size
.with_max_page_size;
// In your code:
ctx.set_metadata;
Optimistic Lock Interceptor
Automatically adds version checks to UPDATE statements.
use ;
let config = default
.with_column
.ignore_table;
let interceptor = new;
Tenant Line Interceptor
Automatically injects tenant ID conditions for multi-tenancy.
use ;
use AkitaValue;
let config = new
.ignore_table
.ignore_table;
let interceptor = new;
Cache Interceptor
Automatically caches query results for improved performance.
use ;
use Duration;
let interceptor = new
.with_default_ttl
.with_cache_prefix;
Performance Interceptor
Monitors SQL query performance and detects slow queries.
use ;
let interceptor = new;
// Get metrics
let metrics = interceptor.metrics;
println!;
println!;
println!;
Cursor Pagination Interceptor
Efficient pagination for large datasets using cursors instead of offsets.
use ;
let interceptor = new
.with_default_limit
.with_max_limit;
// First page (no cursor)
let request = new;
// Next page (with cursor from last result)
let request = new;
Lambda Wrapper (Compile-time Safe Column References)
Akita provides LambdaWrapper<T> for compile-time safe column references:
use *;
// Compile-time safe column references
let wrapper = new
.eq
.gt
.order_by_asc;
SubQuery Builder
Akita provides SubQuery for building subqueries:
use *;
// IN subquery
let sub = in_query
.select
.from
.where_eq;
let wrapper = new
.in_subquery;
// EXISTS subquery
let sub = exists
.select
.from
.where_eq;
let wrapper = new
.exists_subquery;
Entity Methods with Database Portability
Entities can have their own methods:
Pagination with Database Optimization
Batch Operations with Database Optimization
🔧 Configuration
AkitaConfig Options
Environment-based Configuration
🎨 Advanced Features
Custom Type Conversion
use *;
;
📊 Performance Tips
- Connection Pooling: Always configure appropriate pool sizes
-
MySQL/TiDB: 10-100 connections based on workload
-
PostgreSQL: 5-50 connections
-
SQLite: 1 connection (file-based)
- Batch Operations: Use database-specific batch methods
-
MySQL: Multi-value INSERT statements
-
PostgreSQL: COPY command for large datasets
-
SQLite: Transactions around batch operations
- Query Optimization:
-
Use EXPLAIN on MySQL/PostgreSQL to analyze query plans
-
SQLite: Use appropriate indexes and avoid expensive operations in WHERE
- Statement Caching: Akita caches prepared statements automatically
-
Reduces parsing overhead on all databases
-
Especially beneficial for repeated queries
- Connection Reuse: Keep connections alive for related operations
-
Reduces connection establishment overhead
-
Maintains session state
- Database-Specific Features:
-
MySQL/TiDB: Use connection compression for remote connections
-
PostgreSQL: Use prepared statements for complex queries
-
SQLite: Enable WAL mode for better concurrency
🤝 Contributing
We welcome contributions! Here's how you can help:
-
Report Bugs: Create an issue with database-specific details
-
Suggest Features: Start a discussion about new database support or features
-
Submit PRs: Follow our contributing guide
-
Improve Documentation: Help us make the docs better for all database backends
-
Add Database Support: Implement support for new databases
Development Setup
# Clone the repository
# Run tests for specific databases
# Run all tests
# Run examples
# Build documentation
📄 License
Licensed under either of:
-
Apache License, Version 2.0 (LICENSE-APACHE)
-
MIT license (LICENSE-MIT)
at your option.
🙏 Acknowledgments
-
Thanks to all contributors who have helped shape Akita
-
Inspired by great ORMs like Diesel, SQLx, and MyBatis
-
Built with ❤️ by the Cat&Dog Lab team
📞 Contact
-
Author: Mr.Pan
-
Email: 1049058427@qq.com
-
GitHub: @wslongchen
-
Project: Akita on GitHub