db

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package db provides implementation of generic object and event stores.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrations

func ApplyMigrations(ctx context.Context, conn *gosql.DB, name string, g MigrationGroup, options ...MigrateOption) error

func CollectRows added in v0.2.1

func CollectRows[T any](rows Rows[T]) ([]T, error)

func GetRunner

func GetRunner(ctx context.Context, db gosql.Runner) gosql.Runner

func GetTx

func GetTx(ctx context.Context) *sql.Tx

func WithRunner

func WithRunner(ctx context.Context, db gosql.Runner) context.Context

func WithTx

func WithTx(ctx context.Context, tx *sql.Tx) context.Context

func WithZeroMigration

func WithZeroMigration(state []migrationState, beginPos, endPos *int) error

Types

type EventConsumer

type EventConsumer[T any, TPtr EventPtr[T]] interface {
	// BeginEventID should return smallest ID of next possibly consumed event.
	BeginEventID() int64
	// ConsumeEvents should consume new events.
	ConsumeEvents(ctx context.Context, fn func(T) error) error
}

EventConsumer represents consumer for events.

func NewEventConsumer

func NewEventConsumer[T any, TPtr EventPtr[T]](
	store EventROStore[T], beginID int64,
) EventConsumer[T, TPtr]

NewEventConsumer creates consumer for event store.

TODO(udovin): Add support for gapSkipTimeout. TODO(udovin): Add support for limit.

type EventPtr

type EventPtr[T any] interface {
	*T
	// EventID should return sequential ID of event.
	EventID() int64
	// SetEventID should set sequential ID of event.
	SetEventID(int64)
	// EventTime should return time when event occurred.
	EventTime() time.Time
}

EventPtr represents a mutable event from store.

type EventROStore

type EventROStore[T any] interface {
	// LastEventID should return last event ID or sql.ErrNoRows
	// if there is no events.
	LastEventID(ctx context.Context) (int64, error)
	// LoadEvents should load events from store in specified range.
	LoadEvents(ctx context.Context, ranges []EventRange) (Rows[T], error)
}

EventROStore represents read-only store for events.

type EventRange

type EventRange struct {
	// Begin contains begin of range.
	//
	// Begin can not be greater than End and can not be less than 0.
	Begin int64
	// End contains end of range.
	//
	// If End == 0, then there is no upper limit.
	End int64
}

EventRange represents range [begin, end).

type EventStore

type EventStore[T any, TPtr EventPtr[T]] interface {
	EventROStore[T]
	// CreateEvent should create a new event and return copy
	// that has correct ID.
	CreateEvent(ctx context.Context, event TPtr) error
}

EventStore represents persistent store for events.

func NewEventStore

func NewEventStore[T any, TPtr EventPtr[T]](id, table string, db *gosql.DB) EventStore[T, TPtr]

NewEventStore creates a new store for events of specified type.

type FindObjectsOption

type FindObjectsOption interface {
	UpdateSelect(query gosql.SelectQuery)
}

type FindQuery

type FindQuery struct {
	Where   gosql.BoolExpr
	Limit   int
	OrderBy []any
}

func (FindQuery) UpdateSelect

func (q FindQuery) UpdateSelect(query gosql.SelectQuery)

type MigrateOption

type MigrateOption func(state []migrationState, beginPos, endPos *int) error

func WithFromMigration

func WithFromMigration(name string) MigrateOption

func WithMigration

func WithMigration(name string) MigrateOption

type Migration

type Migration interface {
	// Apply should apply database migration.
	Apply(ctx context.Context, conn *gosql.DB) error
	// Unapply should unapply database migration.
	Unapply(ctx context.Context, conn *gosql.DB) error
}

Migration represents database migration.

func NewMigration

func NewMigration(operations []schema.Operation) Migration

type MigrationGroup

type MigrationGroup interface {
	// Register registers new migration to group.
	AddMigration(name string, m Migration)
	// GetMigration returns migration by name.
	GetMigration(name string) Migration
	// GetMigrations returns migrations by their order.
	GetMigrations() []NamedMigration
}

MigrationGroup represents group of database migrations.

func NewMigrationGroup

func NewMigrationGroup() MigrationGroup

type NamedMigration

type NamedMigration struct {
	Name      string
	Migration Migration
}

type ObjectPtr

type ObjectPtr[T any] interface {
	*T
	// ObjectID should return sequential ID of object.
	ObjectID() int64
	// SetObjectID should set sequential ID of object.
	SetObjectID(int64)
}

Object represents an object from store.

type ObjectROStore

type ObjectROStore[T any] interface {
	// LoadObjects should load objects from store.
	LoadObjects(ctx context.Context) (Rows[T], error)
	// FindObjects should find objects with specified expression.
	FindObjects(ctx context.Context, options ...FindObjectsOption) (Rows[T], error)
	// FindObject should bind one object with specified expression.
	FindObject(ctx context.Context, options ...FindObjectsOption) (T, error)
}

ObjectROStore represents read-only store for objects.

type ObjectStore

type ObjectStore[T any, TPtr ObjectPtr[T]] interface {
	ObjectROStore[T]
	// CreateObject should create a new object set valid ID.
	CreateObject(ctx context.Context, object TPtr) error
	// UpdateObject should update object with specified ID.
	UpdateObject(ctx context.Context, object TPtr) error
	// DeleteObject should delete existing object from the store.
	DeleteObject(ctx context.Context, id int64) error
}

ObjectStore represents persistent store for objects.

func NewObjectStore

func NewObjectStore[T any, TPtr ObjectPtr[T]](
	id, table string, db *gosql.DB,
) ObjectStore[T, TPtr]

NewObjectStore creates a new store for objects of specified type.

type Rows

type Rows[T any] interface {
	// Next should read next event and return true if event exists.
	Next() bool
	// Row should return current row.
	Row() T
	// Close should close reader.
	Close() error
	// Err should return error that occurred during reading.
	Err() error
}

Rows represents reader for events.

func NewSliceRows

func NewSliceRows[T any](rows []T) Rows[T]

type WithLimit

type WithLimit int

func (WithLimit) UpdateSelect

func (q WithLimit) UpdateSelect(query gosql.SelectQuery)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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