jiff_sqlx/
lib.rs

1/*!
2This crate provides integration points for [Jiff](jiff) and [SQLx][sqlx].
3
4Examples can be found in the
5[examples directory of the Jiff repository][examples].
6
7Note that to use this crate, you'll likely need to enable one of its
8[database backend features](#crate-features).
9
10# Organization
11
12This crates defines several types that wrap corresponding types in Jiff. Each
13wrapper type provides implementations of traits found in SQLx. For most types,
14these are the [`sqlx_core::types::Type`], [`sqlx_core::decode::Decode`] and
15[`sqlx_core::encode::Encode`] traits.
16
17The intended workflow is to use these wrapper types within your wire types for
18encoding and decoding data from databases such as PostgreSQL. The wrapper types
19own the logic for encoding and decoding the data in database specific formats.
20
21In order to the minimize the annoyance of wrapper types, the following
22conveniences are afforded:
23
24* A [`ToSqlx`] trait is provided. Several Jiff types implement this trait. The
25trait provides easy conversion to the corresponding wrapper type in this crate.
26* A concrete `to_jiff` method is provided on each wrapper type. For example,
27[`Timestamp::to_jiff`]. This method is the reverse of `ToSqlx`. This converts
28from the wrapper type to the corresponding Jiff type.
29* There are `From` trait implementations from the wrapper type to the
30corresponding Jiff type, and vice versa.
31
32# Database support
33
34At present, both PostgreSQL and SQLite are supported.
35
36Ideally, MySQL support would be present too, but it
37[appears impossible until SQLx exposes some APIs][sqlx-mysql-bunk].
38
39# Future
40
41This crate exists because there are generally only three ways to implement
42the necessary traits in SQLx:
43
441. Make Jiff depend on SQLx, and implement the corresponding traits where
45Jiff's types are defined.
462. Make SQLx depend on Jiff, and implement the corresponding traits where the
47traits are defined.
483. Make a crate like this one with types that wrap Jiff's types, and implements
49the corresponding traits for the wrapper types.
50
51This was done because it seems inappropriate for a "lower level" crate like
52Jiff to depend on SQLx. And while it might be appropriate for SQLx to optionally
53depend on Jiff (like it does for [`chrono`] or [`time`]), at time of writing,
54Jiff is still early in its life. It's totally reasonable to wait for it to
55mature. Plus, the thought of three different datetime integrations is,
56admittedly, tough to stomach.
57
58In the future, it may be prudent for this crate to be upstreamed into SQLx
59itself.
60
61# Crate features
62
63* **postgres** - Enables the `sqlx-postgres` dependency.
64* **sqlite** - Enables the `sqlx-sqlite` dependency.
65
66[sqlx]: https://docs.rs/sqlx/0.8
67[examples]: https://github.com/BurntSushi/jiff/tree/master/examples
68[`chrono`]: https://docs.rs/chrono
69[`time`]: https://docs.rs/time
70[sqlx-mysql-bunk]: https://github.com/launchbadge/sqlx/issues/3487#issuecomment-2641843693
71*/
72
73#![deny(missing_docs)]
74
75pub use self::wrappers::{Date, DateTime, Span, Time, Timestamp, ToSqlx};
76
77#[cfg(feature = "postgres")]
78mod postgres;
79#[cfg(feature = "sqlite")]
80mod sqlite;
81mod wrappers;