It provide utilities to work with the tokio-postgres crate, specifically through the use of FromRow
and TryFromRow
derive macros.
These macros simplify the process of converting database rows into Rust structs.
Installation
Add tokio-postgres-utils
to your Cargo.toml
:
[]
= "0.7"
= "0.1"
Example
use FromRow;
Expand into something like:
use Row;
Field Attributes #[column(..)]
Several attributes can be specified to customize how each column in a row is read:
rename
When the name of a field in Rust does not match the name of its corresponding column, you can use the rename attribute to specify the name that the field has in the row. For example:
use FromRow;
Given a query such as:
SELECT id, name, description FROM users;
will read the content of the column description
into the field about_me
.
flatten
If you want to handle a field that implements FromRow, you can use the flatten attribute to specify that you want it to use FromRow for parsing rather than the usual method. For example:
use FromRow;
Given a query such as:
SELECT id, name, country, city, road FROM users;
skip
The corresponding field should be ignored when mapping database query results and use default value.
This is particularly useful when you have fields in your struct that are not present in the query results or when you want to exclude certain fields from being populated by the query.
use FromRow;
Given a query such as:
SELECT name FROM users;