Crates.io | sqlxx |
lib.rs | sqlxx |
version | 0.1.5 |
source | src |
created_at | 2023-02-21 22:01:14.075634 |
updated_at | 2023-02-27 09:57:31.279254 |
description | Extension to sqlx |
homepage | |
repository | |
max_upload_size | |
id | 791096 |
size | 41,989 |
sqlxx is a Rust library that extends the sqlx library with a custom macro that provides convenient functions for common database operations. With sqlxx, you can easily save, find, and delete records in your database using Rust structs.
To use sqlxx in your Rust project, simply add it to your Cargo.toml file:
[dependencies]
sqlxx = "0.1.0"
To use the save, find_by_id, and delete_by_id functions provided by sqlxx, you'll need to define a Rust struct that represents a record in your database, and deriveit with Model
attributes:
use sqlxx::Model;
#[derive(Model)]
struct User {
id: i32,
name: String,
email: String,
}
Once you've defined your struct, you can use the Model macro to generate the save, find_by_id, and delete_by_id functions:
use sqlxx::Model;
#[derive(Model)]
struct User {
id: i32,
name: String,
email: String,
}
fn main() -> Result<(), sqlx::Error> {
let mut user = User {
id: 0,
name: "Alice".to_string(),
email: "alice@example.com".to_string(),
};
user.save(db).await;
let loaded_user = User::find_by_id(db, user.id).await;
loaded_user.delete(db).await;
Ok(())
}
The save
function will insert a new record into the database if the id field is None, or update an existing record if the id field has a value. The find_by_id function will load a record from the database with the given ID, and the delete_by_id function will delete a record from the database with the given ID.
Currently, sqlxx only supports PostgreSQL. Support for other databases may be added in the future.
Contributions to sqlxx are welcome! If you find a bug or would like to suggest a new feature, please open an issue on the GitHub repository. Pull requests are also welcome.
sqlxx is licensed under the MIT License.