sqlprojection

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 4 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 New

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 {
	// 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

	// QueryCheckpointOffset returns the stored checkpoint offset for a specific
	// handler and event stream.
	QueryCheckpointOffset(
		ctx context.Context,
		db *sql.DB,
		h, s *uuidpb.UUID,
	) (uint64, error)

	// UpdateCheckpointOffset updates the checkpoint offset for a specific
	// handler and event stream from c to n.
	//
	// It returns false if c is not the current checkpoint offset.
	UpdateCheckpointOffset(
		ctx context.Context,
		tx *sql.Tx,
		h, s *uuidpb.UUID,
		c, n uint64,
	) (bool, 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.

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 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.Event) error

	// 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.

Directories

Path Synopsis
internal
fixtures
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