Crate diesel_pg_hstore [] [src]

Postgres Hstore support for Diesel

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

Usage

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) rust,ignore 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 Hstore with a table! statement:

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

    my_table {
        id -> Integer,
        some_other_column -> Text,
        an_hstore -> Hstore,
    }
}

Using the Hstore type in your code

#[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.

use diesel_pg_hstore::Hstore;

let mut things = Hstore::new();
things.insert("Hello".into(), "World".into());

Nullable hstore values

Postgres hstore entries having a null value are simply ignored.

Structs

Hstore

The Hstore wrapper type.