KitX - Lightweight SQL Builder for Rust
A minimalistic SQL builder library based on sqlx, supporting SQLite, MySQL/MariaDB, and PostgreSQL. It offers efficient database operations with soft delete capabilities and global filters, enabling developers to interact with databases more effectively.
Features
Core Functionality
-
Efficient CRUD Operations
insert_one,insert_many,update_by_key,update_one,upsert_by_key,
upsert_many,delete_by_key,delete_by_cond,delete_many
with transaction support -
Advanced Queries
get_list,get_by_key,get_one,get_list_paginated,get_list_by_cursor,
exists,count -
Soft Delete Management
restore_one,restore_many
with global configuration -
Flexible Query Building
Supports JOINs, CASE WHEN, WITH CTE and aggregations. Provides native support for ON CONFLICT/DUPLICATE KEY (upsert) and RETURNING, enabling conflict resolution and data retrieval.
Key Advantages
- 🚀 No ORM Overhead - Direct SQL interaction with builder pattern
- 🔧 Field Access API - Utilizes field_access for field operations
- 🌍 Global Filters - Apply tenant ID or soft delete filters across all queries
- 📦 Extensible - Easily add custom operations and query modifiers
Quick Start
1. Add Dependency
# Default SQL Builder, completely decoupled from any external libraries.
= "0.0.10"
# For SQLite only
= { = "0.0.10", = ["sqlite"] }
# For MySQL/MariaDB only
= { = "0.0.10", = ["mysql"] }
# For PostgreSQL only
= { = "0.0.10", = ["postgres"] }
2. Basic Usage
use ;
// SQL Builder Example
// AND and OR conditions can be applied either within filter clauses or directly in the builder.
let query = columns
.from
.where_
.and
.or
.order_by
.build.0;
let query2 = into
.columns
.values
.build.0;
// CRUD Operations
// KitX does not support composite primary keys. For such cases, please use constraints instead.
let op = new;
let article = Article ;
// Insert with transaction
op.insert_one.await?;
3. Pagination Example
let results = op.get_list_paginated.await?;
let results = op.get_list_by_cursor.await?;
4. Optional: Global Configuration
// Soft delete configuration
set_global_soft_delete_field;
// Global_filter is applied on a per-thread basis.
// Multi-tenant filtering
set_global_filter, vec!);
License
MIT License