Documentation
¶
Overview ¶
Package enumdb loads enumeration sets from relational database tables into thread-safe enum caches.
It solves the problem of bootstrapping application enum metadata from database reference tables and building in-memory ID-to-name and name-to-ID lookup maps at service startup.
This package is designed for tables where each row represents a single enum value with values that can be scanned into (id int, name string). In practice, this usually corresponds to a numeric primary key column named "id" and a unique string column named "name".
Top features:
- load multiple enum tables in one call by passing a map of table name to SQL query - build github.com/tecnickcom/nurago/pkg/enumcache.EnumCache instances per table - provide fast runtime lookups in both directions through enumcache APIs - wrap query, scan, and row-iteration failures with contextual errors for easier debugging - close rows safely, joining close errors with main execution errors
Benefits:
- reduce enum duplication across database and application code
- keep enum lookup logic centralized and predictable
- simplify startup loading of reference data such as feature flags, states, and types
Example of a MySQL database table that can be used with this package:
CREATE TABLE IF NOT EXISTS `example` ( `id` SMALLINT UNSIGNED NOT NULL, `name` VARCHAR(50) NOT NULL, `disabled` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC), UNIQUE INDEX `name_UNIQUE` (`name` ASC)) ENGINE = InnoDB COMMENT = 'Example enumeration table';
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EnumDB ¶
EnumDB maps each enumeration table name with the corresponding enumeration cache.
func New ¶
New loads enum caches for all tables listed in queries.
Each query should return rows compatible with (id int, name string). The result map is keyed by table name for direct access from application code.
Loading is all-or-nothing: the first table that fails aborts the whole call and returns a nil map with an error wrapping the underlying failure. When several tables would fail, which one surfaces is not deterministic because queries is a map.
type EnumTableQuery ¶
EnumTableQuery maps each enumeration table name with the SQL query string used to read the data.