Created
May 25, 2024 17:26
-
-
Save ozkansen/a39d6f83ef877f272a281c81a1fbce23 to your computer and use it in GitHub Desktop.
Go Postgresql Client: pgx custom json marshaller & unmarshaller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import ( | |
pgx "github.com/jackc/pgx/v5" | |
"github.com/jackc/pgx/v5/pgtype" | |
"github.com/jackc/pgx/v5/pgxpool" | |
) | |
func postgresCustomJSONUpgrade(conn *pgxpool.Pool) { | |
conn.Config().AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { | |
conn.TypeMap().RegisterType(&pgtype.Type{ | |
Name: "json", | |
OID: pgtype.JSONOID, | |
Codec: &pgtype.JSONCodec{ | |
Marshal: json.Marshal, | |
Unmarshal: json.Unmarshal, | |
}, | |
}) | |
conn.TypeMap().RegisterType(&pgtype.Type{ | |
Name: "jsonb", | |
OID: pgtype.JSONBOID, | |
Codec: &pgtype.JSONBCodec{ | |
Marshal: json.Marshal, | |
Unmarshal: json.Unmarshal, | |
}, | |
}) | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment