postgres

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

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, uri string) error

Connect connects to postgres.

func UpdateBook

func UpdateBook(ctx context.Context, id int64, fields Fields) error

UpdateBook updates given fields on book.

func UpsertBook

func UpsertBook(ctx context.Context, params UpsertBookParams) (int64, error)

UpsertBook creates book in postgres and returns ID.

If row with the same ISBN already exists it will just update fields of existing row and returns id of old book.

Types

type Book

type Book struct {
	ID          int64                     `db:"id"`
	ISBN        pgtype.Text               `db:"isbn"`
	URL         pgtype.Text               `db:"url"`
	Title       pgtype.Text               `db:"title"`
	Image       pgtype.Text               `db:"image"`
	Description pgtype.Text               `db:"description"`
	Authors     pgtype.Array[pgtype.Text] `db:"authors"`
	Publisher   pgtype.Text               `db:"publisher"`
	Properties  map[string]string         `db:"properties"`
	CreatedAt   pgtype.Timestamp          `db:"created_at"`
	UpdatedAt   pgtype.Timestamp          `db:"updated_at"`
}

Book represents book table in postgres.

func FindBooks

func FindBooks(ctx context.Context, filter any) ([]*Book, error)

FindBooks returns books by given filter.

Use squirrel for filtering, e.g. postgres.FindBooks(ctx, sq.Eq{"published": false}) to get books that aren't published yet.

func GetBook

func GetBook(ctx context.Context, filter any) (*Book, error)

GetBook returns first found book by given filter.

Use squirrel for filtering, e.g. postgres.GetBook(ctx, sq.Eq{"id": id}) to get book by id.

type DB

type DB interface {
	Begin(ctx context.Context) (pgx.Tx, error)
	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

DB is postgres database.

type Fields

type Fields = map[string]any

Fields is used for update queries.

type UpsertBookParams

type UpsertBookParams struct {
	ISBN        string
	URL         string
	Title       string
	Image       string
	Description string
	Authors     []string
	Publisher   string
	Properties  map[string]string
}

UpsertBookParams is parameters required for inserting book.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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