[][src]Crate tokio_pg_mapper

tokio-pg-mapper

tokio-pg-mapper is a proc-macro designed to make mapping from postgresql tables to structs simple.

Why?

It can be frustrating to write a lot of boilerplate and, ultimately, duplicated code for mapping from postgres Rows into structs.

For example, this might be what someone would normally write:

use postgres::row::Row;

pub struct User {
    pub id: i64,
    pub name: String,
    pub email: Option<String>,
}

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

// code to execute a query here and get back a row
let user = User::from_row(row); // returns Result<User, tokio_pg_mapper::Error>

The two crates

This repository contains two crates: tokio-pg-mapper which contains an Error enum and traits for converting from tokio-postgres Row without panicking, and pg-mapper-derive which contains the proc-macro.

pg-mapper-derive has 3 features that can be enabled (where T is the struct being derived with the provided TokioPostgresMapper proc-macro):

impl FromTokioPostgresRow<::tokio_postgres::row::Row> for T and impl FromTokioPostgresRow<&::tokio_postgres::row::Row> for T implementations

  • pg-mapper which, for each of the above features, implements pg-mapper's FromTokioPostgresRow trait

This will derive implementations for converting from owned and referenced tokio-postgres::row::Rows, as well as implementing pg-mapper's FromTokioPostgresRow trait for non-panicking conversions.

Enums

Error

General error type returned throughout the library.

Traits

FromTokioPostgresRow

Trait containing various methods for converting from a tokio-postgres Row to a mapped type.