postgres

package
v0.0.0-...-cd63039 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package postgres implements the storage interfaces using PostgreSQL with pgvector and ltree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DSN is the PostgreSQL connection string.
	// Example: "postgres://user:pass@localhost:5432/known?sslmode=disable"
	DSN string

	// MaxConns sets the maximum number of connections in the pool.
	// Zero uses pgxpool's default.
	MaxConns int32
}

Config holds PostgreSQL connection parameters.

type DB

type DB struct {
	Pool *pgxpool.Pool
	// contains filtered or unexported fields
}

DB wraps a pgxpool and provides access to repository implementations.

func New

func New(ctx context.Context, cfg Config) (*DB, error)

New creates a new DB with a connection pool. The caller must call Close when finished.

func (*DB) Close

func (db *DB) Close() error

Close releases all pool resources.

func (*DB) Edges

func (db *DB) Edges() storage.EdgeRepo

Edges returns the EdgeRepo implementation backed by this DB.

func (*DB) Entries

func (db *DB) Entries() storage.EntryRepo

Entries returns the EntryRepo implementation backed by this DB.

func (*DB) Migrate

func (db *DB) Migrate() error

Migrate runs all pending database migrations.

func (*DB) Scopes

func (db *DB) Scopes() storage.ScopeRepo

Scopes returns the ScopeRepo implementation backed by this DB.

func (*DB) WithTx

func (db *DB) WithTx(ctx context.Context, fn func(ctx context.Context) error) error

WithTx runs fn within a database transaction. The transaction is stored in the context, so any repo method called with the returned context will participate in the same transaction. The transaction is committed if fn returns nil, and rolled back otherwise.

If a transaction is already active in the context, fn runs within the existing transaction (no nesting).

type DBTX

type DBTX interface {
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

DBTX is the common interface between *pgxpool.Pool and pgx.Tx, allowing repo methods to operate within or outside a transaction.

type EdgeStore

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

EdgeStore implements storage.EdgeRepo using PostgreSQL.

func (*EdgeStore) Create

func (s *EdgeStore) Create(ctx context.Context, edge *model.Edge) error

Create persists a new edge. When called within a transaction (via context), both the edge type registration and edge insert use the same transaction. Without a caller-provided transaction, each operation runs independently, which is acceptable since edge type registration uses ON CONFLICT DO NOTHING.

func (*EdgeStore) Delete

func (s *EdgeStore) Delete(ctx context.Context, id model.ID) error

Delete removes an edge by ID.

func (*EdgeStore) EdgesBetween

func (s *EdgeStore) EdgesBetween(ctx context.Context, fromID, toID model.ID) ([]model.Edge, error)

EdgesBetween returns edges from source to target.

func (*EdgeStore) EdgesFrom

func (s *EdgeStore) EdgesFrom(ctx context.Context, entryID model.ID, filter storage.EdgeFilter) ([]model.Edge, error)

EdgesFrom returns all outgoing edges from the given entry.

func (*EdgeStore) EdgesTo

func (s *EdgeStore) EdgesTo(ctx context.Context, entryID model.ID, filter storage.EdgeFilter) ([]model.Edge, error)

EdgesTo returns all incoming edges to the given entry.

func (*EdgeStore) FindConflicts

func (s *EdgeStore) FindConflicts(ctx context.Context, entryID model.ID) ([]model.Entry, error)

FindConflicts returns entries that have a "contradicts" edge involving the given entry.

func (*EdgeStore) Get

func (s *EdgeStore) Get(ctx context.Context, id model.ID) (*model.Edge, error)

Get retrieves an edge by ID.

type EntryStore

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

EntryStore implements storage.EntryRepo using PostgreSQL with pgvector.

func (*EntryStore) Create

func (s *EntryStore) Create(ctx context.Context, entry *model.Entry) error

Create persists a new entry. It automatically ensures the scope hierarchy exists. If not already within a transaction, wraps both operations in one for atomicity.

func (*EntryStore) CreateOrUpdate

func (s *EntryStore) CreateOrUpdate(ctx context.Context, entry *model.Entry) (*model.Entry, error)

CreateOrUpdate inserts a new entry or updates an existing one with the same content hash and scope. Uses ON CONFLICT (content_hash, scope) for idempotent upserts. If not already within a transaction, wraps both operations in one for atomicity.

func (*EntryStore) Delete

func (s *EntryStore) Delete(ctx context.Context, id model.ID) error

Delete removes an entry by ID.

func (*EntryStore) DeleteExpired

func (s *EntryStore) DeleteExpired(ctx context.Context) (int64, error)

DeleteExpired removes entries whose ExpiresAt is in the past.

func (*EntryStore) Get

func (s *EntryStore) Get(ctx context.Context, id model.ID) (*model.Entry, error)

Get retrieves an entry by ID.

func (*EntryStore) List

func (s *EntryStore) List(ctx context.Context, filter storage.EntryFilter) ([]model.Entry, error)

List returns entries matching the given filter.

func (*EntryStore) SearchSimilar

func (s *EntryStore) SearchSimilar(ctx context.Context, query []float32, scope string, metric storage.SimilarityMetric, limit int) ([]storage.SimilarityResult, error)

SearchSimilar finds entries with embeddings similar to the query vector.

func (*EntryStore) Update

func (s *EntryStore) Update(ctx context.Context, entry *model.Entry) error

Update replaces an existing entry with optimistic concurrency control. The update only succeeds if the entry's version matches. On success, the version is incremented both in the database and on the entry struct.

type ScopeStore

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

ScopeStore implements storage.ScopeRepo using PostgreSQL with ltree.

func (*ScopeStore) Delete

func (s *ScopeStore) Delete(ctx context.Context, path string) error

Delete removes a scope by path.

func (*ScopeStore) EnsureHierarchy

func (s *ScopeStore) EnsureHierarchy(ctx context.Context, path string) error

EnsureHierarchy ensures that all ancestor scopes exist for a given scope path. For example, given "project.auth.oauth", it ensures "project", "project.auth", and "project.auth.oauth" all exist.

func (*ScopeStore) Get

func (s *ScopeStore) Get(ctx context.Context, path string) (*model.Scope, error)

Get retrieves a scope by path.

func (*ScopeStore) List

func (s *ScopeStore) List(ctx context.Context) ([]model.Scope, error)

List returns all scopes ordered by path.

func (*ScopeStore) ListChildren

func (s *ScopeStore) ListChildren(ctx context.Context, parentPath string) ([]model.Scope, error)

ListChildren returns direct child scopes of the given parent path.

func (*ScopeStore) ListDescendants

func (s *ScopeStore) ListDescendants(ctx context.Context, ancestorPath string) ([]model.Scope, error)

ListDescendants returns all descendant scopes of the given ancestor path.

func (*ScopeStore) Upsert

func (s *ScopeStore) Upsert(ctx context.Context, scope *model.Scope) error

Upsert creates or updates a scope.

Jump to

Keyboard shortcuts

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