eventdb

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package eventdb has support functions for implementing the abstract types

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTxID

func GetTxID(tx any) string

func PreAllocateIDMap added in v0.2.0

func PreAllocateIDMap[T any](events ...eventmodels.ProducingEvent) map[string][]T

func Transact

func Transact[ID eventmodels.AbstractID[ID], TX BasicTX, DB ComboDB[ID, TX]](
	ctx context.Context,
	db DB,
	backupTracer eventmodels.Tracer,
	f func(TX) error,
	saveEvents SaveEventsFunc[ID, TX],
	optProducer eventmodels.Producer[ID, TX],
) error

Transact implements a Transact method as needed by AbstractDB (in ComboDB). It does not call itself recursively and insteads depends upon BeginTx from BasicDB (in ComboDB). backupTracer is only used if optProducer is nil.

func ValidateEventTopics added in v0.3.0

func ValidateEventTopics[ID eventmodels.AbstractID[ID], TX eventmodels.AbstractTX](
	ctx context.Context,
	optProducer eventmodels.Producer[ID, TX],
	events ...eventmodels.ProducingEvent,
) error

ValidateEventTopics returns an error if the topics are not valid. It only checks if a Producer is provided.

func WrapTransaction

func WrapTransaction[ID eventmodels.AbstractID[ID], TX BasicTX, DB BasicDB[TX]](
	ctx context.Context,
	backupTracer eventmodels.Tracer,
	db DB,
	f func(TX) error,
	saveEvents SaveEventsFunc[ID, TX],
	optProducer eventmodels.Producer[ID, TX],
) (map[string][]ID, error)

WrapTransaction is a building block that can be shared between database-sprecific implementations. It handles the Begin/Rollback/Commit sequence and saving events. backupTracer is only used if optProducer is nil

Types

type BasicDB

type BasicDB[TX JustSQLTX] interface {
	eventmodels.AbstractTX
	BeginTx(context.Context, *sql.TxOptions) (TX, error)
}

BasicDB exists to allow the type of transactions to be overridden.

type BasicTX

type BasicTX interface {
	JustSQLTX
	GetPendingEvents() []eventmodels.ProducingEvent
}

BasicTX is what's needed from a transaction in WrapTransaction

type ComboDB

type ComboDB[ID eventmodels.AbstractID[ID], TX BasicTX] interface {
	BasicDB[TX]
	eventmodels.AbstractDB[ID, TX]
}

ComboDB has both the low-level BeginTx and the high-level methods required of AbstractDB

type ExampleBasicDB

type ExampleBasicDB struct {
	*sql.DB
}

ExampleBasicDB wraps a *sql.DB but does not implment AugmentWithProducer

func (ExampleBasicDB) BeginTx

func (db ExampleBasicDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*ExampleBasicTX, error)

type ExampleBasicTX

type ExampleBasicTX struct {
	*sql.Tx
	// contains filtered or unexported fields
}

func (*ExampleBasicTX) GetPendingEvents

func (tx *ExampleBasicTX) GetPendingEvents() []eventmodels.ProducingEvent

func (*ExampleBasicTX) Produce

func (tx *ExampleBasicTX) Produce(events ...eventmodels.ProducingEvent)

type JustSQLTX

type JustSQLTX interface {
	eventmodels.AbstractTX
	Commit() error
	Rollback() error
}

type ProducingEventBlob

type ProducingEventBlob[ID eventmodels.AbstractID[ID]] struct {
	K              string
	TS             time.Time
	KafkaTopic     string
	Data           []byte
	ID             ID
	EncodedHeader  []byte
	HeaderMap      map[string][]string
	SequenceNumber int
}

ProducingEventBlob is used to bridge from pending events stored in the database to calls to Produce() to actually send the event

func (ProducingEventBlob[ID]) GetHeaders

func (e ProducingEventBlob[ID]) GetHeaders() map[string][]string

func (ProducingEventBlob[ID]) GetKey

func (e ProducingEventBlob[ID]) GetKey() string

func (ProducingEventBlob[ID]) GetTimestamp

func (e ProducingEventBlob[ID]) GetTimestamp() time.Time

func (ProducingEventBlob[ID]) GetTopic

func (e ProducingEventBlob[ID]) GetTopic() string

func (ProducingEventBlob[ID]) MarshalJSON

func (e ProducingEventBlob[ID]) MarshalJSON() ([]byte, error)

type SaveEventsFunc

type SaveEventsFunc[ID eventmodels.AbstractID[ID], TX BasicTX] func(context.Context, eventmodels.Tracer, TX, eventmodels.Producer[ID, TX], ...eventmodels.ProducingEvent) (map[string][]ID, error)

SaveEventsFunc is used to preserve events from a transaction. It needs to work if the producer is nil. Tracer can be ignored if producer is not nil.

Jump to

Keyboard shortcuts

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