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 (*Connector) BeforeConnect ¶
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.
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 ¶
func (r *DynamoQueryCacher) Get(ctx context.Context, key *pgxcache.QueryKey) (*pgxcache.QueryItem, error)
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.
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 ¶
func (r *S3QueryCacher) Get(ctx context.Context, key *pgxcache.QueryKey) (*pgxcache.QueryItem, error)
Get implements pgxcache.QueryCacher.