5 Home
Virgil edited this page 2026-02-19 16:56:06 +00:00

go-store

forge.lthn.ai/core/go-store -- Group-namespaced key-value store backed by SQLite.

A lightweight wrapper around SQLite providing a simple key-value interface with group namespacing, UPSERT semantics, and Go template rendering from stored values.

Installation

go get forge.lthn.ai/core/go-store@latest

Dependencies: modernc.org/sqlite (pure-Go SQLite, no CGO required)

Quick Start

package main

import (
    "fmt"
    "log"

    "forge.lthn.ai/core/go-store"
)

func main() {
    s, err := store.New("app.db")
    if err != nil {
        log.Fatal(err)
    }
    defer s.Close()

    // Store values in a group
    _ = s.Set("config", "theme", "dark")
    _ = s.Set("config", "lang", "en-GB")

    // Retrieve
    theme, err := s.Get("config", "theme")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(theme) // "dark"

    // List all keys in a group
    all, _ := s.GetAll("config")
    for k, v := range all {
        fmt.Printf("  %s = %s\n", k, v)
    }

    // Render a template from stored values
    output, _ := s.Render("Theme: {{.theme}}, Language: {{.lang}}", "config")
    fmt.Println(output) // "Theme: dark, Language: en-GB"
}

API Summary

Method Description
New(dbPath) Open or create a store at the given path
Close() Close the underlying database
Get(group, key) Retrieve a value (returns ErrNotFound if missing)
Set(group, key, value) Store a value (upsert semantics)
Delete(group, key) Remove a single key
DeleteGroup(group) Remove all keys in a group
Count(group) Count keys in a group
GetAll(group) Retrieve all key-value pairs in a group
Render(tmpl, group) Render a Go template using group values

Pages

Licence

EUPL-1.2