pgxaws

package module
v0.0.0-...-3fb8803 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 18 Imported by: 0

README

pgxaws

An AWS credentials for pgx

Go Reference

Getting Started

You can use these examples to get started.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector connects the the pgx to AWS RDS.

Example
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
	panic(err)
}

ctx := context.TODO()
// Create a new pgxaws.Connector
connector, err := pgxaws.Connect(ctx)
if err != nil {
	panic(err)
}
// close the connector
defer connector.Close()

config.BeforeConnect = connector.BeforeConnect

// Create a new pgxpool with the config
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
	panic(err)
}
// close the pool
defer pool.Close()

rows, err := pool.Query(ctx, "SELECT * from organization")
if err != nil {
	panic(err)
}
// close the rows
defer rows.Close()

// Organization struct must be defined
type Organization struct {
	Name string `db:"name"`
}

for rows.Next() {
	organization, err := pgx.RowToStructByName[Organization](rows)
	if err != nil {
		panic(err)
	}

	fmt.Println(organization.Name)
}
Output:

func Connect

func Connect(ctx context.Context, options ...func(*config.LoadOptions) error) (*Connector, error)

Connect creates a new connector.

func (*Connector) BeforeConnect

func (x *Connector) BeforeConnect(ctx context.Context, config *pgx.ConnConfig) (err error)

BeforeConnect is called before a new connection is made. It is passed a copy of the underlying pgx.ConnConfig and will not impact any existing open connections.

func (*Connector) Close

func (x *Connector) Close()

Close closes the connector.

type DynamoQuery

type DynamoQuery struct {
	ID       string    `dynamo:"query_id,hash"`
	Data     []byte    `dynamo:"query_data"`
	ExpireAt time.Time `dynamo:"query_expire_at,unixtime"`
}

DynamoQuery represents a record in the dynamodb table.

type DynamoQueryCacher

type DynamoQueryCacher struct {
	// Client to interact with Dynamo DB
	Client *dynamodb.Client
	// Table name in Dynamo DB
	Table string
}

DynamoQueryCacher implements pgxcache.QueryCacher interface to use Dynamo DB.

Example
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
	panic(err)
}

conn, err := pgxpool.NewWithConfig(context.TODO(), config)
if err != nil {
	panic(err)
}
// close the connection
defer conn.Close()

// Create a new cacher
cacher := &pgxaws.DynamoQueryCacher{
	Client: NewDynamoClient(),
	Table:  "queries",
}

// create a new querier
querier := &pgxcache.Querier{
	// set the default query options, which can be overridden by the query
	// -- @cache-max-rows 100
	// -- @cache-ttl 30s
	Options: &pgxcache.QueryOptions{
		MaxLifetime: 30 * time.Second,
		MaxRows:     1,
	},
	Cacher:  cacher,
	Querier: conn,
}

rows, err := querier.Query(context.TODO(), "SELECT * from customer")
if err != nil {
	panic(err)
}
// close the rows
defer rows.Close()

// Customer struct must be defined
type Customer struct {
	FirstName string `db:"first_name"`
	LastName  string `db:"last_name"`
}

for rows.Next() {
	customer, err := pgx.RowToStructByName[Customer](rows)
	if err != nil {
		panic(err)
	}

	fmt.Println(customer.FirstName)
}
Output:

func NewDynamoQueryCacher

func NewDynamoQueryCacher(table string) *DynamoQueryCacher

NewDynamoQueryCacher creates a new instance of DynamoQueryCacher.

func (*DynamoQueryCacher) Get

Get gets a cache item from Dynamo DB. Returns pointer to the item, a boolean which represents whether key exists or not and an error.

func (*DynamoQueryCacher) Reset

Reset implements pgxcache.QueryCacher.

func (*DynamoQueryCacher) Set

Set sets the given item into Dynamo DB with provided TTL duration.

type S3QueryCacher

type S3QueryCacher struct {
	// Client to interact with S3
	Client *s3.Client
	// Bucket name in S3
	Bucket string
}

S3QueryCacher implements pgxcache.QueryCacher interface to use S3.

Example
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
	panic(err)
}

conn, err := pgxpool.NewWithConfig(context.TODO(), config)
if err != nil {
	panic(err)
}
// close the connection
defer conn.Close()

// Create a new cacher
cacher := &pgxaws.S3QueryCacher{
	Client: NewS3Client(),
	Bucket: "queries",
}

// create a new querier
querier := &pgxcache.Querier{
	// set the default query options, which can be overridden by the query
	// -- @cache-max-rows 100
	// -- @cache-ttl 30s
	Options: &pgxcache.QueryOptions{
		MaxLifetime: 30 * time.Second,
		MaxRows:     1,
	},
	Cacher:  cacher,
	Querier: conn,
}

rows, err := querier.Query(context.TODO(), "SELECT * from customer")
if err != nil {
	panic(err)
}
// close the rows
defer rows.Close()

// Customer struct must be defined
type Customer struct {
	FirstName string `db:"first_name"`
	LastName  string `db:"last_name"`
}

for rows.Next() {
	customer, err := pgx.RowToStructByName[Customer](rows)
	if err != nil {
		panic(err)
	}

	fmt.Println(customer.FirstName)
}
Output:

func (*S3QueryCacher) Get

Get implements pgxcache.QueryCacher.

func (*S3QueryCacher) Reset

func (r *S3QueryCacher) Reset(context.Context) error

Reset implements pgxcache.QueryCacher.

func (*S3QueryCacher) Set

Set implements pgxcache.QueryCacher.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL