Skip to content

feat(primitives): add sqlx PgHasArrayType for BYTEA types - #1182

Merged
DaniPopes merged 4 commits into
alloy-rs:mainfrom
pata-eth:feat/pg-array-type-support
Sep 2, 2026
Merged

DaniPopes merged 4 commits into
alloy-rs:mainfrom
pata-eth:feat/pg-array-type-support

Conversation

@pata-eth

@pata-eth pata-eth commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Motivation

Closes #1181.

I store arrays of Ethereum addresses and fixed-size hashes in Postgres as BYTEA[]. Alloy already implements sqlx Type / Encode / Decode for these primitives as scalar BYTEA, but sqlx will not treat Vec<T> as BYTEA[] unless T: PgHasArrayType. That trait lives in sqlx-postgres, which Alloy does not depend on today.

The current workaround is to bind/decode Vec<Vec<u8>> and convert with Address::from_slice / FixedBytes::try_from.

Solution

  • Add an optional sqlx-postgres feature (does not change the generic sqlx feature).
  • Implement PgHasArrayType with PgTypeInfo::array_of("bytea") for FixedBytes, Bytes, and Signed.
  • Delegate the same impl to wrap_fixed_bytes! types (Address, Bloom, Function) via impl_sqlx_postgres!.
  • sqlx’s blanket Vec<T> impls then bind/decode as BYTEA[].
  • Unit tests check Type::type_info().name() == "bytea[]" and that encoding matches Vec<Vec<u8>>.

Related but distinct: #1032 (rust-postgres ToSql/FromSql for Address).

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Enable Vec<Address> and other BYTEA-mapped primitives to bind as BYTEA[]
via an optional sqlx-postgres feature. Closes alloy-rs#1181.
@pata-eth

pata-eth commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Implements #1181.

@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Sep 1, 2026
array_of("bytea") is a name-only declaration; delegating to Vec<u8> returns the built-in OID.
@pata-eth pata-eth closed this Sep 1, 2026
@pata-eth pata-eth reopened this Sep 1, 2026
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Sep 1, 2026
@github-project-automation github-project-automation Bot moved this from Done to In Progress in Alloy Sep 1, 2026

@DaniPopes DaniPopes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

@github-project-automation github-project-automation Bot moved this from In Progress to Reviewed in Alloy Sep 2, 2026
@DaniPopes
DaniPopes merged commit a41f490 into alloy-rs:main Sep 2, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Feature] sqlx PgHasArrayType for Address and FixedBytes (BYTEA[])

3 participants