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 ¶
func New( db *sql.DB, d Driver, h MessageHandler, ) dogma.ProjectionMessageHandler
New returns a new dogma.ProjectionMessageHandler that binds an SQL-specific MessageHandler to an SQL database.
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 []byte,
) (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 []byte,
c, n uint64,
) (bool, error)
// DeleteCheckpointOffsets deletes all checkpoint offsets for a specific
// handler.
DeleteCheckpointOffsets(
ctx context.Context,
tx *sql.Tx,
h []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.
type MessageHandler ¶
type MessageHandler interface {
// Configure declares the handler's configuration by calling methods on c.
//
// The configuration includes the handler's identity and message routes.
//
// The engine calls this method at least once during startup. It must
// produce the same configuration each time it's called.
Configure(c dogma.ProjectionConfigurer)
// HandleEvent updates the projection to reflect the occurrence of a
// [dogma.Event].
//
// Changes to the projection's data must be performed within the supplied
// transaction.
HandleEvent(ctx context.Context, tx *sql.Tx, s dogma.ProjectionEventScope, m dogma.Event) error
// Compact reduces the projection's size by removing or consolidating data.
//
// The handler might delete obsolete entries or merge fine-grained data into
// summaries. The specific strategy depends on the projection's purpose and
// access patterns.
//
// The implementation should perform compaction incrementally to make some
// progress even if ctx reaches its deadline.
//
// The engine may call this method at any time, including in parallel with
// handling an event.
//
// Not all projections need compaction. Embed [NoCompactBehavior] in the
// handler to indicate compaction not required.
Compact(ctx context.Context, db *sql.DB, s dogma.ProjectionCompactScope) error
// Reset clears all projection data.
//
// Changes to the projection's data must be performed within the supplied
// transaction.
//
// Not all projections can be reset. Embed [NoResetBehavior] in the handler
// to indicate that reset is not supported.
Reset(ctx context.Context, tx *sql.Tx, s dogma.ProjectionResetScope) 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 ¶
func (NoCompactBehavior) Compact( context.Context, *sql.DB, dogma.ProjectionCompactScope, ) error
Compact returns nil.
type NoResetBehavior ¶ added in v0.9.0
type NoResetBehavior struct{}
NoResetBehavior is an embeddable type for MessageHandler implementations that don't support resetting their state.
Embed this type in a MessageHandler when resetting projection data isn't feasible or required.
func (NoResetBehavior) Reset ¶ added in v0.9.0
func (NoResetBehavior) Reset(context.Context, *sql.Tx, dogma.ProjectionResetScope) error
Reset returns an error indicating that reset is not supported.