store

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package store provides database interfaces and types for the Lunar application. It defines the DB interface for function, version, and execution operations, along with core data types used throughout the application.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFunctionNotFound          = errors.New("function not found")
	ErrVersionNotFound           = errors.New("version not found")
	ErrNoActiveVersion           = errors.New("no active version")
	ErrExecutionNotFound         = errors.New("execution not found")
	ErrCannotDeleteActiveVersion = errors.New("cannot delete active version")
	ErrAPITokenNotFound          = errors.New("api token not found")
)

Functions

This section is empty.

Types

type AIRequest

type AIRequest struct {
	ID           string          `json:"id"`
	ExecutionID  string          `json:"execution_id"`
	Provider     string          `json:"provider"`
	Model        string          `json:"model"`
	Endpoint     string          `json:"endpoint"`
	RequestJSON  string          `json:"request_json"`
	ResponseJSON *string         `json:"response_json,omitempty"`
	Status       AIRequestStatus `json:"status"`
	ErrorMessage *string         `json:"error_message,omitempty"`
	InputTokens  *int            `json:"input_tokens,omitempty"`
	OutputTokens *int            `json:"output_tokens,omitempty"`
	DurationMs   int64           `json:"duration_ms"`
	CreatedAt    int64           `json:"created_at"`
}

AIRequest represents a tracked AI API request

type AIRequestStatus

type AIRequestStatus string

AIRequestStatus represents the status of an AI API request

const (
	AIRequestStatusSuccess AIRequestStatus = "success"
	AIRequestStatusError   AIRequestStatus = "error"
)

type APIToken

type APIToken struct {
	ID        string `json:"id"`
	TokenHash string `json:"-"`
	Name      string `json:"name"`
	CreatedAt int64  `json:"created_at"`
	LastUsed  *int64 `json:"last_used,omitempty"`
	Revoked   bool   `json:"revoked"`
}

APIToken represents a stored API token for CLI authentication

type CronStatus

type CronStatus string

CronStatus represents the status of a cron schedule

const (
	CronStatusActive CronStatus = "active"
	CronStatusPaused CronStatus = "paused"
)

type DB

type DB interface {
	// CreateFunction creates a new function. Returns the created function with
	// timestamps populated.
	CreateFunction(ctx context.Context, fn Function) (Function, error)

	// GetFunction retrieves a function by ID.
	// Returns ErrFunctionNotFound if the function does not exist.
	GetFunction(ctx context.Context, id string) (Function, error)

	// ListFunctions returns paginated functions with their active versions.
	ListFunctions(ctx context.Context, params PaginationParams) ([]FunctionWithActiveVersion, int64, error)

	// UpdateFunction updates a function's fields.
	// Returns ErrFunctionNotFound if the function does not exist.
	UpdateFunction(ctx context.Context, id string, updates UpdateFunctionRequest) error

	// DeleteFunction removes a function and its associated data.
	// Returns ErrFunctionNotFound if the function does not exist.
	DeleteFunction(ctx context.Context, id string) error

	// CreateVersion creates a new version for a function and sets it as active.
	// Returns ErrFunctionNotFound if the function does not exist.
	CreateVersion(ctx context.Context, functionID string, code string, createdBy *string) (FunctionVersion, error)

	// GetVersion retrieves a specific version by function ID and version number.
	// Returns ErrVersionNotFound if the version does not exist.
	GetVersion(ctx context.Context, functionID string, version int) (FunctionVersion, error)

	// GetVersionByID retrieves a version by its unique ID.
	// Returns ErrVersionNotFound if the version does not exist.
	GetVersionByID(ctx context.Context, versionID string) (FunctionVersion, error)

	// ListVersions returns paginated versions for a function.
	ListVersions(ctx context.Context, functionID string, params PaginationParams) ([]FunctionVersion, int64, error)

	// GetActiveVersion retrieves the currently active version for a function.
	// Returns ErrNoActiveVersion if no version is active.
	GetActiveVersion(ctx context.Context, functionID string) (FunctionVersion, error)

	// ActivateVersion sets a specific version as the active version by its ID.
	// Returns ErrVersionNotFound if the version does not exist.
	ActivateVersion(ctx context.Context, versionID string) error

	// DeleteVersion removes a specific version by its ID.
	// Returns ErrVersionNotFound if the version does not exist.
	// Returns ErrCannotDeleteActiveVersion if attempting to delete the active version.
	DeleteVersion(ctx context.Context, versionID string) error

	// CreateExecution records a new execution. Returns the execution with
	// timestamps populated.
	CreateExecution(ctx context.Context, exec Execution) (Execution, error)

	// GetExecution retrieves an execution by ID.
	// Returns ErrExecutionNotFound if the execution does not exist.
	GetExecution(ctx context.Context, executionID string) (Execution, error)

	// UpdateExecution updates an execution's status and results.
	// Returns ErrExecutionNotFound if the execution does not exist.
	UpdateExecution(ctx context.Context, executionID string, status ExecutionStatus, durationMs *int64, errorMsg *string, responseJSON *string) error

	// ListExecutions returns paginated executions for a function.
	ListExecutions(ctx context.Context, functionID string, params PaginationParams) ([]Execution, int64, error)

	// DeleteOldExecutions removes executions older than the given timestamp.
	// Returns the number of deleted records.
	DeleteOldExecutions(ctx context.Context, beforeTimestamp int64) (int64, error)

	// ListFunctionsWithActiveCron returns all functions that have an active cron schedule.
	ListFunctionsWithActiveCron(ctx context.Context) ([]Function, error)

	// Ping verifies the database connection is alive.
	Ping(ctx context.Context) error

	// CreateAPIToken stores a new API token.
	CreateAPIToken(ctx context.Context, token APIToken) (APIToken, error)

	// GetAPITokenByHash retrieves a non-revoked API token by its hash.
	// Returns ErrAPITokenNotFound if no matching token exists.
	GetAPITokenByHash(ctx context.Context, tokenHash string) (APIToken, error)

	// ListAPITokens returns all API tokens ordered by creation date.
	ListAPITokens(ctx context.Context) ([]APIToken, error)

	// RevokeAPIToken marks an API token as revoked.
	// Returns ErrAPITokenNotFound if the token does not exist.
	RevokeAPIToken(ctx context.Context, id string) error

	// UpdateAPITokenLastUsed updates the last_used timestamp for a token.
	UpdateAPITokenLastUsed(ctx context.Context, id string, timestamp int64) error
}

DB defines the database interface for the Lunar API.

type EmailRequest

type EmailRequest struct {
	ID           string             `json:"id"`
	ExecutionID  string             `json:"execution_id"`
	From         string             `json:"from"`
	To           []string           `json:"to"`
	Subject      string             `json:"subject"`
	HasText      bool               `json:"has_text"`
	HasHTML      bool               `json:"has_html"`
	RequestJSON  string             `json:"request_json"`
	ResponseJSON *string            `json:"response_json,omitempty"`
	Status       EmailRequestStatus `json:"status"`
	ErrorMessage *string            `json:"error_message,omitempty"`
	EmailID      *string            `json:"email_id,omitempty"`
	DurationMs   int64              `json:"duration_ms"`
	CreatedAt    int64              `json:"created_at"`
}

EmailRequest represents a tracked email send request

type EmailRequestStatus

type EmailRequestStatus string

EmailRequestStatus represents the status of an email request

const (
	EmailRequestStatusSuccess EmailRequestStatus = "success"
	EmailRequestStatusError   EmailRequestStatus = "error"
)

type Execution

type Execution struct {
	ID                string           `json:"id"`
	FunctionID        string           `json:"function_id"`
	FunctionVersionID string           `json:"function_version_id"`
	Status            ExecutionStatus  `json:"status"`
	DurationMs        *int64           `json:"duration_ms,omitempty"`
	ErrorMessage      *string          `json:"error_message,omitempty"`
	EventJSON         *string          `json:"event_json,omitempty"`
	ResponseJSON      *string          `json:"response_json,omitempty"`
	Trigger           ExecutionTrigger `json:"trigger"`
	CreatedAt         int64            `json:"created_at"`
}

Execution represents a function execution record

type ExecutionStatus

type ExecutionStatus string

ExecutionStatus represents the status of a function execution

const (
	ExecutionStatusPending ExecutionStatus = "pending"
	ExecutionStatusSuccess ExecutionStatus = "success"
	ExecutionStatusError   ExecutionStatus = "error"
)

type ExecutionTrigger

type ExecutionTrigger string

ExecutionTrigger represents how an execution was triggered

const (
	ExecutionTriggerHTTP ExecutionTrigger = "http"
	ExecutionTriggerCron ExecutionTrigger = "cron"
)

type Function

type Function struct {
	ID            string            `json:"id"`
	Name          string            `json:"name"`
	Description   *string           `json:"description,omitempty"`
	EnvVars       map[string]string `json:"env_vars"`
	Disabled      bool              `json:"disabled"`
	RetentionDays *int              `json:"retention_days,omitempty"`
	CronSchedule  *string           `json:"cron_schedule,omitempty"`
	CronStatus    *string           `json:"cron_status,omitempty"`
	SaveResponse  bool              `json:"save_response"`
	CreatedAt     int64             `json:"created_at"`
	UpdatedAt     int64             `json:"updated_at"`
	ScopedData    map[string]string `json:"scoped_data"`
	GlobalData    map[string]string `json:"global_data"`
}

Function represents a serverless function

type FunctionVersion

type FunctionVersion struct {
	ID         string  `json:"id"`
	FunctionID string  `json:"function_id"`
	Version    int     `json:"version"`
	Code       string  `json:"code"`
	CreatedAt  int64   `json:"created_at"`
	CreatedBy  *string `json:"created_by,omitempty"`
	IsActive   bool    `json:"is_active"`
}

FunctionVersion represents a specific version of a function

type FunctionWithActiveVersion

type FunctionWithActiveVersion struct {
	Function
	ActiveVersion FunctionVersion `json:"active_version"`
}

FunctionWithActiveVersion includes the function and its active version

type LogLevel

type LogLevel string

LogLevel represents the severity level of a log entry

const (
	LogLevelDebug LogLevel = "debug"
	LogLevelInfo  LogLevel = "info"
	LogLevelWarn  LogLevel = "warn"
	LogLevelError LogLevel = "error"
)

type MemoryDB

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

MemoryDB is an in-memory implementation of the DB interface

func NewMemoryDB

func NewMemoryDB() *MemoryDB

NewMemoryDB creates a new in-memory database

func (*MemoryDB) ActivateVersion

func (db *MemoryDB) ActivateVersion(_ context.Context, versionID string) error

func (*MemoryDB) CreateAPIToken

func (db *MemoryDB) CreateAPIToken(_ context.Context, token APIToken) (APIToken, error)

func (*MemoryDB) CreateExecution

func (db *MemoryDB) CreateExecution(_ context.Context, exec Execution) (Execution, error)

func (*MemoryDB) CreateFunction

func (db *MemoryDB) CreateFunction(_ context.Context, fn Function) (Function, error)

func (*MemoryDB) CreateVersion

func (db *MemoryDB) CreateVersion(_ context.Context, functionID string, code string, createdBy *string) (FunctionVersion, error)

func (*MemoryDB) DeleteFunction

func (db *MemoryDB) DeleteFunction(_ context.Context, id string) error

func (*MemoryDB) DeleteOldExecutions

func (db *MemoryDB) DeleteOldExecutions(_ context.Context, beforeTimestamp int64) (int64, error)

func (*MemoryDB) DeleteVersion

func (db *MemoryDB) DeleteVersion(_ context.Context, versionID string) error

func (*MemoryDB) GetAPITokenByHash

func (db *MemoryDB) GetAPITokenByHash(_ context.Context, tokenHash string) (APIToken, error)

func (*MemoryDB) GetActiveVersion

func (db *MemoryDB) GetActiveVersion(_ context.Context, functionID string) (FunctionVersion, error)

func (*MemoryDB) GetExecution

func (db *MemoryDB) GetExecution(_ context.Context, executionID string) (Execution, error)

func (*MemoryDB) GetFunction

func (db *MemoryDB) GetFunction(_ context.Context, id string) (Function, error)

func (*MemoryDB) GetVersion

func (db *MemoryDB) GetVersion(_ context.Context, functionID string, version int) (FunctionVersion, error)

func (*MemoryDB) GetVersionByID

func (db *MemoryDB) GetVersionByID(_ context.Context, versionID string) (FunctionVersion, error)

func (*MemoryDB) ListAPITokens

func (db *MemoryDB) ListAPITokens(_ context.Context) ([]APIToken, error)

func (*MemoryDB) ListExecutions

func (db *MemoryDB) ListExecutions(_ context.Context, functionID string, params PaginationParams) ([]Execution, int64, error)

func (*MemoryDB) ListFunctions

func (db *MemoryDB) ListFunctions(_ context.Context, params PaginationParams) ([]FunctionWithActiveVersion, int64, error)

func (*MemoryDB) ListFunctionsWithActiveCron

func (db *MemoryDB) ListFunctionsWithActiveCron(_ context.Context) ([]Function, error)

func (*MemoryDB) ListVersions

func (db *MemoryDB) ListVersions(_ context.Context, functionID string, params PaginationParams) ([]FunctionVersion, int64, error)

func (*MemoryDB) Ping

func (db *MemoryDB) Ping(_ context.Context) error

func (*MemoryDB) RevokeAPIToken

func (db *MemoryDB) RevokeAPIToken(_ context.Context, id string) error

func (*MemoryDB) UpdateAPITokenLastUsed

func (db *MemoryDB) UpdateAPITokenLastUsed(_ context.Context, id string, timestamp int64) error

func (*MemoryDB) UpdateExecution

func (db *MemoryDB) UpdateExecution(_ context.Context, executionID string, status ExecutionStatus, durationMs *int64, errorMsg *string, responseJSON *string) error

func (*MemoryDB) UpdateFunction

func (db *MemoryDB) UpdateFunction(_ context.Context, id string, updates UpdateFunctionRequest) error

type PaginationInfo

type PaginationInfo struct {
	Total  int64 `json:"total"`  // Total number of items
	Limit  int   `json:"limit"`  // Items per page
	Offset int   `json:"offset"` // Items skipped
}

PaginationInfo contains pagination metadata

type PaginationParams

type PaginationParams struct {
	Limit  int // Number of items per page (default: 20, max: 100)
	Offset int // Number of items to skip (default: 0)
}

PaginationParams contains pagination parameters

func (PaginationParams) Normalize

func (p PaginationParams) Normalize() PaginationParams

Normalize applies defaults and constraints to pagination parameters

type SQLiteDB

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

SQLiteDB is an SQLite implementation of the DB interface

func NewSQLiteDB

func NewSQLiteDB(db *sql.DB) *SQLiteDB

NewSQLiteDB creates a new SQLite-backed API database

func (*SQLiteDB) ActivateVersion

func (db *SQLiteDB) ActivateVersion(ctx context.Context, versionID string) error

func (*SQLiteDB) CreateAPIToken

func (db *SQLiteDB) CreateAPIToken(ctx context.Context, token APIToken) (APIToken, error)

func (*SQLiteDB) CreateExecution

func (db *SQLiteDB) CreateExecution(ctx context.Context, exec Execution) (Execution, error)

func (*SQLiteDB) CreateFunction

func (db *SQLiteDB) CreateFunction(ctx context.Context, fn Function) (Function, error)

func (*SQLiteDB) CreateVersion

func (db *SQLiteDB) CreateVersion(ctx context.Context, functionID string, code string, createdBy *string) (FunctionVersion, error)

func (*SQLiteDB) DeleteFunction

func (db *SQLiteDB) DeleteFunction(ctx context.Context, id string) error

func (*SQLiteDB) DeleteOldExecutions

func (db *SQLiteDB) DeleteOldExecutions(ctx context.Context, beforeTimestamp int64) (int64, error)

func (*SQLiteDB) DeleteVersion

func (db *SQLiteDB) DeleteVersion(ctx context.Context, versionID string) error

func (*SQLiteDB) GetAPITokenByHash

func (db *SQLiteDB) GetAPITokenByHash(ctx context.Context, tokenHash string) (APIToken, error)

func (*SQLiteDB) GetActiveVersion

func (db *SQLiteDB) GetActiveVersion(ctx context.Context, functionID string) (FunctionVersion, error)

func (*SQLiteDB) GetExecution

func (db *SQLiteDB) GetExecution(ctx context.Context, executionID string) (Execution, error)

func (*SQLiteDB) GetFunction

func (db *SQLiteDB) GetFunction(ctx context.Context, id string) (Function, error)

func (*SQLiteDB) GetVersion

func (db *SQLiteDB) GetVersion(ctx context.Context, functionID string, version int) (FunctionVersion, error)

func (*SQLiteDB) GetVersionByID

func (db *SQLiteDB) GetVersionByID(ctx context.Context, versionID string) (FunctionVersion, error)

func (*SQLiteDB) ListAPITokens

func (db *SQLiteDB) ListAPITokens(ctx context.Context) ([]APIToken, error)

func (*SQLiteDB) ListExecutions

func (db *SQLiteDB) ListExecutions(ctx context.Context, functionID string, params PaginationParams) ([]Execution, int64, error)

func (*SQLiteDB) ListFunctions

func (db *SQLiteDB) ListFunctions(ctx context.Context, params PaginationParams) ([]FunctionWithActiveVersion, int64, error)

func (*SQLiteDB) ListFunctionsWithActiveCron

func (db *SQLiteDB) ListFunctionsWithActiveCron(ctx context.Context) ([]Function, error)

func (*SQLiteDB) ListVersions

func (db *SQLiteDB) ListVersions(ctx context.Context, functionID string, params PaginationParams) ([]FunctionVersion, int64, error)

func (*SQLiteDB) Ping

func (db *SQLiteDB) Ping(ctx context.Context) error

func (*SQLiteDB) RevokeAPIToken

func (db *SQLiteDB) RevokeAPIToken(ctx context.Context, id string) error

func (*SQLiteDB) UpdateAPITokenLastUsed

func (db *SQLiteDB) UpdateAPITokenLastUsed(ctx context.Context, id string, timestamp int64) error

func (*SQLiteDB) UpdateExecution

func (db *SQLiteDB) UpdateExecution(ctx context.Context, executionID string, status ExecutionStatus, durationMs *int64, errorMsg *string, responseJSON *string) error

func (*SQLiteDB) UpdateFunction

func (db *SQLiteDB) UpdateFunction(ctx context.Context, id string, updates UpdateFunctionRequest) error

type UpdateFunctionRequest

type UpdateFunctionRequest struct {
	Name          *string `json:"name,omitempty"`
	Description   *string `json:"description,omitempty"`
	Code          *string `json:"code,omitempty"`
	Disabled      *bool   `json:"disabled,omitempty"`
	RetentionDays *int    `json:"retention_days,omitempty"`
	CronSchedule  *string `json:"cron_schedule,omitempty"`
	CronStatus    *string `json:"cron_status,omitempty"`
	SaveResponse  *bool   `json:"save_response,omitempty"`
}

UpdateFunctionRequest is the request body for updating a function

Jump to

Keyboard shortcuts

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