tokio_postgres-mapper
is a proc-macro designed to make mapping from postgresql
tables to structs simple.
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:
```rust extern crate postgres;
use postgres::rows::Row;
pub struct User {
pub id: i64,
pub name: String,
pub email: Option
impl From
// code to execute a query here and get back a row let user = User::from(row); // this can panic ```
This becomes worse when manually implementating using the non-panicking
get_opt
method variant.
Using this crate, the boilerplate is removed, and panicking and non-panicking implementations are derived:
```rust extern crate tokiopgmapperderive; extern crate tokiopg_mapper;
use tokiopgmapper::FromTokioPostgresRow; use tokiopgmapper_derive::PostgresMapper;
pub struct User {
pub id: i64,
pub name: String,
pub email: Option
// Code to execute a query here and get back a row might now look like: let stmt = "SELECT * FROM user WHERE username = $1 AND password = $2";
let result = client.queryone(stmt, &[&5, "asdf"]).await?; let user = User::fromrow(result).unwrap(); // or fromrowref(&result)
```
This repository contains two crates: postgres-mapper
which contains an Error
enum and traits for converting from a tokio-postgres
Row
without panicking, and postgres-mapper-derive
which contains the proc-macro.
Install tokio-pg-mapper-derive
and tokio-pg-mapper
from crates.io
ISC.