GithubHelp home page GithubHelp logo

rust-postgres-derive's Introduction

postgres-derive

CircleCI

Syntax extensions to automatically derive FromSql and ToSql implementations for Postgres enum, domain, and composite types.

The generated code requires rust-postgres 0.14 or 0.15 and Rust 1.20.0 or higher.

Usage

Simply depend on the postgres-derive crate and register it as a plugin:

Cargo.toml

# ...

[dependencies]
postgres-derive = "0.3"
postgres = "0.15"

lib.rs

#[macro_use]
extern crate postgres;
#[macro_use]
extern crate postgres_derive;

#[derive(Debug, ToSql, FromSql)]
pub enum Mood {
    Sad,
    Ok,
    Happy,
}

// ...

Types

Enums

Postgres enums correspond to C-like enums in Rust:

CREATE TYPE "Mood" AS ENUM (
    'Sad',
    'Ok',
    'Happy'
);
#[derive(Debug, ToSql, FromSql)]
enum Mood {
    Sad,
    Ok,
    Happy,
}

The implementations will expect exact matches between the type names and variants. The #[postgres(...)] attribute can be used to adjust the names used on the Postgres side:

CREATE TYPE mood AS ENUM (
    'sad',
    'ok',
    'happy'
);
#[derive(Debug, ToSql, FromSql)]
#[postgres(name = "mood")]
enum Mood {
    #[postgres(name = "sad")]
    Sad,
    #[postgres(name = "ok")]
    Ok,
    #[postgres(name = "happy")]
    Happy,
}

Domains

Postgres domains correspond to tuple structs with one member in Rust:

CREATE DOMAIN "SessionId" AS BYTEA CHECK(octet_length(VALUE) = 16);
#[derive(Debug, FromSql, ToSql)]
struct SessionId(Vec<u8>);

As above, the implementations will expect an exact match between the Rust and Postgres type names, and the #[postgres(...)] attribute can be used to adjust that behavior:

CREATE DOMAIN session_id AS BYTEA CHECK(octet_length(VALUE) = 16);
#[derive(Debug, FromSql, ToSql)]
#[postgres(name = "session_id")]
struct SessionId(Vec<u8>);

Composites

Postgres composite types correspond to structs in Rust:

CREATE TYPE "InventoryItem" AS (
    name TEXT,
    supplier_id INT,
    price DOUBLE PRECISION
);
#[derive(Debug, FromSql, ToSql)]
struct InventoryItem {
    name: String,
    supplier_id: i32,
    price: Option<f64>,
}

Again, the implementations will expect an exact match between the names of the Rust and Postgres types and fields, which can be adjusted via the #[postgres(...)] attribute:

CREATE TYPE inventory_item AS (
    name TEXT,
    supplier_id INT,
    the_price DOUBLE PRECISION
);
#[derive(Debug, FromSql, ToSql)]
#[postgres(name = "inventory_item")]
struct InventoryItem {
    name: String,
    supplier_id: i32,
    #[postgres(name = "the_price")]
    price: Option<f64>,
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

rust-postgres-derive's People

Contributors

sfackler avatar hcpl avatar teburd avatar

Watchers

James Cloos 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.