Skip to content

Wrapper type to use with JsonB fields in diesel Queryable, Insertable structures

License

Notifications You must be signed in to change notification settings

evilsocket/diesel_json

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

diesel_json

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.

Getting started

Wrap data structures into diesel_json::Json type.

#[derive(Serialize, Deserialize, Debug)]
struct ComplexStruct {
  // ...
}

#[derive(Serialize, Deserialize, Queryable, Insertable, AsChangeset, Identifiable)]
struct ExampleTable {
    // ...
    // Field that will be stored in Jsonb format
    jsonb_field: diesel_json::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.

Why should I use this library?

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> {}

TODO:

  • Support not only JsonB, but also Json postgres type
  • Support more database drivers
    • PostgreSQL
    • MySql
    • SQLite
  • Improve testing of the library
    • Test insertion and retrieval of nullable/non-nullable JsonB postgresql fields.
    • Add support and test Json field insertion and retrieval
    • TODO(Expand): Add support for other database driver Json fields

About

Wrapper type to use with JsonB fields in diesel Queryable, Insertable structures

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 82.9%
  • PLpgSQL 15.9%
  • Shell 1.2%