Skip to content

Easily Deserialize Postgres rows.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

amilajack/serde_postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serde Postgres

Build status Crate Lines Of Code Documentation

Easily deserialize rows from postgres into arbitrary structs. (Only deserialization is supported).

extern crate serde;
extern crate serde_derive;
extern crate serde_postgres;
extern crate postgres;

use std::error::Error;

use serde_derive::Deserialize;
use postgres::{Connection, TlsMode};

#[derive(Clone, Debug, Deserialize)]
struct Person {
    name: String,
    age: i32,
}

fn main() -> Result<(), Box<Error>> {
    let connection = Connection::connect("postgres://postgres@localhost:5432", TlsMode::None)?;

    connection.execute("CREATE TABLE IF NOT EXISTS Person (
        name VARCHAR NOT NULL,
        age INT NOT NULL
    )", &[])?;

    connection.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
    &[&"Jane", &23])?;

    connection.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
    &[&"Alice", &32])?;
    
    let rows = connection.query("SELECT name, age FROM Person", &[])?;

    let people: Vec<Person> = serde_postgres::from_rows(&rows)?;

    for person in people {
        println!("{:?}", person);
    }

    Ok(())
}

About

Easily Deserialize Postgres rows.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages