sqlx-postgres-interval

Crates.iosqlx-postgres-interval
lib.rssqlx-postgres-interval
version
sourcesrc
created_at2024-12-19 06:45:10.474583+00
updated_at2024-12-19 06:45:10.474583+00
descriptionsqlx INTERVAL Postgres type with serde Serialize/Deserialize
homepagehttps://github.com/dra11y/sqlx-postgres-interval
repositoryhttps://github.com/dra11y/sqlx-postgres-interval
max_upload_size
id1488841
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Tom Grushka (tgrushka)

documentation

README

sqlx-postgres-interval

The "current" (2024-12-18) version of sqlx (GitHub) for Postgres (0.8.2) does not derive serde::Serialize or serde::Deserialize for its type, sqlx::postgres::types::PgInterval, that represents the Postgres INTERVAL type.

Usage

Just add this crate and use its type:

use serde::{Deserialize, Serialize};
#[derive(sqlx::FromRow, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Video {
    pub id: i32,
    pub title: String,
    // ...
    pub duration: Option<sqlx_postgres_interval::Interval>,
}

Features

The chrono and time features will convert to/from their respective Durations. I haven't fully tested this; the code is copied verbatim from the current sqlx::postgres::types::PgInterval implementations.

Motivation

My database has a couple INTERVAL fields, and I don't want to have to manually implement these in my project, therefore, this crate now exists. Hopefully it will be obsoleted if serde::Serialize and serde::Deserialize get implemented for it (check at https://github.com/launchbadge/sqlx/blob/main/sqlx-postgres/src/types/interval.rs).

Unfortunately, one cannot implement serde (or any other external traits) for another external crate, hence the need to wrap the values.

Implementation

This crate wraps sqlx::postgres::types::PgInterval in its sqlx::Decode and sqlx::Encode implementations, and uses the pg_interval crate to Serialize/Deserialize it to/fron String.

Commit count: 3

cargo fmt