Crates.io | sqlx-template |
lib.rs | sqlx-template |
version | 0.1.1 |
source | src |
created_at | 2024-07-06 22:00:56.778185 |
updated_at | 2024-07-22 16:46:11.176462 |
description | Template query library for Rust SQLx |
homepage | |
repository | https://github.com/hn63wospuvy/sqlx-template |
max_upload_size | |
id | 1294382 |
size | 196,511 |
sqlx-template
is a Rust library designed to generate database query functions using macros, based on the sqlx
framework. It aims to provide a flexible, simple way to interact with databases such as MySQL, Postgres, and SQLite.
sqlx
crate, so you need to add it to your dependencies before using this library.sqlx::FromRow
and TableName
.use sqlx_template::{multi_query, query, select, update, DeleteTemplate, SelectTemplate, TableName, UpdateTemplate};
#[derive(sqlx::FromRow, InsertTemplate, UpdateTemplate, SelectTemplate, DeleteTemplate, TableName)]
#[debug_slow = 1000]
#[table_name = "users"]
#[tp_delete(by = "id")]
#[tp_delete(by = "id, email")]
#[tp_select_all(by = "id, email", order = "id desc")]
#[tp_select_one(by = "id", order = "id desc", fn_name = "get_last_inserted")]
#[tp_select_one(by = "email")]
#[tp_select_page(by = "org", order = "id desc, org desc")]
#[tp_select_count(by = "id, email")]
#[tp_update(by = "id", op_lock = "version", fn_name = "update_user")]
#[tp_select_stream(order = "id desc")]
#[tp_select_stream(by = "email", order = "id desc")]
pub struct User {
#[auto]
id: i32,
email: String,
password: String,
org: Option<i32>,
active: bool,
#[auto]
version: i32,
created_by: Option<String>,
#[auto]
created_at: DateTime<Utc>,
updated_by: Option<String>,
updated_at: Option<DateTime<Utc>>,
}
#[select("
SELECT name, age
FROM users
WHERE name = :name and age = :age
")]
pub async fn query_user_info(name: String, age: i32) -> Stream<(String, i16)> {}
#[multi_query(file = "sql/init.sql", 0)]
async fn migrate() {}
For more details, please see the examples in the repository.
postgres
: Target PostgreSQL databases.mysql
: Target MySQL databases.sqlite
: Target SQLite databases.tracing
: Use the tracing::debug!
macro for logging (requires adding the tracing
crate to Cargo.toml
).log
: Use the log::debug!
macro for logging (requires adding the log
crate to Cargo.toml
).debug_slow
applies to all attributes using derived macros of the struct. It can be overridden by declaring the debug_slow
attribute within the attribute itself. To disable it, set debug_slow = -1
explicity.tracing
nor log
features are declared, information will be printed to the screen using the println!
macro.sqlx::query!
marcomulti_query
marcoThis project is licensed under the Apache 2.0 License.
All PRs are welcome!