Crate pgde

source ·
Expand description

A simple library for consuming tokio_postgres::row::Row data into structs that derive the RowConsumer trait.

Most of the complex PostgreSQL types are not supported, namely arrays. Consequently Vec types on structs are not currently supported.

§Features

FeatureDescriptionExtra dependenciesDefault
consume_jsonImplements consume_json on classes that derive the RowConsumer traitserde, serde_jsonNo
jsonImplements consume on serde_json::Valueserde_jsonNo
uuidImplements consume on uuid::UuiduuidNo

§Examples

§consume

You may use consume to consume PostgreSQL row data into a struct like so.

#[derive(RowConsumer)]
struct Foo {
    Id: i32,
    Data: String,
}

...

let query = "select * from public.\"Foo\";";

match Foo::consume(&conn, query, &[]).await {
    Ok(v) => ..., // v is of type Vec<Foo>
    Err(v) => ...,
};

This crate implements from_row on the following types so that consume can be used in a similar fashion.

TypeFeature
booldefault
i8default
i16default
i32default
u32default
i64default
f32default
f64default
Stringdefault
SystemTimedefault
IpAddrdefault
serde_json::Valuejson
uuid::Uuiduuid
let query = "select Id from public.\"Foo\";";

match i32::consume(&conn, query, &[]).await {
    Ok(v) => ..., // v is of type Vec<i32>
    Err(v) => ...,
};

§Features

The json and uuid features provide consume on serde_json::Value and uuid::Uuid for json and uuid data types in PostgreSQL.

With the consume_json feature you get access to consume_json, which returns json data in a String.

#[derive(Serialize, RowConsumer)]
struct Foo { ... }

...

match Foo::consume_json(&conn, query, &[]).await {
    Ok(v) => ..., // v is of type String
    Err(v) => ...,
};

§Testing

Testing requires access to a PostgreSQL database with no tables. Setting the following environment variables will allow you to test.

Environment VariableDescription
PGDE_DB_HOSTThe host that the database can be accessed at.
POSTGRES_USERThe user credential to provide.
POSTGRES_PASSWORDThe password to provide.
POSTGRES_DBThe name of the database to use for testing.

To test, you would then run.

cargo test --tests --all-features

Macros§

  • A macro for implementing from_row on primitive types that defaults to a provided expression. Used internally to implement from_row on SystemTime and IpAddr.
  • A macro for implementing from_row on primitive types. Used internally to implement from_row on bool, i32, String, etc.

Enums§

Traits§

  • The derivable trait for implementing PostgreSQL row consumption.