Skip to content

Commit

Permalink
Merge pull request #1 from killercup/pg-jsonb-tests
Browse files Browse the repository at this point in the history
Add doc test for Jsonb type
  • Loading branch information
emk authored Jan 13, 2017
2 parents e249061 + ed1b490 commit 0b96bf0
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions diesel/src/pg/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,80 @@ pub mod sql_types {
/// - [`serde_json`][Value]
///
/// [Value]: https://docs.serde.rs/serde_json/value/enum.Value.html
///
/// # Examples
///
/// ```rust
/// # #![allow(dead_code)]
/// extern crate serde_json;
/// # #[macro_use] extern crate diesel;
/// # include!("src/doctest_setup.rs");
/// #
/// # table! {
/// # users {
/// # id -> Serial,
/// # name -> VarChar,
/// # }
/// # }
/// #
/// struct Contact {
/// id: i32,
/// name: String,
/// address: serde_json::Value,
/// }
///
/// impl_Queryable! {
/// struct Contact {
/// id: i32,
/// name: String,
/// address: serde_json::Value,
/// }
/// }
///
/// struct NewContact {
/// name: String,
/// address: serde_json::Value,
/// }
///
/// impl_Insertable! {
/// (contacts)
/// struct NewContact {
/// name: String,
/// address: serde_json::Value,
/// }
/// }
///
/// table! {
/// contacts {
/// id -> Integer,
/// name -> VarChar,
/// address -> Jsonb,
/// }
/// }
///
/// # fn main() {
/// # use self::diesel::insert;
/// # use self::contacts::dsl::*;
/// # let connection = connection_no_data();
/// # connection.execute("CREATE TABLE contacts (
/// # id SERIAL PRIMARY KEY,
/// # name VARCHAR NOT NULL,
/// # address JSONB NOT NULL
/// # )").unwrap();
/// let santas_address: serde_json::Value = serde_json::from_str(r#"{
/// "street": "Article Circle Expressway 1",
/// "city": "North Pole",
/// "postcode": "99705",
/// "state": "Alaska"
/// }"#).unwrap();
/// let new_contact = NewContact {
/// name: "Claus".into(),
/// address: santas_address.clone()
/// };
/// let inserted_contact = insert(&new_contact).into(contacts)
/// .get_result::<Contact>(&connection).unwrap();
/// assert_eq!(santas_address, inserted_contact.address);
/// # }
/// ```
#[derive(Debug, Clone, Copy, Default)] pub struct Jsonb;
}

0 comments on commit 0b96bf0

Please sign in to comment.