store

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a requested record does not exist.

Functions

This section is empty.

Types

type APIKey

type APIKey struct {
	ID        string
	KeyHash   string
	TenantID  string
	Name      string
	CreatedAt time.Time
	RevokedAt *time.Time
}

APIKey represents a row in the sandbox.api_keys table.

type DBTX added in v0.3.0

type DBTX interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

DBTX is the common interface satisfied by both *sql.DB and *sql.Tx. Store methods use this internally so they work inside transactions without code changes.

type Execution

type Execution struct {
	ID           string          `json:"execution_id"`
	SkillName    string          `json:"skill_name"`
	SkillVersion string          `json:"skill_version"`
	TenantID     string          `json:"tenant_id"`
	Status       string          `json:"status"`
	Input        json.RawMessage `json:"input,omitempty"`
	Output       json.RawMessage `json:"output,omitempty"`
	Logs         string          `json:"logs,omitempty"`
	FilesURL     string          `json:"files_url,omitempty"`
	FilesList    []string        `json:"files_list,omitempty"`
	DurationMs   int64           `json:"duration_ms"`
	Error        *string         `json:"error"`
	CreatedAt    time.Time       `json:"created_at"`
	FinishedAt   *time.Time      `json:"finished_at,omitempty"`
}

Execution represents a row in the sandbox.executions table.

type File added in v0.2.0

type File struct {
	ID          string    `json:"id"`
	TenantID    string    `json:"tenant_id"`
	SessionID   string    `json:"session_id,omitempty"`
	ExecutionID string    `json:"execution_id,omitempty"`
	Name        string    `json:"name"`
	ContentType string    `json:"content_type"`
	SizeBytes   int64     `json:"size_bytes"`
	S3Key       string    `json:"s3_key"`
	Version     int       `json:"version"`
	ParentID    *string   `json:"parent_id,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

File represents a row in the sandbox.files table.

type FileFilter added in v0.2.0

type FileFilter struct {
	TenantID    string
	SessionID   string
	ExecutionID string
	Limit       int
	Offset      int
}

FileFilter describes the filter and pagination parameters for listing files.

type SkillRecord

type SkillRecord struct {
	TenantID    string    `json:"tenant_id"`
	Name        string    `json:"name"`
	Version     string    `json:"version"`
	Description string    `json:"description"`
	Lang        string    `json:"lang"`
	UploadedAt  time.Time `json:"uploaded_at"`
}

SkillRecord represents a row in the sandbox.skills metadata table.

type Store

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

Store wraps a PostgreSQL connection pool and provides data-access methods for the Skillbox runtime. When tx is set (via RunInTx), all queries go through the transaction instead of the pool.

func New

func New(dsn string) (*Store, error)

New opens a connection pool to PostgreSQL using the provided DSN, verifies connectivity, and runs all embedded migrations. It returns a ready-to-use Store or an error if any step fails.

func NewWithDB

func NewWithDB(db *sql.DB) *Store

NewWithDB creates a Store that wraps the given *sql.DB without running migrations. This is intended for use in tests that provide a mock or in-memory database.

func (*Store) Close

func (s *Store) Close() error

Close releases the database connection pool.

func (*Store) CreateExecution

func (s *Store) CreateExecution(ctx context.Context, e *Execution) (*Execution, error)

CreateExecution inserts a new execution record with status "running". The Execution is mutated in place with the server-generated ID and timestamp.

func (*Store) CreateFile added in v0.2.0

func (s *Store) CreateFile(ctx context.Context, f *File) (*File, error)

CreateFile inserts a new file record. The File is mutated in place with the server-generated ID and timestamps.

func (*Store) CreateKey

func (s *Store) CreateKey(ctx context.Context, tenantID, name, keyHash string) (*APIKey, error)

CreateKey inserts a new API key record and returns the populated APIKey (including the server-generated UUID and timestamp).

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying *sql.DB for use in tests or advanced queries.

func (*Store) DeleteFile added in v0.2.0

func (s *Store) DeleteFile(ctx context.Context, id, tenantID string) error

DeleteFile removes a file record by its UUID, scoped to a tenant.

func (*Store) DeleteSkill

func (s *Store) DeleteSkill(ctx context.Context, tenantID, name, version string) error

DeleteSkill removes a skill metadata record.

func (*Store) GetAPIKeyByHash

func (s *Store) GetAPIKeyByHash(ctx context.Context, hash string) (*APIKey, error)

GetAPIKeyByHash looks up an API key by its SHA-256 hex hash. It returns ErrNotFound if no matching key exists.

func (*Store) GetExecution

func (s *Store) GetExecution(ctx context.Context, id, tenantID string) (*Execution, error)

GetExecution retrieves a single execution by its UUID, scoped to a tenant. Returns ErrNotFound if the execution does not exist or belongs to another tenant.

func (*Store) GetFile added in v0.2.0

func (s *Store) GetFile(ctx context.Context, id, tenantID string) (*File, error)

GetFile retrieves a single file record by its UUID, scoped to a tenant. Returns ErrNotFound if the file does not exist or belongs to another tenant.

func (*Store) GetSkill

func (s *Store) GetSkill(ctx context.Context, tenantID, name, version string) (*SkillRecord, error)

GetSkill retrieves a single skill metadata record by tenant, name, and version.

func (*Store) InsertExecution

func (s *Store) InsertExecution(ctx context.Context, e *Execution) error

InsertExecution creates a new execution record. This is an alias kept for compatibility with callers that set status before calling.

func (*Store) ListExecutions

func (s *Store) ListExecutions(ctx context.Context, tenantID string, limit, offset int) ([]Execution, error)

ListExecutions returns executions for a tenant ordered by creation time (newest first), with pagination via limit and offset.

func (*Store) ListFileVersions added in v0.2.0

func (s *Store) ListFileVersions(ctx context.Context, fileID, tenantID string) ([]*File, error)

ListFileVersions returns all versions of a file, following the parent_id chain. It finds all files where id = fileID or parent_id = fileID, ordered by version descending. Scoped to tenant_id for isolation.

func (*Store) ListFiles added in v0.2.0

func (s *Store) ListFiles(ctx context.Context, filter FileFilter) ([]*File, error)

ListFiles returns files matching the given filter, ordered by creation time (newest first), with pagination via limit and offset.

func (*Store) ListKeys

func (s *Store) ListKeys(ctx context.Context, tenantID string) ([]APIKey, error)

ListKeys returns all API keys belonging to the given tenant, ordered by creation time (newest first). Revoked keys are included so the caller can display their status.

func (*Store) ListSkills

func (s *Store) ListSkills(ctx context.Context, tenantID string) ([]SkillRecord, error)

ListSkills returns all skill metadata for a tenant, ordered by name then version.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping verifies the database connection is alive.

func (*Store) ResolveLatestVersion added in v0.2.0

func (s *Store) ResolveLatestVersion(ctx context.Context, tenantID, name string) (string, error)

ResolveLatestVersion returns the version string of the most recently uploaded skill for a given tenant and name. If no versions exist, it returns ErrNotFound.

func (*Store) RevokeKey

func (s *Store) RevokeKey(ctx context.Context, keyID string) error

RevokeKey soft-deletes an API key by setting its revoked_at timestamp. It returns an error if the key does not exist or is already revoked.

func (*Store) RunInTx added in v0.3.0

func (s *Store) RunInTx(ctx context.Context, fn func(tx *Store) error) error

RunInTx executes fn inside a database transaction. If fn returns a non-nil error the transaction is rolled back; otherwise it is committed. The Store passed to fn shares the same transaction so all its methods operate atomically.

func (*Store) UpdateExecution

func (s *Store) UpdateExecution(ctx context.Context, e *Execution) error

UpdateExecution writes back mutable fields for an existing execution. Typically called once the execution has completed (or timed out).

func (*Store) UpdateFile added in v0.2.0

func (s *Store) UpdateFile(ctx context.Context, f *File) error

UpdateFile updates a file record's mutable fields: name, content_type, size_bytes, s3_key, and version. It also sets updated_at to now().

func (*Store) UpsertSkill

func (s *Store) UpsertSkill(ctx context.Context, rec *SkillRecord) error

UpsertSkill inserts or updates a skill metadata record. On conflict (same tenant, name, version) it updates the description and lang.

func (*Store) ValidateKey

func (s *Store) ValidateKey(ctx context.Context, keyHash string) (*APIKey, error)

ValidateKey looks up an API key by its SHA-256 hash. It returns the matching APIKey if the hash exists and the key has not been revoked. A nil APIKey (with nil error) is returned when the key is not found or has been revoked.

Jump to

Keyboard shortcuts

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