Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eagletmt/sqlx-derive-with
Derive `sqlx::FromRow` specific to the given database
https://github.com/eagletmt/sqlx-derive-with
Last synced: 2 months ago
JSON representation
Derive `sqlx::FromRow` specific to the given database
- Host: GitHub
- URL: https://github.com/eagletmt/sqlx-derive-with
- Owner: eagletmt
- License: mit
- Created: 2022-08-05T15:55:50.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-08-06T11:25:33.000Z (over 2 years ago)
- Last Synced: 2024-08-08T16:50:19.360Z (5 months ago)
- Language: Rust
- Size: 13.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlx-derive-with
Derive `sqlx::FromRow` specific to the given database.sqlx-derive-with supports `decode` attribute to use custom decoder function to specific columns.
This feature is not (and unable to be, I think) supported by upstream `sqlx::FromRow`.## Usage
```rust
use sqlx::Connection as _;#[derive(sqlx_derive_with::FromRow)]
#[sqlx_with(db = "sqlx::Sqlite")]
struct Row {
#[sqlx_with(decode = "split_x")]
x: (i64, i64),
y: String,
}fn split_x(index: &str, row: &sqlx::sqlite::SqliteRow) -> sqlx::Result<(i64, i64)> {
use sqlx::Row as _;
let n: i64 = row.try_get(index)?;
Ok((n, n + 2))
}#[tokio::main]
async fn main() {
let mut conn = sqlx::SqliteConnection::connect(":memory:").await.unwrap();
let row: Row = sqlx::query_as("select 10 as x, 'hello' as y")
.fetch_one(&mut conn)
.await
.unwrap();
assert_eq!(row.x, (10, 12));
assert_eq!(row.y, "hello");
}
```