#postgresql #sqlx #serde #newtype #pg-row #serialization #serde-json

sqlx-pgrow-serde

serde-compatible newtype wrapper for sqlx::postgres::PgRow

7 releases

0.2.0 Jul 29, 2023
0.1.6 May 25, 2023
0.1.2 Mar 6, 2023

#1774 in Encoding

Download history 36/week @ 2024-07-21 73/week @ 2024-07-28 18/week @ 2024-08-04 74/week @ 2024-08-11 64/week @ 2024-08-18 51/week @ 2024-08-25 33/week @ 2024-09-01 34/week @ 2024-09-08 26/week @ 2024-09-15 34/week @ 2024-09-22 56/week @ 2024-09-29 50/week @ 2024-10-06 59/week @ 2024-10-13 25/week @ 2024-10-20 14/week @ 2024-10-27 75/week @ 2024-11-03

179 downloads per month

MIT license

15KB
246 lines

sqlx-pgrow-serde

Check lib.rs for tests, which should give you a clear idea how to use the crate.

async fn main() {
    let row: PgRow = conn.fetch_one("select 3.3").await.unwrap();
    // option 1 - use function calls
    let headers: Vec<String> = read_headers(&row);
    let row: Vec<serde_json::Value> = read_row(&row);
    // option 2 - serializes as a header -> value map
    let row = SerMapPgRow::from(row);
    let row: serde_json::Value = serde_json::to_value(&row).unwrap();
    // option 3 - serializes as a serde_json::Array
    let row = SerVecPgRow::from(row);
    let row: serde_json::Value = serde_json::to_value(&row).unwrap();
}

Look at the SerMapPgRow and SerVecPgRow structs if you want to use #[serialize_with(...)] on your structs.

Dependencies

~11–27MB
~401K SLoC