Documentation
¶
Overview ¶
Package postgres provides a PostgreSQL implementation of store.Store.
Index ¶
- Constants
- type Option
- type Store
- func (s *Store) AddTag(ctx context.Context, id string, tagID string) error
- func (s *Store) AddTagByFilter(ctx context.Context, ownerID string, filters []store.Filter, tagID string) (int64, error)
- func (s *Store) Close(ctx context.Context) error
- func (s *Store) Connect(ctx context.Context) error
- func (s *Store) Count(ctx context.Context, filters []store.Filter) (int64, error)
- func (s *Store) CountByFolders(ctx context.Context, ownerID string, folderIDs []string) (map[string]store.FolderCounts, error)
- func (s *Store) CreateMessage(ctx context.Context, data store.MessageData) (store.Message, error)
- func (s *Store) CreateMessageIdempotent(ctx context.Context, data store.MessageData, idempotencyKey string) (store.Message, bool, error)
- func (s *Store) CreateMessages(ctx context.Context, data []store.MessageData) ([]store.Message, error)
- func (s *Store) CreateMessagesIdempotent(ctx context.Context, entries []store.IdempotentCreateEntry) ([]store.IdempotentCreateResult, error)
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) DeleteByFilter(ctx context.Context, ownerID string, filters []store.Filter) (int64, error)
- func (s *Store) DeleteDraft(ctx context.Context, id string) error
- func (s *Store) DeleteExpiredMessages(ctx context.Context, cutoff time.Time) (int64, error)
- func (s *Store) DeleteExpiredTrash(ctx context.Context, cutoff time.Time) (int64, error)
- func (s *Store) DeleteMessagesByIDs(ctx context.Context, ids []string) ([]string, error)
- func (s *Store) DeleteTTLExpiredMessages(ctx context.Context, now time.Time) (int64, error)
- func (s *Store) EventOutboxStore() event.OutboxStore
- func (s *Store) Find(ctx context.Context, filters []store.Filter, opts store.ListOptions) (*store.MessageList, error)
- func (s *Store) FindWithCount(ctx context.Context, filters []store.Filter, opts store.ListOptions) (*store.MessageList, int64, error)
- func (s *Store) Get(ctx context.Context, id string) (store.Message, error)
- func (s *Store) GetDraft(ctx context.Context, id string) (store.DraftMessage, error)
- func (s *Store) HardDelete(ctx context.Context, id string) error
- func (s *Store) ListDistinctFolders(ctx context.Context, ownerID string) ([]string, error)
- func (s *Store) ListDrafts(ctx context.Context, ownerID string, opts store.ListOptions) (*store.DraftList, error)
- func (s *Store) MailboxStats(ctx context.Context, ownerID string) (*store.MailboxStats, error)
- func (s *Store) MarkAllRead(ctx context.Context, ownerID, folderID string) (int64, error)
- func (s *Store) MarkRead(ctx context.Context, id string, read bool) error
- func (s *Store) MarkReadByFilter(ctx context.Context, ownerID string, filters []store.Filter, read bool) (int64, error)
- func (s *Store) MoveByFilter(ctx context.Context, ownerID string, filters []store.Filter, folderID string) (int64, error)
- func (s *Store) MoveToFolder(ctx context.Context, id string, folderID string, opts ...store.MoveOption) error
- func (s *Store) NewDraft(ownerID string) store.DraftMessage
- func (s *Store) OutboxEnabled() bool
- func (s *Store) RemoveTag(ctx context.Context, id string, tagID string) error
- func (s *Store) RemoveTagByFilter(ctx context.Context, ownerID string, filters []store.Filter, tagID string) (int64, error)
- func (s *Store) Restore(ctx context.Context, id string) error
- func (s *Store) SaveDraft(ctx context.Context, draft store.DraftMessage) (store.DraftMessage, error)
- func (s *Store) Search(ctx context.Context, query store.SearchQuery) (*store.MessageList, error)
- func (s *Store) ThreadParticipants(ctx context.Context, threadID string) ([]string, error)
- func (s *Store) WithOutboxCtx(ctx context.Context, fn func(ctx context.Context) error) error
Constants ¶
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
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 WithOutbox ¶ added in v0.6.5
WithOutbox enables transactional outbox.
func WithOutboxTable ¶ added in v0.6.5
WithOutboxTable sets the outbox table name. Default is "outbox".
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store implements store.Store using PostgreSQL.
func New ¶
New creates a new PostgreSQL store with the provided database connection. Call Connect() to initialize the schema and indexes.
func NewFromDB ¶
NewFromDB creates a new PostgreSQL store from a standard sql.DB connection. This wraps the sql.DB with sqlx for enhanced functionality.
func (*Store) AddTagByFilter ¶ added in v0.6.5
func (*Store) Close ¶
Close marks the store as disconnected. The caller is responsible for closing the database connection.
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 (*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 (*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) DeleteByFilter ¶ added in v0.6.5
func (*Store) DeleteExpiredMessages ¶ added in v0.6.3
DeleteExpiredMessages atomically deletes all non-draft messages older than cutoff.
func (*Store) DeleteExpiredTrash ¶
DeleteExpiredTrash atomically deletes all messages in trash older than cutoff.
func (*Store) DeleteMessagesByIDs ¶ added in v0.6.3
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
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) ListDistinctFolders ¶ added in v0.3.0
ListDistinctFolders returns all distinct folder IDs for a user's non-deleted messages.
func (*Store) ListDrafts ¶
func (*Store) MailboxStats ¶ added in v0.3.0
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
MarkAllRead marks all unread non-draft messages in a folder as read. Uses a single UPDATE for efficiency.
func (*Store) MarkReadByFilter ¶ added in v0.6.5
func (*Store) MoveByFilter ¶ added in v0.6.5
func (*Store) MoveToFolder ¶
func (*Store) OutboxEnabled ¶ added in v0.6.5
OutboxEnabled returns whether the outbox is configured.
func (*Store) RemoveTagByFilter ¶ added in v0.6.5
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
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
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.