database

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DBName = "checkout"
View Source
const InMemoryDSN = ":memory:"

Variables

View Source
var ErrOutboxItemNotFound = errors.New("outbox item not found")

ErrOutboxItemNotFound is returned when a strict update matches no row.

Functions

This section is empty.

Types

type Database

type Database interface {
	HealthChecker
	InventoryStore
	OrderStore
	OutboxStore
	Transaction(ctx context.Context, fn func(Database) error) error
}

type GormDB

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

func NewGormDB

func NewGormDB(d gorm.Dialector, recreateSchema bool) (*GormDB, error)

func NewPostgresDB

func NewPostgresDB(host, user, password string, port int) (*GormDB, error)

NewPostgresDB returns a new GormDB with PostgreSQL engine.

func NewSQLiteDB

func NewSQLiteDB(databaseDSN string, recreateSchema bool) (*GormDB, error)

NewSQLiteDB returns a new GormDB with SQLite engine.

func (*GormDB) AddOrder

func (g *GormDB) AddOrder(ctx context.Context, o *model.Order) error

func (*GormDB) AddOutboxItems added in v0.7.0

func (g *GormDB) AddOutboxItems(ctx context.Context, items []*model.OutboxItem) error

func (*GormDB) GetItemByName

func (g *GormDB) GetItemByName(ctx context.Context, name string) (*model.Item, error)

func (*GormDB) GetItemBySKU

func (g *GormDB) GetItemBySKU(ctx context.Context, sku string) (*model.Item, error)

func (*GormDB) GetItemsBySKU

func (g *GormDB) GetItemsBySKU(ctx context.Context, skus []string) ([]*model.Item, error)

func (*GormDB) GetOrders

func (g *GormDB) GetOrders(ctx context.Context, customerID string) ([]*model.Order, error)

func (*GormDB) GetOutboxItems added in v0.7.0

func (g *GormDB) GetOutboxItems(ctx context.Context, q *OutboxQuery) ([]*model.OutboxItem, error)

func (*GormDB) ListItems added in v0.5.0

func (g *GormDB) ListItems(ctx context.Context) ([]*model.Item, error)

func (*GormDB) Ping

func (g *GormDB) Ping(ctx context.Context) error

func (*GormDB) SetDeliveredAt added in v0.7.0

func (g *GormDB) SetDeliveredAt(ctx context.Context, id int64, t time.Time) error

func (*GormDB) SetDeliveredByEventID added in v0.7.0

func (g *GormDB) SetDeliveredByEventID(ctx context.Context, eventID string, t time.Time) error

SetDeliveredByEventID marks the outbox row with the given event ID delivered. The notifier holds the event ID (not the row's serial ID), so it marks delivery by that. Strict: matching no row is an error.

func (*GormDB) SetPublishedAt added in v0.7.0

func (g *GormDB) SetPublishedAt(ctx context.Context, id int64, t time.Time) error

func (*GormDB) Transaction added in v0.7.0

func (g *GormDB) Transaction(ctx context.Context, fn func(Database) error) error

func (*GormDB) UpsertItems

func (g *GormDB) UpsertItems(ctx context.Context, items []*model.Item) ([]*model.Item, error)

type HealthChecker

type HealthChecker interface {
	Ping(ctx context.Context) error
}

type InventoryStore

type InventoryStore interface {
	UpsertItems(ctx context.Context, items []*model.Item) ([]*model.Item, error)
	ListItems(ctx context.Context) ([]*model.Item, error) // TODO - add pagination support
	GetItemByName(ctx context.Context, name string) (*model.Item, error)
	GetItemBySKU(ctx context.Context, sku string) (*model.Item, error)
	GetItemsBySKU(ctx context.Context, sku []string) ([]*model.Item, error)
}

type OrderStore

type OrderStore interface {
	AddOrder(ctx context.Context, o *model.Order) error
	GetOrders(ctx context.Context, userID string) ([]*model.Order, error)
}

type OutboxQuery added in v0.7.0

type OutboxQuery struct {
	// OnlyUnpublished restricts to rows not yet sent to the broker
	// (published_at IS NULL). This is the relay's claim filter.
	OnlyUnpublished bool
	// OnlyUndelivered restricts to rows not yet marked delivered
	// (delivered_at IS NULL).
	OnlyUndelivered bool
	// Limit caps the batch size; <= 0 means no limit.
	Limit int
}

OutboxQuery filters an outbox read. The zero value selects everything.

type OutboxStore added in v0.7.0

type OutboxStore interface {
	// AddOutboxItems enqueues items. Intended to run inside the same
	// transaction as the business write it accompanies.
	AddOutboxItems(ctx context.Context, items []*model.OutboxItem) error

	// GetOutboxItems reads items, optionally filtered to those not yet
	// published or delivered. Results are ordered by ID ascending (enqueue
	// order).
	GetOutboxItems(ctx context.Context, q *OutboxQuery) ([]*model.OutboxItem, error)

	// SetPublishedAt strictly marks one item published: it errors with
	// ErrOutboxItemNotFound if no row has that ID.
	SetPublishedAt(ctx context.Context, id int64, t time.Time) error

	// SetDeliveredAt strictly marks one item delivered, with the same
	// not-found semantics as SetPublishedAt.
	SetDeliveredAt(ctx context.Context, id int64, t time.Time) error

	// SetDeliveredByEventID marks the item with the given event ID delivered.
	// The notifier consumes events keyed by event ID, so it marks delivery by
	// that rather than the row's serial ID.
	SetDeliveredByEventID(ctx context.Context, eventID string, t time.Time) error
}

OutboxStore persists and drains transactional outbox rows.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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