Crate tokio_postgres_utils

source ·
Expand description

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:

[dependencies]
tokio-postgres = "0.7"
tokio-postgres-utils = "0.1"

§Example

use tokio_postgres_utils::FromRow;

#[derive(FromRow)]
struct User {
    id: i32,
    name: String,
}

Expand into something like:

use tokio_postgres::Row;

impl From<&Row> for User {
    fn from(row: &Row) -> Self {
        Self {
            id: row.get("id"),
            name: row.get("name"),
        }
    }
}

Derive Macros§

  • Implements From<&Row> trait for a struct, allowing direct conversion from a database row to the struct.
  • Implements the TryFrom<&Row> trait for a struct