txutil

package
v0.0.0-...-0b2e453 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BulkSyncTxDelete

type BulkSyncTxDelete[ID any, Topic comparable] struct {
	// contains filtered or unexported fields
}

BulkSyncTxDelete wraps a SQL transaction and tracks delete events to publish grouped by topic after successful commit.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewBulkDeleteTx[IDType, TopicType](tx, extractDeleteTopicFn)
defer devtoolsdb.TxnRollback(tx)

for _, id := range ids {
    service.Delete(ctx, id)
    syncTx.Track(id, workspaceID, isDelta)
}

err := syncTx.CommitAndPublish(ctx, publishBulkDeleteFn)

func NewBulkDeleteTx

func NewBulkDeleteTx[ID any, Topic comparable](
	tx *sql.Tx,
	topicExtractor func(DeleteEvent[ID]) Topic,
) *BulkSyncTxDelete[ID, Topic]

NewBulkDeleteTx creates a new bulk transaction wrapper for delete operations. The topicExtractor function is used to extract the topic from each DeleteEvent for grouping.

func (*BulkSyncTxDelete[ID, Topic]) CommitAndPublish

func (s *BulkSyncTxDelete[ID, Topic]) CommitAndPublish(
	ctx context.Context,
	publishFn func(Topic, []DeleteEvent[ID]),
) error

CommitAndPublish commits the transaction and publishes all tracked delete events grouped by topic. If commit fails, no events are published. The publishFn receives a topic and slice of DeleteEvents for that topic.

Events are automatically grouped by topic using the topicExtractor function, and publishFn is called once per unique topic.

func (*BulkSyncTxDelete[ID, Topic]) Track

func (s *BulkSyncTxDelete[ID, Topic]) Track(id ID, workspaceID ID, isDelta bool)

Track adds a delete event to be published after successful commit.

type BulkSyncTxInsert

type BulkSyncTxInsert[T any, Topic comparable] struct {
	// contains filtered or unexported fields
}

BulkSyncTxInsert wraps a SQL transaction and tracks items to publish bulk sync events grouped by topic after successful commit. This eliminates the need for manual loops and ensures events are never forgotten.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewBulkInsertTx[ItemType, TopicType](tx, extractTopicFn)
defer devtoolsdb.TxnRollback(tx)

for _, item := range items {
    service.Create(ctx, item)
    syncTx.Track(item)
}

err := syncTx.CommitAndPublish(ctx, publishBulkInsertFn)

func NewBulkInsertTx

func NewBulkInsertTx[T any, Topic comparable](
	tx *sql.Tx,
	topicExtractor TopicExtractor[T, Topic],
) *BulkSyncTxInsert[T, Topic]

NewBulkInsertTx creates a new bulk transaction wrapper for insert operations. The topicExtractor function is used to extract the topic from each item for grouping.

func (*BulkSyncTxInsert[T, Topic]) CommitAndPublish

func (s *BulkSyncTxInsert[T, Topic]) CommitAndPublish(
	ctx context.Context,
	publishFn func(Topic, []T),
) error

CommitAndPublish commits the transaction and publishes all tracked items grouped by topic in bulk. If commit fails, no events are published. The publishFn receives a topic and slice of items for that topic.

Items are automatically grouped by topic using the topicExtractor function, and publishFn is called once per unique topic.

func (*BulkSyncTxInsert[T, Topic]) Track

func (s *BulkSyncTxInsert[T, Topic]) Track(item T)

Track adds an item to be published after successful commit.

type BulkSyncTxUpdate

type BulkSyncTxUpdate[T any, P any, Topic comparable] struct {
	// contains filtered or unexported fields
}

BulkSyncTxUpdate wraps a SQL transaction and tracks update events to publish grouped by topic after successful commit.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewBulkUpdateTx[ItemType, PatchType, TopicType](tx, extractTopicFn)
defer devtoolsdb.TxnRollback(tx)

for _, update := range updates {
    service.Update(ctx, update.Item)
    syncTx.Track(update.Item, update.Patch)
}

err := syncTx.CommitAndPublish(ctx, publishBulkUpdateFn)

func NewBulkUpdateTx

func NewBulkUpdateTx[T any, P any, Topic comparable](
	tx *sql.Tx,
	topicExtractor TopicExtractor[T, Topic],
) *BulkSyncTxUpdate[T, P, Topic]

NewBulkUpdateTx creates a new bulk transaction wrapper for update operations. The topicExtractor function is used to extract the topic from each item for grouping.

func (*BulkSyncTxUpdate[T, P, Topic]) CommitAndPublish

func (s *BulkSyncTxUpdate[T, P, Topic]) CommitAndPublish(
	ctx context.Context,
	publishFn func(Topic, []UpdateEvent[T, P]),
) error

CommitAndPublish commits the transaction and publishes all tracked update events grouped by topic. If commit fails, no events are published. The publishFn receives a topic and slice of UpdateEvents for that topic.

Items are automatically grouped by topic using the topicExtractor function, and publishFn is called once per unique topic.

func (*BulkSyncTxUpdate[T, P, Topic]) Track

func (s *BulkSyncTxUpdate[T, P, Topic]) Track(item T, patch P)

Track adds an update event (item + patch) to be published after successful commit.

type DeleteEvent

type DeleteEvent[ID any] struct {
	ID          ID
	WorkspaceID ID
	IsDelta     bool
}

DeleteEvent represents a delete operation with the ID and workspace context

type SyncTxDelete

type SyncTxDelete[ID any] struct {
	// contains filtered or unexported fields
}

SyncTxDelete wraps a SQL transaction and tracks delete events to publish after successful commit.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewDeleteTx[idwrap.IDWrap](tx)
defer devtoolsdb.TxnRollback(tx)

for _, id := range ids {
    service.Delete(ctx, id)
    syncTx.Track(id, workspaceID, isDelta)
}

err := syncTx.CommitAndPublish(ctx, publishDeleteEvent)

func NewDeleteTx

func NewDeleteTx[ID any](tx *sql.Tx) *SyncTxDelete[ID]

NewDeleteTx creates a new transaction wrapper for delete operations

func (*SyncTxDelete[ID]) CommitAndPublish

func (s *SyncTxDelete[ID]) CommitAndPublish(
	ctx context.Context,
	publishFn func(ID, ID, bool),
) error

CommitAndPublish commits the transaction and publishes all tracked delete events. If commit fails, no events are published. The publishFn receives the ID, workspaceID, and isDelta flag.

func (*SyncTxDelete[ID]) Track

func (s *SyncTxDelete[ID]) Track(id ID, workspaceID ID, isDelta bool)

Track adds a delete event to be published after successful commit

type SyncTxInsert

type SyncTxInsert[T any] struct {
	// contains filtered or unexported fields
}

SyncTxInsert wraps a SQL transaction and tracks items to publish sync events after successful commit. This ensures sync events are never forgotten.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewInsertTx[mhttp.HTTP](tx)
defer devtoolsdb.TxnRollback(tx)

for _, item := range items {
    service.Create(ctx, item)
    syncTx.Track(item)
}

err := syncTx.CommitAndPublish(ctx, publishInsertEvent)

func NewInsertTx

func NewInsertTx[T any](tx *sql.Tx) *SyncTxInsert[T]

NewInsertTx creates a new transaction wrapper for insert operations

func (*SyncTxInsert[T]) CommitAndPublish

func (s *SyncTxInsert[T]) CommitAndPublish(
	ctx context.Context,
	publishFn func(T),
) error

CommitAndPublish commits the transaction and publishes all tracked items. If commit fails, no events are published.

func (*SyncTxInsert[T]) Track

func (s *SyncTxInsert[T]) Track(item T)

Track adds an item to be published after successful commit

type SyncTxUpdate

type SyncTxUpdate[T any, P any] struct {
	// contains filtered or unexported fields
}

SyncTxUpdate wraps a SQL transaction and tracks update events to publish after successful commit.

Usage:

tx, _ := db.BeginTx(ctx, nil)
syncTx := txutil.NewUpdateTx[mhttp.HTTP, patch.HTTPDeltaPatch](tx)
defer devtoolsdb.TxnRollback(tx)

for _, update := range updates {
    service.Update(ctx, update.Item)
    syncTx.Track(update.Item, update.Patch)
}

err := syncTx.CommitAndPublish(ctx, publishUpdateEvent)

func NewUpdateTx

func NewUpdateTx[T any, P any](tx *sql.Tx) *SyncTxUpdate[T, P]

NewUpdateTx creates a new transaction wrapper for update operations

func (*SyncTxUpdate[T, P]) CommitAndPublish

func (s *SyncTxUpdate[T, P]) CommitAndPublish(
	ctx context.Context,
	publishFn func(T, P),
) error

CommitAndPublish commits the transaction and publishes all tracked update events. If commit fails, no events are published. The publishFn receives both the item and its patch.

func (*SyncTxUpdate[T, P]) Track

func (s *SyncTxUpdate[T, P]) Track(item T, patch P)

Track adds an update event (item + patch) to be published after successful commit

type TopicExtractor

type TopicExtractor[T any, Topic any] func(item T) Topic

TopicExtractor is a function that extracts a topic from an item. Used for auto-grouping items by topic before bulk publishing.

type UpdateEvent

type UpdateEvent[T any, P any] struct {
	Item  T
	Patch P
}

UpdateEvent represents an update operation with the updated item and its delta patch

Jump to

Keyboard shortcuts

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