Documentation
¶
Overview ¶
Package postgres is the PostgreSQL driven adapter: connection pool, migrations and the stores that implement the core's resource ports.
Index ¶
- func Migrate(pool *pgxpool.Pool) error
- func NewPool(ctx context.Context, cfg config.DB) (*pgxpool.Pool, error)
- type ConversationStore
- func (s *ConversationStore) Create(ctx context.Context, c domain.Conversation) (domain.Conversation, error)
- func (s *ConversationStore) Delete(ctx context.Context, userID, id uuid.UUID) error
- func (s *ConversationStore) Get(ctx context.Context, userID, id uuid.UUID) (domain.Conversation, error)
- func (s *ConversationStore) List(ctx context.Context, userID uuid.UUID, limit int) ([]domain.Conversation, error)
- func (s *ConversationStore) Rename(ctx context.Context, userID, id uuid.UUID, title string) error
- type MessageStore
- func (s *MessageStore) AppendPair(ctx context.Context, userID, convID uuid.UUID, content string) (user, reply domain.Message, err error)
- func (s *MessageStore) Claim(ctx context.Context, userID, convID, msgID uuid.UUID) (resource.Claimed, error)
- func (s *MessageStore) Finish(ctx context.Context, msgID uuid.UUID, out resource.Outcome) error
- func (s *MessageStore) Get(ctx context.Context, userID, convID, msgID uuid.UUID) (domain.Message, error)
- func (s *MessageStore) List(ctx context.Context, userID, convID uuid.UUID) ([]domain.Message, error)
- func (s *MessageStore) RecoverInterrupted(ctx context.Context, olderThan time.Duration) (int, error)
- func (s *MessageStore) ReplaceLastAssistant(ctx context.Context, userID, convID, msgID uuid.UUID) (domain.Message, error)
- type Migrator
- type SessionStore
- func (s *SessionStore) Create(ctx context.Context, sess domain.Session) error
- func (s *SessionStore) Delete(ctx context.Context, id string) error
- func (s *SessionStore) DeleteExpired(ctx context.Context, now time.Time) (int, error)
- func (s *SessionStore) Get(ctx context.Context, id string) (domain.Session, error)
- type Statements
- type UserStore
- func (s *UserStore) ByEmail(ctx context.Context, email string) (domain.User, error)
- func (s *UserStore) ByID(ctx context.Context, id uuid.UUID) (domain.User, error)
- func (s *UserStore) Count(ctx context.Context) (int, error)
- func (s *UserStore) Create(ctx context.Context, u domain.User) (domain.User, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewPool ¶
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 ¶
func (s *ConversationStore) Create(ctx context.Context, c domain.Conversation) (domain.Conversation, error)
Create 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
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) 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 ¶
NewMigrator prepares a Migrator that shares pool's connections. The pool needs room for one extra connection, which holds the advisory lock.
func (*Migrator) Down ¶
Down reverts every migration. It exists for tests and local resets; the application never calls it.
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) Delete ¶
func (s *SessionStore) Delete(ctx context.Context, id string) error
Delete implements resource.SessionStore.
func (*SessionStore) DeleteExpired ¶
DeleteExpired 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.