Coder Social home page Coder Social logo

diesel-chrono-duration's Introduction

diesel-chrono-duration

CI MIT licensed

This crate adds support for the chrono::Duration type into the diesel ORM.

Why

Diesel crate has a policy of including only such type implementations which can be represented as is in the SQL types. chrono::Duration does not have a direct 1-to-1 mapping in both the SQLite and PostgreSQL.

How

The chrono::Duration type stores its value as i64 number. This is exactly 8 bytes and such types are BigInteger and BigInt. So, the ToSql and FromSql traits implementation simply uses chrono::Duration's inner i64 value.

Usage

To implement this we added ChronoDurationProxy type which is just a strong type as defined as:

pub struct ChronoDurationProxy(pub chrono::Duration);

In your table model you use it instead of chrono::Duration:

extern crate diesel_chrono_duration;

use diesel_chrono_duration::ChronoDurationProxy;

#[derive(Debug, Clone, Queryable, Insertable)]
#[table_name = "sometable"]
pub struct SomeTable {
    /// The ID of the record in the table.
    pub id: i32,
    /// Some duration
    pub duration: ChronoDurationProxy,
}

Later, when you want to use it's value as chrono::Duration you have these options:

  • *duration returns a reference to the inner chrono::Duration object. It is done by the Deref trait.
  • &duration also returns a reference to the inner chrono::Duration object. It is done by the AsRef trait.
  • duration.0 as in usual rust.

Contribute

The project is very simple and small but all contributions are "please make a contribution". Thanks in advance.

License

This project is licensed under the MIT license.

diesel-chrono-duration's People

Contributors

iddm avatar

Stargazers

 avatar

Watchers

 avatar

diesel-chrono-duration's Issues

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.