-
Notifications
You must be signed in to change notification settings - Fork 0
/
crate.go
45 lines (35 loc) · 899 Bytes
/
crate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package crate
import (
"context"
"errors"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
func NewCrate(ctx context.Context, config *pgxpool.Config) (c Crate, err error) {
if config == nil {
err = errors.New("Missing config")
return
}
afterConnect := config.AfterConnect
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
registerDataTypes(conn)
if afterConnect != nil {
return afterConnect(ctx, conn)
}
return nil
}
c.pool, err = pgxpool.ConnectConfig(ctx, config)
return
}
type Crate struct {
pool *pgxpool.Pool
}
// Register "JSON Array" (OID 199) type
func registerDataTypes(conn *pgx.Conn) {
conn.ConnInfo().RegisterDataType(pgtype.DataType{
Value: pgtype.NewArrayType("__json", pgtype.JSONOID, func() pgtype.ValueTranscoder { return &pgtype.JSON{} }),
Name: "__json",
OID: 199,
})
}