jet icon indicating copy to clipboard operation
jet copied to clipboard

Ability to add db tags to use pgx struct scanning

Open WilliamSoler opened this issue 1 year ago • 2 comments
trafficstars

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.

WilliamSoler avatar Jul 20 '24 07:07 WilliamSoler