postgres

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package postgres provides a PostgreSQL implementation of store.Store.

Index

Constants

View Source
const (
	DefaultTable   = "messages"
	DefaultTimeout = 10 * time.Second
)

Default configuration values.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option configures a PostgreSQL store.

func WithFTSEnabled added in v0.7.6

func WithFTSEnabled(enabled bool) Option

WithFTSEnabled enables PostgreSQL full-text search using tsvector/GIN indexes. When true, Search() uses plainto_tsquery and ts_rank instead of ILIKE. A search_vector column and trigger are created automatically during Connect. Default is false (uses ILIKE for backward compatibility).

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets a custom logger.

func WithOutbox added in v0.6.5

func WithOutbox(enabled bool) Option

WithOutbox enables transactional outbox.

func WithOutboxTable added in v0.6.5

func WithOutboxTable(name string) Option

WithOutboxTable sets the outbox table name. Default is "outbox".

func WithTable

func WithTable(name string) Option

WithTable sets the table name. The name must be a valid SQL identifier (letters, digits, underscores only) since it is interpolated into queries as an identifier.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the operation timeout.

type Store

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

Store implements store.Store using PostgreSQL.

func New

func New(db *sqlx.DB, opts ...Option) *Store

New creates a new PostgreSQL store with the provided database connection. Call Connect() to initialize the schema and indexes.

func NewFromDB

func NewFromDB(db *sql.DB, opts ...Option) *Store

NewFromDB creates a new PostgreSQL store from a standard sql.DB connection. This wraps the sql.DB with sqlx for enhanced functionality.

func (*Store) AddTag

func (s *Store) AddTag(ctx context.Context, id string, tagID string) error

func (*Store) AddTagByFilter added in v0.6.5

func (s *Store) AddTagByFilter(ctx context.Context, ownerID string, filters []store.Filter, tagID string) (int64, error)

func (*Store) Close

func (s *Store) Close(ctx context.Context) error

Close marks the store as disconnected. The caller is responsible for closing the database connection.

func (*Store) Connect

func (s *Store) Connect(ctx context.Context) error

Connect initializes the schema and indexes.

func (*Store) Count

func (s *Store) Count(ctx context.Context, filters []store.Filter) (int64, error)

func (*Store) CountByFolders added in v0.3.0

func (s *Store) CountByFolders(ctx context.Context, ownerID string, folderIDs []string) (map[string]store.FolderCounts, error)

CountByFolders returns message counts and unread counts for the given folders. Uses a single GROUP BY query with array filter for efficient batch counting.

func (*Store) CreateMessage

func (s *Store) CreateMessage(ctx context.Context, data store.MessageData) (store.Message, error)

func (*Store) CreateMessageIdempotent

func (s *Store) CreateMessageIdempotent(ctx context.Context, data store.MessageData, idempotencyKey string) (store.Message, bool, error)

CreateMessageIdempotent atomically creates a message or returns existing.

func (*Store) CreateMessages

func (s *Store) CreateMessages(ctx context.Context, data []store.MessageData) ([]store.Message, error)

func (*Store) CreateMessagesIdempotent added in v0.6.5

func (s *Store) CreateMessagesIdempotent(ctx context.Context, entries []store.IdempotentCreateEntry) ([]store.IdempotentCreateResult, error)

CreateMessagesIdempotent creates multiple messages with idempotency keys.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

func (*Store) DeleteByFilter added in v0.6.5

func (s *Store) DeleteByFilter(ctx context.Context, ownerID string, filters []store.Filter) (int64, error)

func (*Store) DeleteDraft

func (s *Store) DeleteDraft(ctx context.Context, id string) error

func (*Store) DeleteExpiredMessages added in v0.6.3

func (s *Store) DeleteExpiredMessages(ctx context.Context, cutoff time.Time) (int64, error)

DeleteExpiredMessages atomically deletes all non-draft messages older than cutoff.

func (*Store) DeleteExpiredTrash

func (s *Store) DeleteExpiredTrash(ctx context.Context, cutoff time.Time) (int64, error)

DeleteExpiredTrash atomically deletes all messages in trash older than cutoff.

func (*Store) DeleteMessagesByIDs added in v0.6.3

func (s *Store) DeleteMessagesByIDs(ctx context.Context, ids []string) ([]string, error)

DeleteMessagesByIDs deletes the specified messages and returns the IDs that were actually deleted by this call. Uses DELETE ... RETURNING id for atomic winner determination in multi-instance environments.

func (*Store) DeleteTTLExpiredMessages added in v0.6.5

func (s *Store) DeleteTTLExpiredMessages(ctx context.Context, now time.Time) (int64, error)

DeleteTTLExpiredMessages atomically deletes all non-draft messages whose expires_at is non-null and before the given time.

func (*Store) EventOutboxStore added in v0.6.7

func (s *Store) EventOutboxStore() event.OutboxStore

EventOutboxStore returns an event.OutboxStore backed by the same PostgreSQL database, for configuring the event bus with event.WithOutbox(). Returns nil if outbox is not enabled.

func (*Store) Find

func (s *Store) Find(ctx context.Context, filters []store.Filter, opts store.ListOptions) (*store.MessageList, error)

func (*Store) FindWithCount added in v0.3.0

func (s *Store) FindWithCount(ctx context.Context, filters []store.Filter, opts store.ListOptions) (*store.MessageList, int64, error)

FindWithCount retrieves messages and total count in a single operation. Delegates to Find + Count for simplicity.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (store.Message, error)

func (*Store) GetDraft

func (s *Store) GetDraft(ctx context.Context, id string) (store.DraftMessage, error)

func (*Store) HardDelete

func (s *Store) HardDelete(ctx context.Context, id string) error

func (*Store) ListDistinctFolders added in v0.3.0

func (s *Store) ListDistinctFolders(ctx context.Context, ownerID string) ([]string, error)

ListDistinctFolders returns all distinct folder IDs for a user's non-deleted messages.

func (*Store) ListDrafts

func (s *Store) ListDrafts(ctx context.Context, ownerID string, opts store.ListOptions) (*store.DraftList, error)

func (*Store) MailboxStats added in v0.3.0

func (s *Store) MailboxStats(ctx context.Context, ownerID string) (*store.MailboxStats, error)

MailboxStats returns aggregate statistics for a user's mailbox using a single query with a CTE for consistent point-in-time results.

func (*Store) MarkAllRead added in v0.4.0

func (s *Store) MarkAllRead(ctx context.Context, ownerID, folderID string) (int64, error)

MarkAllRead marks all unread non-draft messages in a folder as read. Uses a single UPDATE for efficiency.

func (*Store) MarkRead

func (s *Store) MarkRead(ctx context.Context, id string, read bool) error

func (*Store) MarkReadByFilter added in v0.6.5

func (s *Store) MarkReadByFilter(ctx context.Context, ownerID string, filters []store.Filter, read bool) (int64, error)

func (*Store) MoveByFilter added in v0.6.5

func (s *Store) MoveByFilter(ctx context.Context, ownerID string, filters []store.Filter, folderID string) (int64, error)

func (*Store) MoveToFolder

func (s *Store) MoveToFolder(ctx context.Context, id string, folderID string, opts ...store.MoveOption) error

func (*Store) NewDraft

func (s *Store) NewDraft(ownerID string) store.DraftMessage

func (*Store) OutboxEnabled added in v0.6.5

func (s *Store) OutboxEnabled() bool

OutboxEnabled returns whether the outbox is configured.

func (*Store) RemoveTag

func (s *Store) RemoveTag(ctx context.Context, id string, tagID string) error

func (*Store) RemoveTagByFilter added in v0.6.5

func (s *Store) RemoveTagByFilter(ctx context.Context, ownerID string, filters []store.Filter, tagID string) (int64, error)

func (*Store) Restore

func (s *Store) Restore(ctx context.Context, id string) error

func (*Store) SaveDraft

func (s *Store) SaveDraft(ctx context.Context, draft store.DraftMessage) (store.DraftMessage, error)

func (*Store) Search

func (s *Store) Search(ctx context.Context, query store.SearchQuery) (*store.MessageList, error)

func (*Store) ThreadParticipants added in v0.7.9

func (s *Store) ThreadParticipants(ctx context.Context, threadID string) ([]string, error)

ThreadParticipants returns distinct owner IDs of all non-deleted, non-draft messages with the given thread_id.

func (*Store) WithOutboxCtx added in v0.6.5

func (s *Store) WithOutboxCtx(ctx context.Context, fn func(ctx context.Context) error) error

WithOutboxCtx wraps fn in a PostgreSQL transaction that is shared by both store methods (via txCtxKey) and the event bus (via event.WithOutboxTx). This enables atomic mutation + event publish: store methods use the tx for DB writes, and Event.Publish() routes to the outbox table in the same tx. Zero overhead when outbox is disabled.

Jump to

Keyboard shortcuts

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