postgres

package
v0.0.0-...-7c5988b Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProductNotFound = errors.New("product not found")

ErrProductNotFound is returned when a product is not found.

View Source
var ErrReviewNotFound = errors.New("product review not found")

ErrReviewNotFound is returned when a review is not found.

Functions

This section is empty.

Types

type DB

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

DB handles database communication with PostgreSQL.

func NewDB

func NewDB(pool *pgxpool.Pool, logger *slog.Logger) DB

NewDB creates a DB.

func (DB) Commit

func (db DB) Commit(ctx context.Context) error

Commit transaction from context.

func (DB) CreateProduct

func (db DB) CreateProduct(ctx context.Context, params inventory.CreateProductParams) error

CreateProduct creates a new product.

func (DB) CreateProductReview

func (db DB) CreateProductReview(ctx context.Context, params inventory.CreateProductReviewDBParams) error

CreateProductReview for a given product.

func (DB) DeleteProduct

func (db DB) DeleteProduct(ctx context.Context, id string) error

DeleteProduct from the database.

func (DB) DeleteProductReview

func (db DB) DeleteProductReview(ctx context.Context, id string) error

DeleteProductReview from the database.

func (DB) GetProduct

func (db DB) GetProduct(ctx context.Context, id string) (*inventory.Product, error)

GetProduct returns a product.

func (DB) GetProductReview

func (db DB) GetProductReview(ctx context.Context, id string) (*inventory.ProductReview, error)

GetProductReview gets a specific review.

func (DB) GetProductReviews

func (db DB) GetProductReviews(ctx context.Context, params inventory.ProductReviewsParams) (*inventory.ProductReviewsResponse, error)

GetProductReviews gets reviews for a given product or from a given user.

func (DB) Release

func (db DB) Release(ctx context.Context)

Release PostgreSQL connection acquired by context back to the pool.

func (DB) Rollback

func (db DB) Rollback(ctx context.Context) error

Rollback transaction from context.

func (DB) SearchProducts

SearchProducts returns a list of products.

func (DB) TransactionContext

func (db DB) TransactionContext(ctx context.Context) (context.Context, error)

TransactionContext returns a copy of the parent context which begins a transaction to PostgreSQL.

Once the transaction is over, you must call db.Commit(ctx) to make the changes effective. This might live in the go-pkg/postgres package later for the sake of code reuse.

func (DB) UpdateProduct

func (db DB) UpdateProduct(ctx context.Context, params inventory.UpdateProductParams) error

UpdateProduct updates an existing product.

func (DB) UpdateProductReview

func (db DB) UpdateProductReview(ctx context.Context, params inventory.UpdateProductReviewParams) error

UpdateProductReview for a given product.

func (DB) WithAcquire

func (db DB) WithAcquire(ctx context.Context) (dbCtx context.Context, err error)

WithAcquire returns a copy of the parent context which acquires a connection to PostgreSQL from pgxpool to make sure commands executed in series reuse the same database connection.

To release the connection back to the pool, you must call postgres.Release(ctx).

Example: dbCtx := db.WithAcquire(ctx) defer postgres.Release(dbCtx)

Jump to

Keyboard shortcuts

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