go-gorm / postgres

GORM PostgreSQL driver
MIT License
225 stars 119 forks source link

feat: support pgx open db options #274

Closed sasakiyori closed 2 months ago

sasakiyori commented 3 months ago

What did this pull request do?

Enable the hooks which are supported by pgx, such as BeforeConnect, AfterConnect.

User Case Description

dialector := postgres.New(postgres.Config{
    DSN: "host=localhost user=postgres password=1 dbname=postgres port=5432 sslmode=disable",
    OpenOptions: []stdlib.OptionOpenDB{
        stdlib.OptionBeforeConnect(func(ctx context.Context, cfg *pgx.ConnConfig) error {
            fmt.Println("before connect")
            // do anything with the actual pgx connection config
        return nil
        }),
    },
})

db, err := gorm.Open(dialector)
jinzhu commented 2 months ago

Thank you for your PR, but I suggest using it like https://gorm.io/docs/connecting_to_the_database.html#Existing-database-connection-1 to avoid relying too much on the driver.