outboxsql

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package outboxsql is the outbox for consumer apps running against database/sql, for both Postgres (any driver: lib/pq, pgx/stdlib, etc.) and MySQL (go-sql-driver/mysql). InitSchema (and the CreateOutboxTableSQLPostgres / CreateOutboxTableSQLMySQL constants it dispatches on) create the outbox_messages table an outbox processor then drains; Writer writes event, audit-log and dispatch-job rows into it inside the caller's transaction, the same rows outboxpgx writes.

Index

Constants

View Source
const CreateOutboxTableSQLMySQL = `` /* 783-byte string literal not displayed */

CreateOutboxTableSQLMySQL is the schema for MySQL 8.0+. Includes SDK-specific columns and indexes matching the Java outbox-processor's expectations. Mirrors the TypeScript SDK migration.

View Source
const CreateOutboxTableSQLPostgres = `` /* 814-byte string literal not displayed */

CreateOutboxTableSQLPostgres is the schema for Postgres. Mirrors the TypeScript SDK's migration so all SDKs converge on one shape.

Variables

This section is empty.

Functions

func InitSchema

func InitSchema(ctx context.Context, db *sql.DB, dialect Dialect) error

InitSchema creates the outbox_messages table for the given dialect. Idempotent. In production, run your real migration system instead.

Postgres: a single Exec covers all statements (the driver handles the script). MySQL: only one statement per Exec; this helper splits on bare semicolons at top level which is sufficient for the schema above (single CREATE TABLE statement, indexes inline). For richer schemas, use a real migration tool.

Types

type Dialect

type Dialect int

Dialect picks the placeholder style and dialect-specific SQL.

const (
	// DialectPostgres uses $1, $2 placeholders and Postgres-flavoured DDL.
	DialectPostgres Dialect = iota
	// DialectMySQL uses ? placeholders and MySQL-flavoured DDL.
	DialectMySQL
)

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Execer is what a Writer writes through: *sql.Tx (the usual case: the caller's transaction, so the rows commit with the work) or *sql.DB.

type Writer

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

Writer writes outbox rows through database/sql. Safe for concurrent use.

func NewWriter

func NewWriter(dialect Dialect, cfg WriterConfig) (*Writer, error)

NewWriter builds a Writer for the dialect's placeholder style. It fails only on an invalid table name.

func (*Writer) WriteAudit

func (w *Writer) WriteAudit(ctx context.Context, ex Execer, event usecase.DomainEvent, command any) error

WriteAudit inserts the AUDIT_LOG row of (event, command).

func (*Writer) WriteDispatchJob

func (w *Writer) WriteDispatchJob(ctx context.Context, ex Execer, job outbox.DispatchJob) error

WriteDispatchJob validates job and inserts its DISPATCH_JOB row.

func (*Writer) WriteEvent

func (w *Writer) WriteEvent(ctx context.Context, ex Execer, event usecase.DomainEvent) error

WriteEvent inserts the EVENT row of a domain event.

type WriterConfig

type WriterConfig struct {
	// TableName is the outbox table. Default: "outbox_messages".
	TableName string
	// ClientID is stored in every row's client_id column. May be empty.
	ClientID string
	// ClientCode is the client's identifier slug, sent as clientCode in
	// event and audit payloads. Empty leaves it out.
	ClientCode string
	// ApplicationCode is written as each audit row's applicationCode.
	ApplicationCode string
	// Now is the clock for created_at/updated_at. Default time.Now.
	Now func() time.Time
}

WriterConfig configures a Writer. The fields mean what they mean on outboxpgx.Config.

Jump to

Keyboard shortcuts

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