| Crates.io | co-orm |
| lib.rs | co-orm |
| version | 0.3.15 |
| created_at | 2022-10-22 14:29:18.48057+00 |
| updated_at | 2025-08-26 13:21:13.279236+00 |
| description | Implement Create, Read, Update, and Delete (CRUD) methods for sqlx. |
| homepage | |
| repository | https://github.com/cody-why/co-orm |
| max_upload_size | |
| id | 694479 |
| size | 113,397 |
A high-performance, async CRUD library for sqlx that provides elegant database operations with compile-time safety.
Add to your Cargo.toml:
[dependencies]
co-orm = { version = "0.3", features = ["mysql"] }
sqlx = { version = "0.8", features = ["mysql", "runtime-tokio-native-tls"] }
Available features: mysql, postgres, sqlite
use co_orm::{Crud, sqlx::FromRow};
use chrono::NaiveDateTime;
#[derive(Debug, Crud, FromRow)]
#[co_orm(rename = "users")]
struct User {
#[co_orm(skip_insert)]
pub id: i64,
#[co_orm(rename = "name")]
pub name: String,
#[co_orm(update)]
pub password: String,
#[co_orm(skip)]
#[sqlx(skip)]
pub addr: Option<String>,
pub age: Option<u32>,
#[co_orm(skip_insert)]
pub update_at: Option<NaiveDateTime>,
}
use co_orm::{Crud, Where};
// Get by primary key
let user = User::get(&pool, 1).await?;
// Query all
let users = User::query(&pool).await?;
// Insert
let new_user = User { /* ... */ };
new_user.insert(&pool).await?;
// Update
user.update(&pool).await?;
// Delete
user.delete(&pool).await?;
// Simple conditions
let users = User::query_where(&pool, Where::new()
.eq("status", "active")
.and()
.ge("age", 18)
).await?;
// Complex grouped conditions
let users = User::query_where(&pool, Where::new()
.eq("status", "active")
.and_group(|w| {
w.gt("age", 18)
.and()
.lt("age", 65)
})
.or_group(|w| {
w.eq("name", "admin")
.and()
.ge("age", 21)
})
).await?;
// Pagination
let (count, users) = User::query_page_where(
&pool,
Where::new().eq("status", "active"),
1, // page
10 // page_size
).await?;
The #[derive(Crud)] macro generates these methods:
get(), get_by(), get_where(), query(), query_by(), query_where()insert(), insert_all(), update(), update_by(), update_where()delete(), delete_by(), delete_where()query_page_by(), query_page_where()| 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 |
| 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 |
| Method | Description |
|---|---|
and() |
AND logic |
or() |
OR logic |
and_group(f) |
AND grouping |
or_group(f) |
OR grouping |
// Query arguments
let args = args!(&name, age);
// Pagination arguments
let args = page_args!(&name, age);
// Raw queries
query!("INSERT INTO users (name, password) VALUES (?, ?)", name, password)
.execute(&pool).await?;
// Typed queries
query_as!(User, "SELECT * FROM users WHERE name = ?", name)
.fetch_one(&pool).await?;
See the examples/ directory for complete working examples:
crud.rs - Basic CRUD operationswhere_examples.rs - Advanced querying patternspool.rs - Connection pool managementLicensed under the MIT License.