stasher

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package stasher is a helper for inserting records into an outbox table within transaction scope.

Example
db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password= dbname=postgres sslmode=disable")
if err != nil {
	panic(err)
}
defer db.Close()

st := New("outbox")

// Begin a transaction.
tx, _ := db.Begin()
defer tx.Rollback()

// Update other database entities in transaction scope.
// ...

// Stash an outbox record for subsequent harvesting.
err = st.Stash(tx, goharvest.OutboxRecord{
	KafkaTopic: "my-app.topic",
	KafkaKey:   "hello",
	KafkaValue: goharvest.String("world"),
	KafkaHeaders: goharvest.KafkaHeaders{
		{Key: "applicationId", Value: "my-app"},
	},
})
if err != nil {
	panic(err)
}

// Commit the transaction.
tx.Commit()
Output:

Example (Prepare)
db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password= dbname=postgres sslmode=disable")
if err != nil {
	panic(err)
}
defer db.Close()

st := New("outbox")

// Begin a transaction.
tx, _ := db.Begin()
defer tx.Rollback()

// Update other database entities in transaction scope.
// ...

// Formulates a prepared statement that may be reused within the scope of the transaction.
prestash, _ := st.Prepare(tx)

// Publish a bunch of messages using the same prepared statement.
for i := 0; i < 10; i++ {
	// Stash an outbox record for subsequent harvesting.
	err = prestash.Stash(goharvest.OutboxRecord{
		KafkaTopic: "my-app.topic",
		KafkaKey:   "hello",
		KafkaValue: goharvest.String("world"),
		KafkaHeaders: goharvest.KafkaHeaders{
			{Key: "applicationId", Value: "my-app"},
		},
	})
	if err != nil {
		panic(err)
	}
}

// Commit the transaction.
tx.Commit()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PreStash

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

PreStash houses a prepared statement bound to the scope of a single transaction.

func (PreStash) Stash

func (p PreStash) Stash(rec goharvest.OutboxRecord) error

Stash one record using the prepared statement.

type Stasher

type Stasher interface {
	Stash(tx *sql.Tx, rec goharvest.OutboxRecord) error
	Prepare(tx *sql.Tx) (PreStash, error)
}

Stasher writes records into the outbox table.

func New

func New(outboxTable string) Stasher

New creates a new Stasher instance for the given outboxTable.

Jump to

Keyboard shortcuts

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