postgres-from-row
Derive FromRow
to generate a mapping between a struct and postgres rows.
This crate is compatible with both postgres and tokio-postgres.
[]
= "0.5.2"
Examples
use FromRow;
let row = client.query_one.unwrap;
// Pass a row with the correct columns.
let todo = from_row;
let row = client.query_one.unwrap;
// Use `try_from_row` if the operation could fail.
let todo = try_from_row;
assert!;
Each field need's to implement postgres::types::FromSql
, as this will be used to convert a
single column to the specified type. If you want to override this behavior and delegate it to a
nested structure that also implements FromRow
, use #[from_row(flatten)]
:
use FromRow;
let row = client.query_one.unwrap;
let todo = from_row;
If a the struct contains a field with a name that differs from the name of the sql column, you can use the #[from_row(rename = "..")]
attribute.
When a field in your struct has a type T
that doesn't implement FromSql
or FromRow
but
it does impement T: From<C>
or T: TryFrom<c>
, and C
does implment FromSql
or FromRow
you can use #[from_row(from = "C")]
or #[from_row(try_from = "C")]
. This will use type C
to extract it from the row and
then finally converts it into T
.