KitX: Lightweight Rust SQL Builder for Rapid CRUD Operations
A minimalistic SQL builder library for Rust built on sqlx, designed for streamlined database interactions. This lightweight wrapper focuses on accelerating core CRUD operations (Create, Read, Update, Delete) while maintaining simplicity for straightforward database management tasks.
Features
Core Functionality
-
CRUD Operations
insert_one,insert_many,update_one,update_by_cond,upsert_one,upsert_many,delete_by_pk,delete_by_cond
get_one_by_pk,get_one_by_cond,get_list_by_cond,get_list_paginated,get_list_by_cursor,exists,count -
Soft Delete Management
restore_by_pk,restore_by_cond
with global configuration -
Flexible Query Building
Supports JOINs, CASE WHEN, WITH CTE, and aggregations. Supports ON CONFLICT/DUPLICATE KEY (upsert) and RETURNING for conflict resolution and data retrieval. -
Code Characteristics
- No Macros: Public interfaces avoid macros, ensuring transparency and maintainability.
- No
.unwrap()or.expect(): Prevents runtime panics by promoting robust error handling.
Key Advantages
- 🛠️ SeaQuery-Like API - Simplified Query builder
- 🚀 No ORM Overhead - Direct SQL interaction with builder pattern
- 🌍 Global Filters - Apply tenant ID or soft delete filters across all queries
Quick Start
1. Add Dependency
# Default SQL Builder, completely decoupled from any external libraries.
= "0.0.13"
# For SQLite only, WAL mode is enabled by default.
= { = "0.0.13", = ["sqlite"] }
# For MySQL/MariaDB only
= { = "0.0.13", = ["mysql"] }
# For PostgreSQL only
= { = "0.0.13", = ["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
.and_where
.and_where
.or_where
.order_by
.build.0;
let query2 = into
.columns
.values
.build.0;
// CRUD Operations (Single Key)
let op = new;
// Composite Key Operations
// let op = MutliKeyOperations::new("articles_tag", vec!["article_id", "tag_id"]);
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. JOIN Operations (Relationship Handling)
let results = op.get_list
5. Transaction Management
use ;
let query = shared;
let article_op = article_operations.set;
let article_tag_op = tag_operations.set;
query.share.begin_transaction.await?
let mut article = new;
article.content = Some;
let mut article_ag = new;
article_ag.article_id = 1;
article_ag.share_seq = 1234;
article_op.insert_one.await?;
article_tag_op.insert_one.await?;
query.share.commit.await?;
6. 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, &);
7. More Usage Examples
For more detailed usage examples and advanced scenarios, please refer to the test cases provided in the repository.
License
MIT License