Crates.io | diesel_json |
lib.rs | diesel_json |
version | 0.2.1 |
source | src |
created_at | 2021-01-17 21:15:28.557312 |
updated_at | 2023-07-05 08:58:43.958614 |
description | Json wrapper type for JsonB data handling in diesel |
homepage | https://github.com/PPakalns/diesel_json |
repository | https://github.com/PPakalns/diesel_json |
max_upload_size | |
id | 343278 |
size | 13,617 |
Provides a wrapper diesel_json::Json
type that can be directly used
to wrap serde serializable, deserializable structures and recognize them
as queryable, insertable JsonB fields.
Add diesel_json dependency to Cargo.toml.
diesel_json = "0.2"
diesel_json = "0.1"
Wrap data structures into diesel_json::Json
type.
#[derive(Serialize, Deserialize, Debug)]
struct ComplexStruct {
// ...
}
use diesel_json::Json;
#[derive(Serialize, Deserialize, Queryable, Insertable, AsChangeset, Identifiable)]
struct ExampleTable {
// ...
// Field that will be stored in Jsonb format
jsonb_field: diesel_json::Json<ComplexStruct>,
// Or simply as
jsonb_field2: Json<ComplexStruct>,
// ...
}
Json
type provides new
function for object initialization, implements Deref
,
DerefMut
, AsRef
, AsMut
and other traits that maps data access directly to underlying data.
See tests/postgresql.rs
for example use.
Without wrapper type for each unique type you store as JsonB field you
would need to use serde_json::Value
directly
or implement your own implementation for following traits:
impl<T> FromSql<sql_types::JsonB, Pg> for Json<T> {}
impl<T> ToSql<sql_types::JsonB, Pg> for Json<T> {}