Pgx: Does pgx support scanning into structs?

Created on 27 May 2020  路  4Comments  路  Source: jackc/pgx

something like: https://godoc.org/github.com/jmoiron/sqlx#StructScan

And struct tags like this:

type TenantModule struct {
    Id   string `db:"id"`
    Name string `db:"type"`
        Channels Channel `json:"channels"`
}
type Channel struct {
    Channels []string
}

Or this:

type TenantModule struct {
    Id   string `sql:"id"`
    Name string `sql:"type"`
}

I didn't find any information on https://github.com/jackc/pgx/blob/master/query_test.go file and documentation.
There are only examples which scan into the basic data type like int, float

Please give an example of how to scan rows into a struct. Thanks

Most helpful comment

@mrdulin here is the library exactly for what you asked: https://github.com/georgysavva/scany
It's feature-rich and supports pgx native interface.

Disclaimer: I am the author of this library.

All 4 comments

pgx does not have anything like sqlx. But you can use sqlx with pgx when pgx is used as a database/sql driver.

Also the sqlc in next version will do what you need albeit in a bit different way that you expect.

pgx does not have anything like sqlx. But you can use sqlx with pgx when pgx is used as a database/sql driver.

While this works, it is a somewhat annoying to have to drop down from sqlx.DB to pgx.Conn by AcquireConn() in order to use the very much faster bulk-copy (CopyFrom()). In libpq, you could feed a sqlx Prepare statement with the output of pq.CopyIn() in order do do bulk-inserts.

@mrdulin here is the library exactly for what you asked: https://github.com/georgysavva/scany
It's feature-rich and supports pgx native interface.

Disclaimer: I am the author of this library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zamiell picture Zamiell  路  10Comments

kellabyte picture kellabyte  路  11Comments

kokizzu picture kokizzu  路  6Comments

jeromer picture jeromer  路  5Comments

deletexl picture deletexl  路  4Comments