store

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package store provides PostgreSQL-backed data access for Creel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureSchema added in v0.1.4

func EnsureSchema(ctx context.Context, baseURL, schema string) error

EnsureSchema creates the given schema if it does not already exist. It opens a one-shot connection to baseURL (which should not include search_path), validates the schema name, runs CREATE SCHEMA IF NOT EXISTS, and closes the connection.

func NewPool

func NewPool(ctx context.Context, postgresURL string) (*pgxpool.Pool, error)

NewPool creates a new pgxpool connection pool.

func RunMigrations

func RunMigrations(postgresURL, migrationsPath string) error

RunMigrations applies all pending database migrations.

Types

type Chunk

type Chunk struct {
	ID          string
	DocumentID  string
	Sequence    int
	Content     string
	EmbeddingID *string
	Status      string
	CompactedBy *string
	Metadata    map[string]any
	CreatedAt   time.Time
}

Chunk represents a stored chunk.

type ChunkStore

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

ChunkStore handles chunk persistence.

func NewChunkStore

func NewChunkStore(pool DBTX) *ChunkStore

NewChunkStore creates a new chunk store.

func (*ChunkStore) ChunkIDsByTopics

func (s *ChunkStore) ChunkIDsByTopics(ctx context.Context, topicIDs []string, excludeDocIDs []string) ([]string, error)

ChunkIDsByTopics returns all active chunk IDs belonging to the given topics.

func (*ChunkStore) Create

func (s *ChunkStore) Create(ctx context.Context, documentID, content string, sequence int, metadata map[string]any) (*Chunk, error)

Create inserts a new chunk and returns it.

func (*ChunkStore) Delete

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

Delete removes a chunk.

func (*ChunkStore) DocumentTopicID

func (s *ChunkStore) DocumentTopicID(ctx context.Context, documentID string) (string, error)

DocumentTopicID returns the topic ID for a chunk's document.

func (*ChunkStore) DocumentTopicIDs

func (s *ChunkStore) DocumentTopicIDs(ctx context.Context, docIDs []string) (map[string]string, error)

DocumentTopicIDs returns a mapping of document ID to topic ID for the given documents.

func (*ChunkStore) Get

func (s *ChunkStore) Get(ctx context.Context, id string) (*Chunk, error)

Get retrieves a chunk by ID.

func (*ChunkStore) GetMultiple

func (s *ChunkStore) GetMultiple(ctx context.Context, ids []string) (map[string]*Chunk, error)

GetMultiple retrieves multiple chunks by ID in a single query.

func (*ChunkStore) ListByDocument added in v0.1.4

func (s *ChunkStore) ListByDocument(ctx context.Context, documentID string, lastN int, since time.Time) ([]*Chunk, error)

ListByDocument returns active chunks for a document in sequence order. If lastN > 0, returns only the last N chunks (ordered ascending by sequence). If since is non-zero, only returns chunks created at or after that time.

func (*ChunkStore) SetEmbeddingID

func (s *ChunkStore) SetEmbeddingID(ctx context.Context, chunkID, embeddingID string) error

SetEmbeddingID updates the embedding_id for a chunk.

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
	Begin(ctx context.Context) (pgx.Tx, error)
}

DBTX is the interface satisfied by *pgxpool.Pool and *pgx.Tx. All stores accept this instead of a concrete pool so that a QueryCounter (or transaction) can be substituted in tests.

type Document

type Document struct {
	ID        string
	TopicID   string
	Slug      string
	Name      string
	DocType   string
	Metadata  map[string]any
	CreatedAt time.Time
	UpdatedAt time.Time
}

Document represents a stored document.

type DocumentStore

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

DocumentStore handles document persistence.

func NewDocumentStore

func NewDocumentStore(pool DBTX) *DocumentStore

NewDocumentStore creates a new document store.

func (*DocumentStore) Create

func (s *DocumentStore) Create(ctx context.Context, topicID, slug, name, docType string, metadata map[string]any) (*Document, error)

Create inserts a new document.

func (*DocumentStore) Delete

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

Delete removes a document and cascades to chunks.

func (*DocumentStore) Get

func (s *DocumentStore) Get(ctx context.Context, id string) (*Document, error)

Get retrieves a document by ID.

func (*DocumentStore) ListByTopic

func (s *DocumentStore) ListByTopic(ctx context.Context, topicID string) ([]Document, error)

ListByTopic returns documents in a topic.

func (*DocumentStore) TopicIDForDocument

func (s *DocumentStore) TopicIDForDocument(ctx context.Context, docID string) (string, error)

TopicIDForDocument returns the topic ID that owns the given document.

func (*DocumentStore) Update

func (s *DocumentStore) Update(ctx context.Context, id, name, docType string, metadata map[string]any) (*Document, error)

Update modifies a document's name, doc_type, and metadata.

type GrantStore

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

GrantStore provides access to topic grants and ownership from PostgreSQL.

func NewGrantStore

func NewGrantStore(pool DBTX) *GrantStore

NewGrantStore creates a new grant store.

func (*GrantStore) GrantsForPrincipal

func (s *GrantStore) GrantsForPrincipal(ctx context.Context, principals []string) ([]auth.Grant, error)

GrantsForPrincipal returns all grants matching any of the given principals.

func (*GrantStore) TopicOwner

func (s *GrantStore) TopicOwner(ctx context.Context, topicID string) (string, error)

TopicOwner returns the owner of the given topic.

type SystemAccount

type SystemAccount struct {
	ID          string
	Name        string
	Description string
	Principal   string
	CreatedAt   time.Time
}

SystemAccount represents a stored system account.

type SystemAccountKey

type SystemAccountKey struct {
	ID             string
	AccountID      string
	KeyHash        string
	KeyPrefix      string
	Status         string // "active", "grace_period", "revoked"
	GraceExpiresAt *time.Time
	CreatedAt      time.Time
	RevokedAt      *time.Time
}

SystemAccountKey represents a stored API key.

type SystemAccountStore

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

SystemAccountStore handles system account and key persistence.

func NewSystemAccountStore

func NewSystemAccountStore(pool DBTX) *SystemAccountStore

NewSystemAccountStore creates a new store backed by the given pool.

func (*SystemAccountStore) Create

func (s *SystemAccountStore) Create(ctx context.Context, name, description string) (*SystemAccount, string, error)

Create inserts a new system account and its initial key. Returns the account and the raw API key (shown once).

func (*SystemAccountStore) Delete

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

Delete removes a system account and cascades to its keys.

func (*SystemAccountStore) List

List returns all system accounts.

func (*SystemAccountStore) LookupKeyHash

func (s *SystemAccountStore) LookupKeyHash(ctx context.Context, hash string) (*auth.Principal, error)

LookupKeyHash implements auth.KeyLookup. It resolves a key hash to a principal by checking active and grace-period keys.

func (*SystemAccountStore) RevokeKey

func (s *SystemAccountStore) RevokeKey(ctx context.Context, accountID string) error

RevokeKey immediately revokes all active keys for the account.

func (*SystemAccountStore) RotateKey

func (s *SystemAccountStore) RotateKey(ctx context.Context, accountID string, gracePeriod time.Duration) (string, error)

RotateKey generates a new key for the account. If gracePeriod > 0, old active keys enter grace period; otherwise they are revoked immediately.

type Topic

type Topic struct {
	ID          string
	Slug        string
	Name        string
	Description string
	Owner       string
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

Topic represents a stored topic.

type TopicGrant

type TopicGrant struct {
	ID         string
	TopicID    string
	Principal  string
	Permission string
	GrantedBy  string
	CreatedAt  time.Time
}

TopicGrant represents a stored topic grant.

type TopicStore

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

TopicStore handles topic and grant persistence.

func NewTopicStore

func NewTopicStore(pool DBTX) *TopicStore

NewTopicStore creates a new topic store.

func (*TopicStore) Create

func (s *TopicStore) Create(ctx context.Context, slug, name, description, owner string) (*Topic, error)

Create inserts a new topic.

func (*TopicStore) Delete

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

Delete removes a topic and cascades to grants, documents, chunks.

func (*TopicStore) Get

func (s *TopicStore) Get(ctx context.Context, id string) (*Topic, error)

Get retrieves a topic by ID.

func (*TopicStore) Grant

func (s *TopicStore) Grant(ctx context.Context, topicID, principal, permission, grantedBy string) (*TopicGrant, error)

Grant creates or updates a topic grant.

func (*TopicStore) ListForPrincipals

func (s *TopicStore) ListForPrincipals(ctx context.Context, principals []string) ([]Topic, error)

ListForPrincipals returns topics accessible to the given principal identities. If principals is nil, returns all topics (for system accounts).

func (*TopicStore) ListGrants

func (s *TopicStore) ListGrants(ctx context.Context, topicID string) ([]TopicGrant, error)

ListGrants returns all grants for a topic.

func (*TopicStore) Revoke

func (s *TopicStore) Revoke(ctx context.Context, topicID, principal string) error

Revoke removes a topic grant.

func (*TopicStore) Update

func (s *TopicStore) Update(ctx context.Context, id, name, description string) (*Topic, error)

Update modifies a topic's name and description.

Directories

Path Synopsis
Package dbtest provides test helpers for database stores.
Package dbtest provides test helpers for database stores.

Jump to

Keyboard shortcuts

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