Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FromRow mapping and query_*_as convenience methods#1351

Open
xhjkl wants to merge 2 commits into
rust-postgres:masterfrom
xhjkl:from-row
Open

FromRow mapping and query_*_as convenience methods#1351
xhjkl wants to merge 2 commits into
rust-postgres:masterfrom
xhjkl:from-row

Conversation

@xhjkl
Copy link
Copy Markdown

@xhjkl xhjkl commented May 21, 2026

This adds first-party row-level mapping support to tokio-postgres.

postgres-types::FromSql already handles decoding one Postgres cell into one Rust value. This change set adds the missing row-level layer: mapping a Row onto a user struct by generating the same row.try_get("field")? boilerplate users commonly write by hand.

This is intentionally not an ORM and does not use serde as the decoding contract. It preserves the existing FromSql behavior for UUID, chrono, jiff, JSON, arrays, custom types, and other feature-gated decoders.

This is how it can be useful:

use tokio_postgres::FromRow;

#[derive(FromRow)]
struct User {
    id: i64,
    name: String,
}

let users = rows
    .iter()
    .map(User::from_row)
    .collect::<Result<Vec<_>, _>>()?;

And, for owned mappings, this also adds convenience query methods:

let user = client
    .query_one_as::<User>("SELECT id, name FROM users WHERE id = $1", &[&id])
    .await?;

let users = client
    .query_as::<User>("SELECT id, name FROM users ORDER BY id", &[])
    .await?;

Borrowed mapping remain supported through direct FromRow::from_row(&row). The query_*_as convenience methods return owned values and intentionally cannot be used with row-borrowing mappings.


All in all, this covers a common slice of ORM ergonomics while staying inside the existing tokio-postgres decoding model.

@xhjkl
Copy link
Copy Markdown
Author

xhjkl commented May 21, 2026

One intentional omission: the derive macro currently does not depend on proc-macro-crate, so generated paths use tokio_postgres::* directly.

That means renamed dependencies are not handled automatically, but this keeps the new proc macro lean and matches the existing style in postgres-derive, which hardcodes postgres_types::*. If renamed-dependency support is important to maintainers, it should be straightforward to add proc-macro-crate as part of this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant