Crates.io | sqlx-meta |
lib.rs | sqlx-meta |
version | 0.1.0 |
source | src |
created_at | 2022-12-18 16:23:29.034228 |
updated_at | 2022-12-18 16:23:29.034228 |
description | Derive macro for SQLx to get metadata about the entity such as column names |
homepage | |
repository | https://github.com/rusty-ferris-club/sqlx-meta |
max_upload_size | |
id | 740482 |
size | 4,805 |
A library derived as a slim version of sqlx-crud to provide metadata about the entity it marks, design to only provide the building blocks for you to build your own queries.
Add to your dependencies:
sqlx-meta = "0.1.0"
And mark your entity with SqlxMeta
:
#[derive(Default, Debug, Clone, sqlx::FromRow, SqlxMeta)]
pub struct File {
pub id: Option<i32>,
pub entry_time: String,
pub abs_path: String,
pub path: String,
pub ext: Option<String>,
pub mode: Option<String>,
// ..
}
Build your own query (prefer lazy_static
when it doesn't really change):
lazy_static! {
static ref INSERT_SQL: String = {
let cols = &File::columns()[1..];
let holders = (0..cols.len()).map(|_| "?").collect::<Vec<_>>().join(", ");
let excludes = cols
.iter()
.map(|c| format!("'{}'=excluded.'{}'", c, c))
.collect::<Vec<_>>()
.join(",");
// ..
// ..
Use the update_binds
or insert_binds
methods to save up typing:
use sqlx_meta::{Binds, Schema};
f.update_binds(q).fetch_optional(&mut conn).await?;