Giter VIP home page Giter VIP logo

diesel_json's Introduction

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

Add diesel_json dependency to Cargo.toml.

  • Diesel 2.0 supported: diesel_json = "0.2"
  • Diesel 1.4 supported: 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.

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

diesel_json's People

Contributors

flomonster avatar henriiik avatar jayvdb avatar ppakalns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

diesel_json's Issues

Implement `->` operator

It looks like you just created this project, and its perfect timing for me as I need a way to query json data. The only thing missing for that is support for the -> operator in postgres, to allow filtering data based on specific fields in the json data. Do you have any plans to add that?

Example from the postgres docs:

Query: '{"a": {"b":"foo"}}'::json->'a'

Result: {"b":"foo"}

Update to diesel 2.0

When changing the version to 2.0, the build fails with

> rustup run stable cargo build
    Updating crates.io index
   Compiling version_check v0.9.4
   Compiling syn v1.0.107
   Compiling serde v1.0.152
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling serde_derive v1.0.152
   Compiling diesel_derives v2.0.1
   Compiling serde_json v1.0.91
   Compiling diesel v2.0.3
   Compiling diesel_json v0.1.1 (/home/jayvdb/rust/diesel_json)
warning: #[sql_type] attribute form is deprecated
  = help: use `#[diesel(sql_type = sql_types::Jsonb)]` instead

warning: #[sql_type] attribute form is deprecated
  = help: use `#[diesel(sql_type = sql_types::Jsonb)]` instead

error[E0107]: this struct takes 1 generic argument but 2 generic arguments were supplied
  --> src/lib.rs:84:38
   |
84 |         out: &mut diesel::serialize::Output<W, Pg>,
   |                                      ^^^^^^    -- help: remove this generic argument
   |                                      |
   |                                      expected 1 generic argument
   |
note: struct defined here, with 1 generic parameter: `DB`
  --> /home/jayvdb/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-2.0.3/src/serialize.rs:35:12
   |
35 | pub struct Output<'a, 'b, DB>
   |            ^^^^^^         --

For more information about this error, try `rustc --explain E0107`.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.