sqlprojection

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 11 Imported by: 3

Documentation

Overview

Package sqlprojection provides utilities for building SQL-based projections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSchema added in v0.6.1

func CreateSchema(ctx context.Context, db *sql.DB, options ...Option) error

CreateSchema creates the schema elements necessary to store projections on the given database.

It does not return an error if the schema already exists.

If no candidate drivers are provided all built-in drivers are considered as candidates.

func DropSchema added in v0.6.1

func DropSchema(ctx context.Context, db *sql.DB, options ...Option) error

DropSchema drops the schema elements necessary to store projections on the given database.

It does not return an error if the schema does not exist.

If no candidate drivers are provided all built-in drivers are considered as candidates.

func New

func New(
	db *sql.DB,
	h MessageHandler,
	options ...Option,
) dogma.ProjectionMessageHandler

New returns a new Dogma projection message handler by binding an SQL-specific projection handler to an SQL database.

If db is nil the returned handler will return an error whenever an operation that requires the database is performed.

By default an appropriate Driver implementation is chosen from the built-in drivers listed in the Drivers slice.

Types

type Driver

type Driver interface {
	// IsCompatibleWith returns nil if this driver can be used to store
	// projections on db.
	IsCompatibleWith(ctx context.Context, db *sql.DB) error

	// CreateSchema creates the schema elements required by the driver.
	CreateSchema(ctx context.Context, db *sql.DB) error

	// DropSchema drops the schema elements required by the driver.
	DropSchema(ctx context.Context, db *sql.DB) error

	// StoreVersion unconditionally updates the version for a specific handler
	// and resource.
	//
	// v must be non-empty, to set an empty version, use DeleteResource().
	StoreVersion(
		ctx context.Context,
		db *sql.DB,
		h string,
		r, v []byte,
	) error

	// UpdateVersion updates the version for a specific handler and resource.
	UpdateVersion(
		ctx context.Context,
		tx *sql.Tx,
		h string,
		r, c, n []byte,
	) (bool, error)

	// QueryVersion returns the version for a specific handler and resource.
	QueryVersion(
		ctx context.Context,
		db *sql.DB,
		h string,
		r []byte,
	) ([]byte, error)

	// DeleteResource removes the version for a specific handler and resource.
	DeleteResource(
		ctx context.Context,
		db *sql.DB,
		h string,
		r []byte,
	) error
}

Driver is an interface for database-specific projection drivers.

var MySQLDriver Driver = mysqlDriver{}

MySQLDriver is a Driver for MySQL.

This driver should work with any underlying Go SQL driver that supports MySQL compatible databases and ?-style placeholders.

var PostgresDriver Driver = postgresDriver{}

PostgresDriver is a Driver for PostgreSQL.

This driver should work with any underlying Go SQL driver that supports PostgreSQL compatible databases and $1-style placeholders.

var SQLiteDriver Driver = sqliteDriver{}

SQLiteDriver is Driver for SQLite.

This driver should work with any underlying Go SQL driver that supports SQLite v3 compatible databases and $1-style placeholders.

func BuiltInDrivers

func BuiltInDrivers() []Driver

BuiltInDrivers returns a list of the built-in drivers.

func SelectDriver

func SelectDriver(ctx context.Context, db *sql.DB, candidates []Driver) (Driver, error)

SelectDriver returns the appropriate driver implementation to use with the given database from a list of candidate drivers.

type MessageHandler

type MessageHandler interface {
	// Configure produces a configuration for this handler by calling methods on
	// the configurer c.
	//
	// The implementation MUST allow for multiple calls to Configure(). Each
	// call SHOULD produce the same configuration.
	//
	// The engine MUST call Configure() before calling HandleEvent(). It is
	// RECOMMENDED that the engine only call Configure() once per handler.
	Configure(c dogma.ProjectionConfigurer)

	// HandleEvent updates the projection to reflect the occurrence of an event.
	//
	// Changes to the projection state MUST be performed within the supplied
	// transaction.
	//
	// If nil is returned, the projection state has been persisted successfully.
	//
	// If an error is returned, the projection SHOULD be left in the state it
	// was before HandleEvent() was called.
	//
	// The engine SHOULD provide "at-least-once" delivery guarantees to the
	// handler. That is, the engine should call HandleEvent() with the same
	// event message until a nil error is returned.
	//
	// The engine MAY provide guarantees about the order in which event messages
	// will be passed to HandleEvent(), however in the interest of engine
	// portability the implementation SHOULD NOT assume that HandleEvent() will
	// be called with events in the same order that they were recorded.
	//
	// The supplied context parameter SHOULD have a deadline. The implementation
	// SHOULD NOT impose its own deadline. Instead a suitable timeout duration
	// can be suggested to the engine via the handler's TimeoutHint() method.
	//
	// The engine MUST NOT call HandleEvent() with any message of a type that
	// has not been configured for consumption by a prior call to Configure().
	// If any such message is passed, the implementation MUST panic with the
	// UnexpectedMessage value.
	//
	// The engine MAY call HandleEvent() from multiple goroutines concurrently.
	HandleEvent(ctx context.Context, tx *sql.Tx, s dogma.ProjectionEventScope, m dogma.Message) error

	// TimeoutHint returns a duration that is suitable for computing a deadline
	// for the handling of the given message by this handler.
	//
	// The hint SHOULD be as short as possible. The implementation MAY return a
	// zero-value to indicate that no hint can be made.
	//
	// The engine SHOULD use a duration as close as possible to the hint. Use of
	// a duration shorter than the hint is NOT RECOMMENDED, as this will likely
	// lead to repeated message handling failures.
	TimeoutHint(m dogma.Message) time.Duration

	// Compact reduces the size of the projection's data.
	//
	// The implementation SHOULD attempt to decrease the size of the
	// projection's data by whatever means available. For example, it may delete
	// any unused data, or collapse multiple data sets into one.
	//
	// The context MAY have a deadline. The implementation SHOULD compact data
	// using multiple small transactions, such that if the deadline is reached a
	// future call to Compact() does not need to compact the same data.
	//
	// The engine SHOULD call Compact() repeatedly throughout the lifetime of
	// the projection. The precise scheduling of calls to Compact() are
	// engine-defined. It MAY be called concurrently with any other method.
	Compact(ctx context.Context, db *sql.DB, s dogma.ProjectionCompactScope) error
}

MessageHandler is a specialization of dogma.ProjectionMessageHandler that persists to an SQL database.

type NoCompactBehavior

type NoCompactBehavior struct{}

NoCompactBehavior can be embedded in MessageHandler implementations to indicate that the projection does not require its data to be compacted.

It provides an implementation of MessageHandler.Compact() that always returns a nil error.

func (NoCompactBehavior) Compact

Compact returns nil.

type Option

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

An Option configures the optional behavior of an SQL projection.

func WithCandidateDrivers

func WithCandidateDrivers(drivers ...Driver) Option

WithCandidateDrivers returns an Option that adds candidate drivers for selection as the driver to use.

func WithDriver

func WithDriver(d Driver) Option

WithDriver returns an Option that forces use of a specific Driver.

It takes precedence over any WithCandidateDriver() option.

type ResourceRepository added in v0.6.4

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

ResourceRepository is an implementation of resource.Repository that stores resources versions in an SQL database.

func NewResourceRepository added in v0.6.4

func NewResourceRepository(
	db *sql.DB,
	key string,
	options ...Option,
) *ResourceRepository

NewResourceRepository returns a new ResourceRepository that uses db to store resource versions.

func (*ResourceRepository) DeleteResource added in v0.6.4

func (rr *ResourceRepository) DeleteResource(ctx context.Context, r []byte) error

DeleteResource removes all information about the resource r.

func (*ResourceRepository) ResourceVersion added in v0.6.4

func (rr *ResourceRepository) ResourceVersion(ctx context.Context, r []byte) ([]byte, error)

ResourceVersion returns the version of the resource r.

func (*ResourceRepository) StoreResourceVersion added in v0.6.4

func (rr *ResourceRepository) StoreResourceVersion(ctx context.Context, r, v []byte) error

StoreResourceVersion sets the version of the resource r to v without checking the current version.

func (*ResourceRepository) UpdateResourceVersion added in v0.6.4

func (rr *ResourceRepository) UpdateResourceVersion(
	ctx context.Context,
	r, c, n []byte,
) (ok bool, err error)

UpdateResourceVersion updates the version of the resource r to n.

If c is not the current version of r, it returns false and no update occurs.

func (*ResourceRepository) UpdateResourceVersionFn added in v0.6.4

func (rr *ResourceRepository) UpdateResourceVersionFn(
	ctx context.Context,
	r, c, n []byte,
	fn func(context.Context, *sql.Tx) error,
) (ok bool, err error)

UpdateResourceVersionFn updates the version of the resource r to n and performs a user-defined operation within the same transaction.

If c is not the current version of r, it returns false and no update occurs.

Directories

Path Synopsis
Package fixtures is a set of test fixtures and mocks for SQL projections.
Package fixtures is a set of test fixtures and mocks for SQL projections.

Jump to

Keyboard shortcuts

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