#diesel #postgresql #hstore

diesel_pg_hstore

Postgres Hstore support for Diesel

2 unstable releases

Uses old Rust 2015

0.2.0 Dec 12, 2017
0.1.0 Dec 12, 2017

#2637 in Database interfaces

49 downloads per month

MIT/Apache

19KB
273 lines

Postgres Hstore for Diesel

This crate provides an Hstore type for use with Diesel and Postgres.

Currently only serializing to and from hstore columns is supported. If someone would be interested in building out support for the Postgres hstore query syntax, help would be appreciated!

Usage

Please see the Documentation for more details.

Add diesel_pg_hstore to your Cargo.toml:

[dependencies]
diesel_pg_hstore = "*"

Bring the crate into your project. (For example, from your lib.rs file)

extern diesel_pg_hstore;

Using the Hstore type with Diesel

The type must be present in the table! definition for your schema. There is currently no easy way to provide this without explicitly adding it to each table! requiring the type manually.

Once Diesel 1.0 is out of beta, Diesel will be providing the ability for both the diesel print-schema command and the infer_schema! macro to bring external types into scope. For now, I recommend not using the infer_schema! macro.

If you are using the diesel print-schema command to regenerate your schema, you might consider creating a .patch file that contains the required use diesel_pg_hstore::Hstore; statements for bringing the Hstore type into scope as needed.

Using the Hstore

#[macro_use] extern crate diesel;
extern crate diesel_pg_hstore;

use std::collections::HashMap;
use diesel::prelude::*;
use diesel_pg_hstore::Hstore;

table! {
    use diesel::types::*;
    use diesel_pg_hstore::Hstore;

    user_profile {
        id -> Integer,
        settings -> Hstore,
    }
}

#[derive(Insertable, Debug, PartialEq)]
#[table_name="user_profile"]
struct NewUserProfile {
    settings: Hstore,
}

fn main() {
    let mut settings = HashMap::new();
    settings.insert("Hello".to_string(), "World".to_string());

    let profile = NewUserProfile { settings: Hstore::from_hashmap(settings) };
}

For your convenience, the Hstore type also provides proxy methods to the standard HashMap functions.

License

diesel_pg_hstore is licensed under either of

Contributing

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.

Please see the CONTRIBUTING file for more information.

Dependencies

~4MB
~87K SLoC