co-orm
A high-performance, async CRUD library for sqlx that provides elegant database operations with compile-time safety.
Features
- Zero-cost abstractions - Minimal runtime overhead
- Type-safe queries - Compile-time SQL generation
- Async-first - Built on tokio and sqlx
- Flexible filtering - Powerful WHERE clause builder
- Multiple databases - MySQL, PostgreSQL, SQLite support
- Macro-driven - Simple derive macros for common operations
Installation
Add to your Cargo.toml
:
[]
= { = "0.3", = ["mysql"] }
= { = "0.8", = ["mysql", "runtime-tokio-native-tls"] }
Available features: mysql
, postgres
, sqlite
Quick Start
Basic Model Definition
use ;
use NaiveDateTime;
Basic CRUD Operations
use ;
// Get by primary key
let user = get.await?;
// Query all
let users = query.await?;
// Insert
let new_user = User ;
new_user.insert.await?;
// Update
user.update.await?;
// Delete
user.delete.await?;
Advanced Querying
// Simple conditions
let users = query_where.await?;
// Complex grouped conditions
let users = query_where.await?;
// Pagination
let = query_page_where.await?;
Available Methods
The #[derive(Crud)]
macro generates these methods:
- Read:
get()
,get_by()
,get_where()
,query()
,query_by()
,query_where()
- Write:
insert()
,insert_all()
,update()
,update_by()
,update_where()
- Delete:
delete()
,delete_by()
,delete_where()
- Pagination:
query_page_by()
,query_page_where()
Field Attributes
Attribute | Description |
---|---|
#[co_orm(id)] |
Mark as primary key (default: first field), for update and delete. |
#[co_orm(skip_insert)] |
Skip field during insert operations |
#[co_orm(rename = "name")] |
Rename table or field in database |
#[co_orm(skip)] |
Ignore field completely |
#[co_orm(update)] |
Generate update methods for this field |
Query Operators
Method | SQL | Description |
---|---|---|
eq(col, value) |
= |
Equal |
ne(col, value) |
<> |
Not equal |
gt(col, value) |
> |
Greater than |
lt(col, value) |
< |
Less than |
ge(col, value) |
>= |
Greater or equal |
le(col, value) |
<= |
Less or equal |
like(col, value) |
LIKE |
Pattern matching |
between(col, start, end) |
BETWEEN |
Range query |
r#in(col, values) |
IN |
Set membership |
is_null(col) |
IS NULL |
Null check |
is_not_null(col) |
IS NOT NULL |
Not null check |
raw(fragment) |
Custom | Custom SQL fragment |
Logical Operators
Method | Description |
---|---|
and() |
AND logic |
or() |
OR logic |
and_group(f) |
AND grouping |
or_group(f) |
OR grouping |
Utility Macros
// Query arguments
let args = args!;
// Pagination arguments
let args = page_args!;
// Raw queries
query!
.execute.await?;
// Typed queries
query_as!
.fetch_one.await?;
Examples
See the examples/
directory for complete working examples:
crud.rs
- Basic CRUD operationswhere_examples.rs
- Advanced querying patternspool.rs
- Connection pool management
License
Licensed under the MIT License.