Skip to content

Ability to add db tags to use pgx struct scanning #366

Closed
@WilliamSoler

Description

@WilliamSoler

I am using pgx together with jet, and currently to be able to scan rows into structs I have to use AS method and write all the columns instead of using AllColumns, example:

	stmnt := ResidenceInformations.SELECT(ResidenceInformations.City.AS("City"),
		ResidenceInformations.Country.AS("Country"),
		ResidenceInformations.PostalCode.AS("ZipCode"),
		ResidenceInformations.Address.AS("Address"),
		ResidenceInformations.UserID.AS("UserID"),
	).
		WHERE(ResidenceInformations.UserID.EQ(String(id))).
		LIMIT(1)
	sql, args := stmnt.Sql()
	rows, err := as.db.Query(context.Background(), sql, args...)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	residentialAddress, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[model.ResidenceInformations])

Since to use RowToStructByName pgx functionality the resulting SQL has to match the name of the struct field or it needs a db tag to map the data into the struct.

Therefore it would be very nice to support db tags to delete a lot of this code.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions