sqlite

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package sqlite provides the local SQLite database for the CLI. This is the primary data layer - PowerSync keeps it in sync with the server, but you query it directly like any SQLite database.

Package sqlite provides the local SQLite database for the CLI.

This is the primary data layer. PowerSync keeps it in sync with the server, but you query it directly like any SQLite database.

Code Generation

The schema types are generated from the PowerSync service. To regenerate:

doppler run -- go generate ./internal/sqlite

This fetches the current schema and sync rules from the PowerSync API and generates Go types for each synced table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetExtensionPath

func SetExtensionPath(path string)

SetExtensionPath configures the PowerSync extension to be loaded on every new database connection. This must be called before Open() to have effect. Typically called once at startup by the powersync package.

func WrapSQLiteError

func WrapSQLiteError(err error, op string) error

WrapSQLiteError wraps an error with SQLite-specific details if available. If the error is not a sqlite3.Error, it returns a generic wrapped error.

Types

type CompliancePolicies added in v1.16.0

type CompliancePolicies interface {
	ListPendingPoliciesByCategory(ctx context.Context, category domain.PolicyCategory, limit int64) ([]domain.CompliancePolicy, error)
}

CompliancePolicies provides type-safe access to compliance policies (PII, Secrets, PHI, Payment Data).

type Conversations

type Conversations interface {
	Count(ctx context.Context) (int64, error)
	Create(ctx context.Context, accountID domain.AccountID, workspaceID domain.WorkspaceID) (domain.ConversationID, error)
	UpdateTitle(ctx context.Context, id domain.ConversationID, title string) error
	List(ctx context.Context, accountID domain.AccountID) ([]gen.Conversation, error)
	Get(ctx context.Context, id domain.ConversationID) (gen.Conversation, error)
}

Conversations provides type-safe access to conversations.

type DB

type DB interface {
	// Domain entities
	Conversations() Conversations
	LogEventPolicies() LogEventPolicies
	LogEventPolicyStatuses() LogEventPolicyStatuses
	CompliancePolicies() CompliancePolicies
	LogEvents() LogEvents
	Messages() Messages
	Services() Services

	// Aggregated statuses
	DatadogAccountStatuses() DatadogAccountStatuses
	LogEventPolicyCategoryStatuses() LogEventPolicyCategoryStatuses
	LogEventStatuses() LogEventStatuses
	ServiceStatuses() ServiceStatuses

	// Sync
	PendingUploadCounts(ctx context.Context) (map[Table]int64, error)

	// Low-level
	Query(ctx context.Context, sql string, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) *sql.Row
	Exec(ctx context.Context, sql string, args ...any) (sql.Result, error)
	WithTx(ctx context.Context, fn func(tx *Tx) error) error
	Raw() *sql.DB     // Write pool — for PowerSync controller and direct writes
	ReadRaw() *sql.DB // Read pool — for query tool and direct reads
	Close() error
}

DB is the interface for application code. It provides type-safe access to domain data and raw query execution.

func Open

func Open(ctx context.Context, path string) (DB, error)

Open opens a SQLite database at the given path with separate read and write connection pools. The write pool enables WAL mode for concurrent read access during writes. The read pool enforces query_only = ON via the driver hook.

The database file and parent directories are created if they don't exist.

type DatadogAccountStatuses

type DatadogAccountStatuses interface {
	GetSummary(ctx context.Context) (domain.AccountSummary, error)
}

DatadogAccountStatuses provides access to Datadog account status data.

type LogEventPolicies

type LogEventPolicies interface {
	Count(ctx context.Context) (int64, error)
}

LogEventPolicies provides type-safe access to log event policies.

type LogEventPolicyCategoryStatuses added in v1.18.0

type LogEventPolicyCategoryStatuses interface {
	ListWasteCategoryStatuses(ctx context.Context) ([]domain.PolicyCategoryStatus, error)
	ListQualityCategoryStatuses(ctx context.Context) ([]domain.PolicyCategoryStatus, error)
	ListComplianceCategoryStatuses(ctx context.Context) ([]domain.PolicyCategoryStatus, error)
}

LogEventPolicyCategoryStatuses provides access to pre-computed per-category policy rollups.

type LogEventPolicyStatuses added in v1.18.0

type LogEventPolicyStatuses interface {
	GetPolicyCard(ctx context.Context, policyID string) (*domain.PolicyCard, error)
	ListTopPendingPoliciesByCategory(ctx context.Context, category domain.PolicyCategory, limit int64) ([]domain.WastePolicy, error)
}

LogEventPolicyStatuses provides type-safe access to pre-computed policy status data.

type LogEventStatuses added in v1.17.0

type LogEventStatuses interface {
	ListByService(ctx context.Context, serviceName string, limit int64) ([]domain.LogEventStatus, error)
}

LogEventStatuses provides access to per-log-event status data.

type LogEvents

type LogEvents interface {
	Count(ctx context.Context) (int64, error)
}

LogEvents provides type-safe access to log events.

type Messages

type Messages interface {
	Count(ctx context.Context) (int64, error)
	Get(ctx context.Context, id domain.MessageID) (*domain.Message, error)
	CreateUserMessage(ctx context.Context, accountID domain.AccountID, conversationID domain.ConversationID, text string) (domain.MessageID, error)
	CreateToolResultMessage(ctx context.Context, accountID domain.AccountID, conversationID domain.ConversationID, results []domain.ToolResult) (domain.MessageID, error)
	CreateAssistantMessage(ctx context.Context, accountID domain.AccountID, conversationID domain.ConversationID, model string) (domain.MessageID, error)
	UpdateContent(ctx context.Context, id domain.MessageID, content string) error
	UpdateMeta(ctx context.Context, id domain.MessageID, model, stopReason string) error
	Delete(ctx context.Context, id domain.MessageID) error
	List(ctx context.Context, conversationID domain.ConversationID) ([]domain.Message, error)
}

Messages provides type-safe access to messages.

type SQLiteError

type SQLiteError struct {
	Code         int    // Primary error code
	ExtendedCode int    // Extended error code with more detail
	Message      string // Error message from sqlite3_errmsg()
	Op           string // Operation that failed (e.g., "insert message")
}

SQLiteError wraps a sqlite3.Error with additional context. It provides structured access to error codes for debugging.

func (*SQLiteError) Error

func (e *SQLiteError) Error() string

func (*SQLiteError) Unwrap

func (e *SQLiteError) Unwrap() error

Unwrap returns nil since this is the root error.

type ServiceStatuses

type ServiceStatuses interface {
	ListAllServiceStatuses(ctx context.Context) ([]domain.ServiceStatus, error)
	ListEnabledServiceStatuses(ctx context.Context, limit int64) ([]domain.ServiceStatus, error)
}

ServiceStatuses provides access to per-service status data.

type Services

type Services interface {
	Count(ctx context.Context) (int64, error)
	Get(ctx context.Context, id domain.ServiceID) (gen.Service, error)
	SetEnabled(ctx context.Context, id domain.ServiceID, enabled bool) error
}

Services provides type-safe access to services.

type Storage

type Storage interface {
	DatabasePath(accountID string) (string, error)
	ClearDatabase(accountID string) error
	Clear() error
}

Storage provides database file operations.

type StorageService

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

StorageService implements Storage.

func NewStorageService

func NewStorageService(cfg *config.Config) *StorageService

NewStorageService creates a new storage service.

func (*StorageService) Clear

func (s *StorageService) Clear() error

Clear removes all SQLite database files.

func (*StorageService) ClearDatabase

func (s *StorageService) ClearDatabase(accountID string) error

ClearDatabase removes the SQLite database file for a specific account.

func (*StorageService) DatabasePath

func (s *StorageService) DatabasePath(accountID string) (string, error)

DatabasePath returns the path to the SQLite database for a specific account.

type Table

type Table string

Table represents a watched table.

const (
	TableConversations    Table = "conversations"
	TableLogEventPolicies Table = "log_event_policies"
	TableLogEvents        Table = "log_events"
	TableMessages         Table = "messages"
	TableServices         Table = "services"
)

Table names matching the schema.

type Tx

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

Tx wraps a SQL transaction with convenience methods.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec

func (t *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a statement within the transaction.

func (*Tx) Query

func (t *Tx) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query within the transaction.

func (*Tx) QueryRow

func (t *Tx) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

QueryRow executes a query that returns at most one row within the transaction.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback aborts the transaction.

Directories

Path Synopsis
Package main generates SQLite schema types from PowerSync.
Package main generates SQLite schema types from PowerSync.
Package sqlitetest provides test doubles for the sqlite package.
Package sqlitetest provides test doubles for the sqlite package.

Jump to

Keyboard shortcuts

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