https://github.com/rusty-ferris-club/sqlx-meta
Derive macro to give sqlx entities their metadata
https://github.com/rusty-ferris-club/sqlx-meta
Last synced: 3 months ago
JSON representation
Derive macro to give sqlx entities their metadata
- Host: GitHub
- URL: https://github.com/rusty-ferris-club/sqlx-meta
- Owner: rusty-ferris-club
- License: apache-2.0
- Created: 2022-12-18T16:26:23.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-18T16:26:38.000Z (almost 3 years ago)
- Last Synced: 2025-04-30T19:41:41.820Z (6 months ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Sqlx Meta
A library derived as a slim version of [sqlx-crud](https://github.com/treydempsey/sqlx-crud) to provide metadata about the entity it marks, design to only provide the building blocks for you to build your own queries.
## Getting started
Add to your dependencies:
```toml
sqlx-meta = "0.1.0"
```And mark your entity with `SqlxMeta`:
```rust
#[derive(Default, Debug, Clone, sqlx::FromRow, SqlxMeta)]
pub struct File {
pub id: Option,
pub entry_time: String,
pub abs_path: String,
pub path: String,
pub ext: Option,
pub mode: Option,
// ..
}
```Build your own query (prefer `lazy_static` when it doesn't really change):
```rust
lazy_static! {
static ref INSERT_SQL: String = {
let cols = &File::columns()[1..];let holders = (0..cols.len()).map(|_| "?").collect::>().join(", ");
let excludes = cols
.iter()
.map(|c| format!("'{}'=excluded.'{}'", c, c))
.collect::>()
.join(",");
// ..
// ..
```Use the `update_binds` or `insert_binds` methods to save up typing:
```rust
use sqlx_meta::{Binds, Schema};
f.update_binds(q).fetch_optional(&mut conn).await?;
```