hooks

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package hooks provides lifecycle hook management for DynamORM model operations with cost tracking integration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuditHook

func AuditHook(ctx context.Context, model any) error

AuditHook creates an audit trail entry

func CacheInvalidationHook

func CacheInvalidationHook(ctx context.Context, model any) error

CacheInvalidationHook invalidates relevant caches

func Execute

func Execute(ctx context.Context, model any, hookType HookType) error

Execute executes hooks with the global registry

func NotificationHook

func NotificationHook(ctx context.Context, model any) error

NotificationHook creates notifications based on model changes

func Register

func Register(modelType reflect.Type, hookType HookType, fn HookFunc)

Register registers a hook with the global registry

func RegisterAsync

func RegisterAsync(modelType reflect.Type, hookType HookType, fn AsyncHookFunc)

RegisterAsync registers an async hook with the global registry

func RegisterConditional

func RegisterConditional(modelType reflect.Type, hookType HookType, condition func(ctx context.Context, model any) bool, fn HookFunc)

RegisterConditional registers a conditional hook with the global registry

func SearchIndexHook

func SearchIndexHook(ctx context.Context, model any) error

SearchIndexHook updates search indexes

func SetGlobalRegistry

func SetGlobalRegistry(registry *HookRegistry)

SetGlobalRegistry sets the global hook registry instance

func TimestampHook

func TimestampHook(_ context.Context, model any) error

TimestampHook updates timestamps on models

func ValidationHook

func ValidationHook(_ context.Context, model any) error

ValidationHook validates the model using its Validate method if available

Types

type AsyncHookFunc

type AsyncHookFunc func(ctx context.Context, model any)

AsyncHookFunc is a function that gets executed asynchronously

type ConditionalHookFunc

type ConditionalHookFunc struct {
	Condition func(ctx context.Context, model any) bool
	Hook      HookFunc
}

ConditionalHookFunc is a hook function with a condition

type FavoriteModel

type FavoriteModel interface {
	GetUserID() string
	GetStatusID() string
	GetStatusAuthorID() string
}

FavoriteModel interface for favorite-related models

type FollowModel

type FollowModel interface {
	GetFollowerID() string
	GetFolloweeID() string
}

FollowModel interface for follow-related models

type HookFunc

type HookFunc func(ctx context.Context, model any) error

HookFunc is a function that gets executed during model lifecycle events

type HookRegistry

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

HookRegistry manages all registered hooks

func GetGlobalRegistry

func GetGlobalRegistry() *HookRegistry

GetGlobalRegistry returns the global hook registry instance

func NewHookRegistry

func NewHookRegistry(logger *zap.Logger) *HookRegistry

NewHookRegistry creates a new hook registry

func (*HookRegistry) Clear

func (hr *HookRegistry) Clear(modelType reflect.Type)

Clear removes all hooks for a model type

func (*HookRegistry) ClearAll

func (hr *HookRegistry) ClearAll()

ClearAll removes all registered hooks

func (*HookRegistry) Execute

func (hr *HookRegistry) Execute(ctx context.Context, model any, hookType HookType) error

Execute executes all registered hooks for a model and hook type

func (*HookRegistry) GetRegisteredHooksCount

func (hr *HookRegistry) GetRegisteredHooksCount(modelType reflect.Type, hookType HookType) int

GetRegisteredHooksCount returns the number of registered hooks for a model type

func (*HookRegistry) Register

func (hr *HookRegistry) Register(modelType reflect.Type, hookType HookType, fn HookFunc)

Register registers a synchronous hook for a model type

func (*HookRegistry) RegisterAsync

func (hr *HookRegistry) RegisterAsync(modelType reflect.Type, hookType HookType, fn AsyncHookFunc)

RegisterAsync registers an asynchronous hook for a model type

func (*HookRegistry) RegisterConditional

func (hr *HookRegistry) RegisterConditional(modelType reflect.Type, hookType HookType, condition func(ctx context.Context, model any) bool, fn HookFunc)

RegisterConditional registers a conditional hook for a model type

type HookStats

type HookStats struct {
	TotalExecutions int64
	TotalDuration   time.Duration
	ErrorCount      int64
	AverageDuration time.Duration
	LastExecution   time.Time
}

HookStats tracks execution statistics for hooks

type HookStatsTracker

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

HookStatsTracker tracks statistics for hook executions

func GetGlobalStatsTracker

func GetGlobalStatsTracker() *HookStatsTracker

GetGlobalStatsTracker returns the global hook statistics tracker

func NewHookStatsTracker

func NewHookStatsTracker() *HookStatsTracker

NewHookStatsTracker creates a new hook statistics tracker

func (*HookStatsTracker) GetStats

func (hst *HookStatsTracker) GetStats() map[string]*HookStats

GetStats returns statistics for all hooks

func (*HookStatsTracker) Reset

func (hst *HookStatsTracker) Reset()

Reset clears all statistics

func (*HookStatsTracker) TrackExecution

func (hst *HookStatsTracker) TrackExecution(modelType reflect.Type, hookType HookType, duration time.Duration, err error)

TrackExecution tracks the execution of a hook

type HookType

type HookType string

HookType represents the type of lifecycle hook

const (
	// BeforeCreate represents a before create hook
	BeforeCreate HookType = "before_create"
	// AfterCreate represents an after create hook
	AfterCreate HookType = "after_create"
	// BeforeUpdate represents a before update hook
	BeforeUpdate HookType = "before_update"
	// AfterUpdate represents an after update hook
	AfterUpdate HookType = "after_update"
	// BeforeDelete represents a before delete hook
	BeforeDelete HookType = "before_delete"
	// AfterDelete represents an after delete hook
	AfterDelete HookType = "after_delete"
	// AfterFind represents an after find hook
	AfterFind HookType = "after_find"
	// BeforeSave represents a before save hook
	BeforeSave HookType = "before_save"
	// AfterSave represents an after save hook
	AfterSave HookType = "after_save"
)

type MentionModel

type MentionModel interface {
	GetUserID() string
	GetMentionedUserID() string
	GetStatusID() string
}

MentionModel interface for mention-related models

type NotificationData

type NotificationData struct {
	Type        string
	UserID      string
	FromUser    string
	StatusID    string
	ErrorFields []zap.Field
}

NotificationData holds parameters for creating notifications

type NotificationRepository

type NotificationRepository interface {
	CreateNotification(ctx context.Context, notification any) error
	GetUserPushSubscriptions(ctx context.Context, username string) ([]any, error)
	SendPushNotification(ctx context.Context, username string, notification any) error
}

NotificationRepository interface for notification operations

type PollModel

type PollModel interface {
	GetPollID() string
	GetAuthorID() string
	GetVoterID() string
	HasEnded() bool
}

PollModel interface for poll-related models

type ReblogModel

type ReblogModel interface {
	GetUserID() string
	GetStatusID() string
	GetOriginalAuthorID() string
}

ReblogModel interface for reblog-related models

type StatusModel

type StatusModel interface {
	GetUserID() string
	GetContent() string
	GetMentions() []string
}

StatusModel interface for status-related models

type Validator

type Validator interface {
	Validate() error
}

Validator interface for models that can be validated

Jump to

Keyboard shortcuts

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