jsonstore

package
v0.260313.0-preview Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsonstore provides a generic JSON file-based key-value storage. It is designed for simple persistence needs where a full database is overkill.

Features:

  • Thread-safe operations with read-write mutexes
  • Atomic file writes (write to temp, then rename)
  • In-memory caching for fast reads
  • Dirty flag to only write when data changes
  • Generic model support through type parameters

Example usage:

type MyModel struct {
    Name  string `json:"name"`
    Count int    `json:"count"`
}

store, err := jsonstore.New[MyModel]("data.json")
if err != nil {
    log.Fatal(err)
}
defer store.Close()

// Set a value
err = store.Set("key1", &MyModel{Name: "test", Count: 42})

// Get a value
model, ok := store.Get("key1")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model interface {
	any // Allows any type as a model
}

Model is the interface that all models must implement. Use a pointer to your model type as the type parameter.

type Store

type Store[T any] struct {
	// contains filtered or unexported fields
}

Store manages JSON file-based storage with generic type support.

func New

func New[T any](filePath string, opts ...StoreOption[T]) (*Store[T], error)

New creates a new JSON-based store for the given model type. If filePath doesn't exist, an empty store is created. If filePath exists, existing data is loaded.

The model type T must be a pointer type (e.g., *MyModel) to ensure consistent behavior when values are nil.

func (*Store[T]) Clear

func (s *Store[T]) Clear() error

Clear removes all items from the store.

func (*Store[T]) Close

func (s *Store[T]) Close() error

Close ensures data is persisted before closing.

func (*Store[T]) Count

func (s *Store[T]) Count() int

Count returns the number of items in the store.

func (*Store[T]) Delete

func (s *Store[T]) Delete(key string) bool

Delete removes a value from the store. Returns true if the key was found and removed, false otherwise.

func (*Store[T]) ForceSave

func (s *Store[T]) ForceSave() error

ForceSave saves the data even if not marked as dirty.

func (*Store[T]) Get

func (s *Store[T]) Get(key string) *T

Get retrieves a value by key. Returns the value and true if found, nil and false otherwise.

func (*Store[T]) GetUpdated

func (s *Store[T]) GetUpdated() time.Time

GetUpdated returns the last update time.

func (*Store[T]) GetVersion

func (s *Store[T]) GetVersion() int

GetVersion returns the store version.

func (*Store[T]) Has

func (s *Store[T]) Has(key string) bool

Has checks if a key exists in the store.

func (*Store[T]) Keys

func (s *Store[T]) Keys() []string

Keys returns all keys in the store.

func (*Store[T]) List

func (s *Store[T]) List() map[string]*T

List returns all values in the store as a map. The returned map is a shallow copy; modifications won't affect the store.

func (*Store[T]) Set

func (s *Store[T]) Set(key string, value *T) error

Set stores a value for the given key. The value is stored as a pointer to enable nil values.

func (*Store[T]) Update

func (s *Store[T]) Update(key string, fn func(*T) *T) error

Update updates a value using the provided function. The function receives the current value (or nil if not found) and returns the new value. If the function returns nil, the key is deleted.

type StoreData

type StoreData[T any] struct {
	Version int           `json:"version"`
	Items   map[string]*T `json:"items"` // Key: string, Value: pointer to model
	Updated time.Time     `json:"updated"`
}

StoreData represents the JSON file structure. Version allows for future schema migrations.

type StoreOption

type StoreOption[T any] func(*Store[T])

StoreOption is a function that configures a Store.

func WithVersion

func WithVersion[T any](version int) StoreOption[T]

WithVersion sets the version for the store data.

Jump to

Keyboard shortcuts

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