pgxgcp

package module
v0.0.0-...-305ee66 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 13 Imported by: 0

README

pgxgcp

Use Google Cloud SQL 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 to a Cloud SQL instance using the Cloud SQL Proxy.

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

ctx := context.TODO()
// Create a new pgxgcp.Connector
connector, err := pgxgcp.Connect(ctx)
if err != nil {
	panic(err)
}

// Set BeforeConnect hook to pgxaws.BeforeConnect
config.BeforeConnect = connector.BeforeConnect

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

rows, err := conn.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)
}

func Connect

func Connect(ctx context.Context, options ...cloudsqlconn.Option) (*Connector, error)

Connect creates a new Connector using the provided options.

func (*Connector) BeforeConnect

func (x *Connector) BeforeConnect(ctx context.Context, conn *pgx.ConnConfig) 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.

type DatastoreQuery

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

DatastoreQuery represents a record in the dynamodb table.

type DatastoreQueryCacher

type DatastoreQueryCacher struct {
	// Client is the Datastore client.
	Client *datastore.Client
	// Kind is the name of the kind in Datastore.
	Kind string
}

DatastoreQueryCacher implements pgxcache.QueryCacher interface to use Google Datastore.

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 client
client, err := datastore.NewClient(context.TODO(), os.Getenv("GOOGLE_PROJECT_ID"))
if err != nil {
	panic(err)
}

// Create a new cacher
cacher := &pgxgcp.DatastoreQueryCacher{
	Client: client,
	Kind:   "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)
}

func (*DatastoreQueryCacher) Get

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

func (*DatastoreQueryCacher) Reset

Reset implements pgxcache.QueryCacher.

func (*DatastoreQueryCacher) Set

Set sets the given item into Google Datastore with provided TTL duration.

type FirestoreQuery

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

FirestoreQuery represents a record in the dynamodb table.

type FirestoreQueryCacher

type FirestoreQueryCacher struct {
	// Client is the Firestore client.
	Client *firestore.Client
	// Collection is the name of the collection in Firestore.
	Collection string
}

FirestoreQueryCacher implements pgxcache.QueryCacher interface to use Google Firestore.

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 client
client, err := firestore.NewClient(context.TODO(), os.Getenv("GOOGLE_PROJECT_ID"))
if err != nil {
	panic(err)
}

// Create a new cacher
cacher := &pgxgcp.FirestoreQueryCacher{
	Client:     client,
	Collection: "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)
}

func (*FirestoreQueryCacher) Get

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

func (*FirestoreQueryCacher) Reset

Reset implements pgxcache.QueryCacher.

func (*FirestoreQueryCacher) Set

Set sets the given item into Google Firestore with provided TTL duration.

type StorageQueryCacher

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

StorageQueryCacher implements pgxcache.QueryCacher interface to use Google Cloud Storage.

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 client
client, err := storage.NewClient(context.TODO())
if err != nil {
	panic(err)
}

// Create a new cacher
cacher := &pgxgcp.StorageQueryCacher{
	Client: client,
	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)
}

func (*StorageQueryCacher) Get

Get implements pgxcache.QueryCacher.

func (*StorageQueryCacher) Reset

Reset implements pgxcache.QueryCacher.

func (*StorageQueryCacher) Set

Set implements pgxcache.QueryCacher.

Jump to

Keyboard shortcuts

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