Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WilliamSoler opened this issue Jul 20, 2024 · 2 comments
Open

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

WilliamSoler opened this issue Jul 20, 2024 · 2 comments

Comments

@WilliamSoler
Copy link

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.

@houten11
Copy link

This is already supported, using generator customization - https://github.com/go-jet/jet/wiki/Generator#generator-customization.

@go-jet
Copy link
Owner

go-jet commented Jul 24, 2024

Hi @WilliamSoler, check this issue - #364. When this issues is resolved you should be able to write:

	stmnt := ResidenceInformations.SELECT(ResidenceInformations.AllColumns.As("")).
		WHERE(ResidenceInformations.UserID.EQ(String(id))).
		LIMIT(1)

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

No branches or pull requests

3 participants