Using Jiff with SQLx #4331
NuclEnergy
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Using Jiff with SQLx
This guide uses PostgreSQL
TIMESTAMPTZmapped tojiff::Timestampas the example, and explains how to use Jiff with SQLx 0.9.Key Idea
jiff-sqlxdoes not implement SQLx'sType,Encode, andDecodetraits directly forjiff::Timestamp. Instead, it provides wrapper types, such as:That means there are usually two usage patterns:
query_as!, configure type overrides insqlx.toml.query_as()/FromRow, specify#[sqlx(try_from = "...")]on the field.1. Add Dependencies
Notes:
sqlx/postgresandjiff-sqlx/postgresenable PostgreSQL support.sqlx/macrosenables compile-time checked macros such asquery!andquery_as!.sqlx/sqlx-tomlallows SQLx macros to readsqlx.toml.serdeandjiff/serdeare only needed if your structs need serialization or deserialization. They are not required for SQLx to decode timestamp values.2. Create
sqlx.tomlCreate
sqlx.tomlnext to theCargo.tomlof the crate that uses SQLx macros:In a workspace, put
sqlx.tomlin the crate that invokes the SQLx macros, not necessarily at the workspace root.SQLx macros read configuration from:
3. Using
query_as!When using the
query_as!macro, your struct field can usejiff::Timestampdirectly:Here,
created_atis a PostgreSQLTIMESTAMPTZcolumn. Becausesqlx.tomlcontains:the SQLx macro decodes the column as
jiff_sqlx::Timestamp, then calls.into()when assigning it to thejiff::Timestampfield.Important:
query_as!does not useFromRow. It generates struct literal initialization code, so this path does not require#[derive(sqlx::FromRow)].4. Using
query_as()/FromRowIf you use the non-macro form:
then SQLx uses
FromRow. If the struct is written like this:you will usually get an error similar to:
The reason is that
Decode<Postgres>is implemented forjiff_sqlx::Timestamp, not directly forjiff::Timestamp.The fix is to specify the intermediate decoded type on the field:
This means:
jiff_sqlx::Timestamp.TryFrom<jiff_sqlx::Timestamp>to convert it into the field type,jiff::Timestamp.5. Why Does
try_fromNot Actually Fail?jiff-sqlxalready implements conversion from its wrapper type to the corresponding Jiff type:The Rust standard library provides a blanket implementation for infallible conversions:
So once
jiff_sqlx::Timestamp: Into<jiff::Timestamp>is true,jiff::Timestampautomatically has:In other words,
#[sqlx(try_from = "jiff_sqlx::Timestamp")]is just the current interface exposed by SQLx'sFromRowderive. Semantically, this timestamp conversion is not actually fallible.6. Which One Should You Use?
If your query can be written as static SQL, prefer
query_as!:If you need dynamic SQL, or if you need to reuse
FromRow, addtry_fromon the timestamp field:Both approaches have the same goal: let SQLx use
jiff_sqlx::Timestampfor database encoding and decoding, while your application code continues to usejiff::Timestamp.Beta Was this translation helpful? Give feedback.
All reactions