Cherry is an asynchronous ORM, support MySQL/MariaDB, PostgreSQL, SQLite and SQL Server. It's lightweight and build on top of SQLx.
This crate is under development (only mysql was tested partially).
toml
[dependencies]
cherry = "0.3.0"
cherry-derive = "0.3.0"
```rust use cherry::{DataSource, PoolConfig};
pub async fn setup() -> Result<(), Box
Foo::setup(conn).await?;
let result: Option<User> = Foo::select()
.and_where_eq("id", 123)
.fetch()
.await?;
Ok(())
}
pub struct Foo;
impl DataSource for Foo {}
// You can more than one DataSources if you need. // pub struct Bar; // impl DataSource for Bar {} ```
```rust
async fn select() -> Result<(), Box
Ok(())
} ```
```rust
async fn insert() -> Result<(), Box
// Insert multiple
let users = [
User { id: 2, name: "Sam".to_owned() },
User { id: 3, name: "Jack".to_owned() }
];
let result = Foo::insert_bulk(&users).execute().await?;
assert_eq!(result.rows_affected(), 2);
// insert replace, insert ignore etc.
Ok(())
} ```
```rust
async fn delete() -> Result<(), Box
Ok(())
} ```
```rust
async fn update() -> Result<(), Box
Ok(())
} ```
```rust use cherry::Transaction;
async fn transaction() -> Result<(), Box
let books = [
Book { id: 1, name: "Book name 1".to_owned() },
Book { id: 2, name: "Book name 2".to_owned() }
];
// Without transaction
Foo::insert_bulk(&users).execute().await?;
// Auto transaction
Foo::insert_bulk(&users).execute_tx().await?;
// Manual transaction
let mut tx: Transaction = Foo::begin().await?;
Foo::insert_bulk(&users).execute_with(&mut tx).await?;
Foo::insert_bulk(&books).execute_with(&mut tx).await?;
tx.commit().await?;
// Or tx.rollback().await?;
Ok(())
} ```
```rust
struct User { id: u64, name: String, } ```
mysql
is the default database, postgres
, sqlite
, mssql
are other options.
runtime-async-std-rustls
is the default connection way. runtime-actix-native-tls
,
runtime-async-std-native-tls
,
runtime-tokio-native-tls
, runtime-actix-rustls
, runtime-tokio-rustls
are other options.
See SQLx Feature Flags for more details.