Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package postgres is the PostgreSQL driven adapter: connection pool, migrations and the stores that implement the core's resource ports.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(pool *pgxpool.Pool) error

Migrate applies every pending migration using pool.

func NewPool

func NewPool(ctx context.Context, cfg config.DB) (*pgxpool.Pool, error)

NewPool opens a pgx connection pool and checks that the database answers. No error holds the connection string as a whole. The reason for a string the driver cannot read may name the piece it stumbled on (see parseReason), and the driver's error for a connection that fails names the user, the host and the database as the driver read them. They are no pieces of the password when the driver ends the user and the password where net/url does: config.Load takes no other URL, and a DB that was put together by hand may hold one.

Types

type ConversationStore

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

ConversationStore is the PostgreSQL implementation of resource.ConversationStore.

func NewConversationStore

func NewConversationStore(pool *pgxpool.Pool, sql Statements) *ConversationStore

NewConversationStore returns a store backed by pool, with its SQL from sql.

func (*ConversationStore) Create

Create implements resource.ConversationStore.

func (*ConversationStore) Delete

func (s *ConversationStore) Delete(ctx context.Context, userID, id uuid.UUID) error

Delete implements resource.ConversationStore.

func (*ConversationStore) Get

func (s *ConversationStore) Get(ctx context.Context, userID, id uuid.UUID) (domain.Conversation, error)

Get implements resource.ConversationStore.

Pattern: Ownership in the Query

func (*ConversationStore) List

func (s *ConversationStore) List(ctx context.Context, userID uuid.UUID, limit int) ([]domain.Conversation, error)

List implements resource.ConversationStore.

func (*ConversationStore) Rename

func (s *ConversationStore) Rename(ctx context.Context, userID, id uuid.UUID, title string) error

Rename implements resource.ConversationStore.

type MessageStore

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

MessageStore is the PostgreSQL implementation of resource.MessageStore.

func NewMessageStore

func NewMessageStore(pool *pgxpool.Pool, sql Statements) *MessageStore

NewMessageStore returns a store backed by pool, with its SQL from sql.

func (*MessageStore) AppendPair

func (s *MessageStore) AppendPair(ctx context.Context, userID, convID uuid.UUID, content string) (user, reply domain.Message, err error)

AppendPair implements resource.MessageStore.

Pattern: Row-Locked Sequence

func (*MessageStore) Claim

func (s *MessageStore) Claim(ctx context.Context, userID, convID, msgID uuid.UUID) (resource.Claimed, error)

Claim implements resource.MessageStore. The UPDATE's WHERE clause is the compare-and-set: of several concurrent callers only one still sees "pending".

Pattern: Compare-and-Set Claim

func (*MessageStore) Finish

func (s *MessageStore) Finish(ctx context.Context, msgID uuid.UUID, out resource.Outcome) error

Finish implements resource.MessageStore.

Pattern: Zero-Row Diagnosis

func (*MessageStore) Get

func (s *MessageStore) Get(ctx context.Context, userID, convID, msgID uuid.UUID) (domain.Message, error)

Get implements resource.MessageStore.

func (*MessageStore) List

func (s *MessageStore) List(ctx context.Context, userID, convID uuid.UUID) ([]domain.Message, error)

List implements resource.MessageStore. It asks in two statements: whether the user owns the conversation, then for its messages. A conversation that is deleted between the two lists as empty, not as missing. Joining the two would not give the caller one view of the conversation: the use case reads the conversation itself first, in a statement of its own.

func (*MessageStore) RecoverInterrupted

func (s *MessageStore) RecoverInterrupted(ctx context.Context, olderThan time.Duration) (int, error)

RecoverInterrupted implements resource.MessageStore.

Pattern: Recover at Startup

func (*MessageStore) ReplaceLastAssistant

func (s *MessageStore) ReplaceLastAssistant(ctx context.Context, userID, convID, msgID uuid.UUID) (domain.Message, error)

ReplaceLastAssistant implements resource.MessageStore.

type Migrator

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

Migrator applies the embedded SQL migrations. golang-migrate takes a PostgreSQL advisory lock while it runs, so several instances can start at once and only one of them migrates.

func NewMigrator

func NewMigrator(pool *pgxpool.Pool) (*Migrator, error)

NewMigrator prepares a Migrator that shares pool's connections. The pool needs room for one extra connection, which holds the advisory lock.

func (*Migrator) Close

func (m *Migrator) Close() error

Close releases the migrator's resources. The pool stays open.

func (*Migrator) Down

func (m *Migrator) Down() error

Down reverts every migration. It exists for tests and local resets; the application never calls it.

func (*Migrator) Up

func (m *Migrator) Up() error

Up applies every pending migration. Nothing to do is not an error.

func (*Migrator) Version

func (m *Migrator) Version() (version uint, dirty bool, err error)

Version reports the applied version (0 when none) and whether a failed migration left the database dirty.

type SessionStore

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

SessionStore is the PostgreSQL implementation of resource.SessionStore.

func NewSessionStore

func NewSessionStore(pool *pgxpool.Pool, sql Statements) *SessionStore

NewSessionStore returns a store backed by pool, with its SQL from sql.

func (*SessionStore) Create

func (s *SessionStore) Create(ctx context.Context, sess domain.Session) error

Create implements resource.SessionStore.

func (*SessionStore) Delete

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

Delete implements resource.SessionStore.

func (*SessionStore) DeleteExpired

func (s *SessionStore) DeleteExpired(ctx context.Context, now time.Time) (int, error)

DeleteExpired implements resource.SessionStore.

func (*SessionStore) Get

func (s *SessionStore) Get(ctx context.Context, id string) (domain.Session, error)

Get implements resource.SessionStore.

type Statements

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

Statements is the SQL that the stores send, filled in and ready for the driver. It comes from LoadStatements; the zero value holds no SQL.

func LoadStatements

func LoadStatements() (Statements, error)

LoadStatements reads the embedded templates into a cache and fills in every statement from it. A program calls it once, when it starts: a statement that is missing or does not parse stops the start, and no request finds out. No statement takes data today, so the stores keep the texts and not the cache.

type UserStore

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

UserStore is the PostgreSQL implementation of resource.UserStore.

func NewUserStore

func NewUserStore(pool *pgxpool.Pool, sql Statements) *UserStore

NewUserStore returns a store backed by pool, with its SQL from sql.

func (*UserStore) ByEmail

func (s *UserStore) ByEmail(ctx context.Context, email string) (domain.User, error)

ByEmail implements resource.UserStore.

func (*UserStore) ByID

func (s *UserStore) ByID(ctx context.Context, id uuid.UUID) (domain.User, error)

ByID implements resource.UserStore.

func (*UserStore) Count

func (s *UserStore) Count(ctx context.Context) (int, error)

Count implements resource.UserStore.

func (*UserStore) Create

func (s *UserStore) Create(ctx context.Context, u domain.User) (domain.User, error)

Create implements resource.UserStore.

Pattern: Named Constraints

Jump to

Keyboard shortcuts

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