sqlite

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package sqlite provides a SQLite-backed implementation of the persistence ports. The driver is modernc.org/sqlite (pure Go, no CGO) so chronos builds and cross-compiles without a C toolchain.

The package owns one Conn type that bundles the per-aggregate repositories (entity states, signals) on a shared *sql.DB. SQL goes through sqlc-generated code in the sqlcgen subpackage; this file is only responsible for opening the database, configuring PRAGMAs, and applying migrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bootstrap

func Bootstrap(db *sql.DB) error

Bootstrap applies the embedded migration to db. Reused by the libsql provider since the schema works unchanged on libSQL.

func TxFn

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

TxFn is exported so the libsql provider can reuse this exact implementation without duplicating it. Other providers that need the same database/sql semantics may also use it.

Types

type Conn

type Conn struct {
	DB *sql.DB

	EntityStates *EntityStateRepository
	Signals      *SignalRepository
	// contains filtered or unexported fields
}

Conn bundles the SQLite-backed repositories. Construct with Open.

func NewFromDB

func NewFromDB(db *sql.DB) *Conn

NewFromDB wires the SQLite repositories onto a pre-opened *sql.DB without applying PRAGMAs or migrations. The libsql provider uses this to reuse the SQLite repositories on a libsql-driver-backed connection (libSQL is wire-compatible at the SQL level).

The caller owns db and is responsible for closing it. Conn.Close will close it; in shared scenarios call only one of them.

func Open

func Open(path string) (*Conn, error)

Open connects to a SQLite database at path (or ":memory:") and applies migrations. Foreign keys, WAL journal, and a 5-second busy timeout are enabled via PRAGMAs encoded in the DSN.

func (*Conn) Close

func (c *Conn) Close() error

Close releases the underlying database handle.

type EntityStateRepository

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

EntityStateRepository persists entity states in SQLite. Features, Labels, and Meta are JSON-encoded TEXT to keep adapter-specific schema out of the engine's tables.

func (*EntityStateRepository) Count

func (r *EntityStateRepository) Count(ctx context.Context, adapterName string) (int64, error)

Count returns the number of states stored under adapterName.

func (*EntityStateRepository) DeleteOlderThan

func (r *EntityStateRepository) DeleteOlderThan(ctx context.Context, cutoff time.Time, adapterName string) error

DeleteOlderThan removes states observed before cutoff for adapterName.

func (*EntityStateRepository) Ingest

func (r *EntityStateRepository) Ingest(ctx context.Context, adapterName string, state chronos.EntityState) error

Ingest persists a single observation.

func (*EntityStateRepository) ListByEntity

func (r *EntityStateRepository) ListByEntity(ctx context.Context, entityID uuid.UUID) ([]chronos.EntityState, error)

ListByEntity returns all observations of an entity, most recent first.

func (*EntityStateRepository) ListByScope

func (r *EntityStateRepository) ListByScope(ctx context.Context, scopeID uuid.UUID) ([]chronos.EntityState, error)

ListByScope returns the scope's states, most recent first.

func (*EntityStateRepository) ListScopes

func (r *EntityStateRepository) ListScopes(ctx context.Context) ([]uuid.UUID, error)

ListScopes returns the distinct ScopeIDs that have at least one observation. Used by the in-process detection scheduler. The query is direct SQL rather than sqlc-generated to keep the surface minimal — there is exactly one query and it has no parameters.

func (*EntityStateRepository) Save

func (r *EntityStateRepository) Save(ctx context.Context, adapterName string, states []chronos.EntityState) error

Save persists a batch of observations transactionally.

type SignalRepository

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

SignalRepository persists signals and their evidence in SQLite.

Filtering uses dynamic SQL because sqlc cannot express optional predicates compactly. Inserts go through the sqlc-generated code so the same query pipeline carries them as the rest of the schema.

func (*SignalRepository) Count

func (r *SignalRepository) Count(ctx context.Context, filter ports.SignalFilter) (int64, error)

Count returns the number of signals matching filter.

func (*SignalRepository) Get

Get returns a single signal by ID, including its evidence.

func (*SignalRepository) List

List returns signals matching the filter, ordered detected-at desc then confidence desc.

func (*SignalRepository) Save

func (r *SignalRepository) Save(ctx context.Context, sig domain.Signal) error

Save writes a signal and its evidence atomically.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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