Skip to content

Commit

Permalink
Added example and test for pgx pool - #18
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 23, 2024
1 parent 39bcf83 commit ff8c3ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ Enable the extension
_, err := conn.Exec(ctx, "CREATE EXTENSION IF NOT EXISTS vector")
```

Register the types
Register the types with the connection

```go
err := pgxvector.RegisterTypes(ctx, conn)
```

or the pool

```go
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
return pgxvector.RegisterTypes(ctx, conn)
}
```

Create a table

```go
Expand Down
23 changes: 23 additions & 0 deletions pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pgvector/pgvector-go"
pgxvector "github.com/pgvector/pgvector-go/pgx"
)
Expand Down Expand Up @@ -146,4 +147,26 @@ func TestPgx(t *testing.T) {
if err != nil {
panic(err)
}

config, err := pgxpool.ParseConfig("postgres://localhost/pgvector_go_test")
if err != nil {
panic(err)
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
return pgxvector.RegisterTypes(ctx, conn)
}
pool, err := pgxpool.NewWithConfig(ctx, config)
defer pool.Close()

_, err = pool.CopyFrom(
ctx,
pgx.Identifier{"pgx_items"},
[]string{"embedding", "binary_embedding", "sparse_embedding"},
pgx.CopyFromSlice(1, func(i int) ([]any, error) {
return []interface{}{"[1,2,3]", "101", "{1:1,2:2,3:3}/3"}, nil
}),
)
if err != nil {
panic(err)
}
}

0 comments on commit ff8c3ee

Please sign in to comment.