Posted on Liked https://boinkor.net/2024/04/some-useful-types-for-database-using-rust-web-apps/

I've been meaning to try out this IdType trait pattern. My SQLx usage so far somewhat benefits from different structs for to-write-data and read-data so I haven't quite gotten around to testing it out. via

Filed under: this-week-in-rust sqlx

Posted on Liked http://www.matildasmeds.com/posts/no-more-unchecked-sqlx-queries/

I love sqlx and type checked queries but always found it annoying to duplicate a bunch of queries vs using the dynamic query builder (which isn't type checked). Using Option and NULLs seems to improve things though!

// Postgres version
let ids = sqlx::query_as!(
    Uuid,
    "SELECT id FROM users \
     WHERE ($1::timestamptz IS NULL OR updated_at < $1) \
       AND ($2::timestamptz IS NULL OR updated_at > $2) \
       AND ($3::boolean IS NULL OR is_guest = $3)",
    updated_before_option,
    updated_after_option,
    is_guest_option,
    )
    .fetch_all(&pool)
    .await;

via