Documentation
¶
Overview ¶
Package database provides a source.Reader implementation that reads scripts from any SQL database via the standard database/sql package.
Supported databases include MySQL, PostgreSQL, SQLite, SQL Server, and any driver registered with database/sql.
Construction:
src, err := database.New(ctx,
database.WithDriver("mysql"),
database.WithDSN("user:pass@tcp(localhost:3306)/scripts"),
database.WithTable("scripts"),
database.WithKeyColumn("name"),
database.WithValueColumn("content"),
database.WithChecksumColumn("updated_at"),
)
Hot-reload detection uses checksum polling: the Watcher periodically re-queries the checksum column and compares it with the value recorded at the last Load.
Index ¶
- Variables
- func IsNotFound(err error) bool
- type Option
- func WithChecksumColumn(col string) Option
- func WithConnMaxLifetime(d time.Duration) Option
- func WithDB(db *sql.DB) Option
- func WithDSN(dsn string) Option
- func WithDriver(driver string) Option
- func WithKeyColumn(col string) Option
- func WithMaxIdleConns(n int) Option
- func WithMaxOpenConns(n int) Option
- func WithPollInterval(d time.Duration) Option
- func WithPrefix(prefix string) Option
- func WithQuery(query string) Option
- func WithTable(table string) Option
- func WithValueColumn(col string) Option
- type Reader
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("database source: key not found")
ErrNotFound is returned (wrapped) by Load when the requested key does not exist in the database. Detect with errors.Is(err, ErrNotFound) or the convenience helper IsNotFound.
Functions ¶
func IsNotFound ¶
IsNotFound reports whether err represents a "key not found" response from the database. Equivalent to errors.Is(err, ErrNotFound).
Types ¶
type Option ¶
type Option func(*configOptions)
Option configures a Reader. Pass to New.
func WithChecksumColumn ¶
WithChecksumColumn sets the column used for change detection (default "updated_at"). This should be a column that changes whenever the row is modified (e.g., updated_at, version, checksum, etag).
func WithConnMaxLifetime ¶
WithConnMaxLifetime sets the maximum lifetime of a connection.
func WithDB ¶
WithDB injects a pre-existing *sql.DB instance. When set, WithDriver and WithDSN are ignored. The Reader will NOT close the DB on Close().
func WithDSN ¶
WithDSN sets the data source name for the database connection. Must be used together with WithDriver unless WithDB is used.
func WithDriver ¶
WithDriver sets the database driver name (e.g. "mysql", "postgres", "sqlite3"). Must be used together with WithDSN unless WithDB is used.
func WithKeyColumn ¶
WithKeyColumn sets the column name that identifies the script (default "name").
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections.
func WithMaxOpenConns ¶
WithMaxOpenConns sets the maximum number of open connections.
func WithPollInterval ¶
WithPollInterval sets the polling interval for Watch (default 10s).
func WithPrefix ¶
WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against the database. Useful when all scripts share a common namespace (e.g. WithPrefix("scripts/lua/")).
Leading slashes are stripped; no other normalization is applied.
func WithQuery ¶
WithQuery sets a custom SQL query that overrides the auto-generated one. The query must return exactly two columns: value first, checksum second. The key is passed as the first positional parameter (? or $1).
Example for PostgreSQL:
WithQuery("SELECT content, updated_at FROM scripts WHERE name = $1")
Example for MySQL / SQLite:
WithQuery("SELECT content, updated_at FROM scripts WHERE name = ?")
func WithValueColumn ¶
WithValueColumn sets the column name that stores the script content (default "content").
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads scripts from a SQL database.
All exported methods are safe for concurrent use. Reader implements the source.ReadWatcher interface.
func New ¶
New creates a database-backed Reader.
At minimum, either:
- WithDriver + WithDSN (the Reader opens and owns the connection)
- WithDB (the Reader uses a pre-existing *sql.DB and will not close it)
must be supplied. All other settings are optional with sensible defaults.
func (*Reader) Load ¶
Load fetches the script value from the database and returns it as a string. Context cancellation propagates to the underlying query.
A missing row (sql.ErrNoRows) is reported as a wrapped ErrNotFound. Other errors are wrapped with the key for easier debugging.
func (*Reader) Watch ¶
Watch returns a channel that signals when the value identified by `key` changes. It polls the checksum column every pollInterval and sends a signal on the channel when the checksum differs from the one recorded during the last Load.
The returned channel is closed when the context is cancelled. Callers should re-Load the script after receiving from the channel.