Giter Club home page Giter Club logo

tokio-postgres-mapper's Introduction

tokio-pg-mapper

tokio_postgres-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:

extern crate postgres;

use postgres::rows::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); // 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:

extern crate tokio_pg_mapper_derive;
extern crate tokio_pg_mapper;

use tokio_pg_mapper::FromTokioPostgresRow;
use tokio_pg_mapper_derive::PostgresMapper;

#[derive(PostgresMapper)]
pub struct User {
    pub id: i64,
    pub name: String,
    pub email: Option<String>,
}

// 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.query_one(stmt, &[&5, "asdf"]).await?;
let user = User::from_row(result).unwrap(); // or from_row_ref(&result)

The two crates

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.

Installation

Install tokio-pg-mapper-derive and tokio-pg-mapper from crates.io

License

ISC.

tokio-postgres-mapper's People

Contributors

dowwie avatar dunnock avatar indy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.