settingstore

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: AGPL-3.0 Imports: 20 Imported by: 0

README ΒΆ

Settings Store

Tests Status Go Report Card PkgGoDev

Saves settings in an SQL database.

🌏 Open in the Cloud

Click any of the buttons below to start a new development environment to demo or contribute to the codebase without having to install anything on your machine:

Open in VS Code Open in Glitch Open in GitHub Codespaces Edit in Codesandbox Open in StackBlitz Open in Repl.it Open in Codeanywhere Open in Gitpod

Description

Every application needs to preserve settings between multiple restarts. This package helps save the setting represented as key-value pairs in an SQL database.

License

This project is dual-licensed under the following terms:

  • For non-commercial use, you may choose either the GNU Affero General Public License v3.0 (AGPLv3) or a separate commercial license (see below). You can find a copy of the AGPLv3 at: https://www.gnu.org/licenses/agpl-3.0.txt

  • For commercial use, a separate commercial license is required. Commercial licenses are available for various use cases. Please contact me via my contact page to obtain a commercial license.

Features

  • Saves settings data as key-value pairs
  • Supports SQLite, MySQL and Postgres
  • Uses sql.DB directly
  • Automigration

Installation

go get -u github.com/gouniverse/settingstore

Setup

// as one line
settingStore, err = settingstore.NewStore(settingstore.NewStoreOptions{
	DB: databaseInstance,
	SettingTableName: "settings",
	AutomigrateEnabled: true,
})

if err != nil {
	panic(err)
}

// as multiple lines
settingStore, err = settingstore.NewStore(settingstore.NewStoreOptions{
	DB: databaseInstance,
	SettingTableName: "settings",
})

if err != nil {
	panic(err)
}

settingStore.AutoMigrate()

Usage

  1. Create a new key value setting pair
settingsStore.Set("app.name", "My Web App")
settingsStore.Set("app.url", "http://localhost")
settingsStore.Set("server.ip", "127.0.0.1")
settingsStore.Set("server.port", "80")
  1. Retrieve a setting value (or default value if not exists)
appName = settingsStore.Get("app.name", "Default Name")
appUrl = settingsStore.Get("app.url", "")
serverIp = settingsStore.Get("server.ip", "")
serverPort = settingsStore.Get("server.port", "")
  1. Check if required setting is setup
if serverIp == "" {
    log.Panic("server ip not setup")
}

Methods

These methods may be subject to change as still in development

Store Methods
  • NewStore(opts NewStoreOptions) (*store, error) - creates a new setting store
  • AutoMigrate(ctx context.Context) error - auto migrate (create the tables in the database) the settings store tables
  • DriverName(db *sql.DB) string - the name of the driver used for SQL strings (you may use this if you need to debug)
  • SettingCount(ctx context.Context, query SettingQueryInterface) (int64, error) - counts the number of settings
  • SettingCreate(ctx context.Context, setting SettingInterface) error - creates a new setting
  • SettingDelete(ctx context.Context, setting SettingInterface) error - deletes a setting
  • SettingDeleteByID(ctx context.Context, settingID string) error - deletes a setting by ID
  • SettingDeleteByKey(ctx context.Context, settingKey string) error - deletes a setting by key
  • SettingFindByID(ctx context.Context, settingID string) (SettingInterface, error) - finds a setting by ID
  • SettingList(ctx context.Context, query SettingQueryInterface) ([]SettingInterface, error) - lists settings
  • SettingSoftDelete(ctx context.Context, setting SettingInterface) error - soft deletes a setting
  • SettingSoftDeleteByID(ctx context.Context, settingID string) error - soft deletes a setting by ID
  • SettingUpdate(ctx context.Context, setting SettingInterface) error - updates a setting
Shortcut Methods
  • Get(ctx context.Context, key string, valueDefault string) (string, error) - gets a value from key-value setting pair

  • Set(ctx context.Context, key string, value string, seconds int64) error - sets new key value pair

  • GetAny(ctx context.Context, key string, valueDefault interface{}) (interface{}, error) - gets a value from key-value setting pair

  • GetJSON(key string, valueDefault interface{}) (interface{}, error) - gets a value as JSON from key-value setting pair

  • SetJSON(ctx context.Context, key string, value interface{}, seconds int64) error - sets new key JSON value pair

  • GetMap(ctx context.Context, key string, valueDefault map[string]any) (map[string]any, error) - gets a value as JSON from key-value setting pair

  • MergeMap(ctx context.Context, key string, mergeMap map[string]any, seconds int64) error - merges a map with an existing map

  • Has(ctx context.Context, settingKey string) (bool, error) - checks if a setting exists

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	COLUMN_ID              = "id"
	COLUMN_SETTING_KEY     = "setting_key"
	COLUMN_SETTING_VALUE   = "setting_value"
	COLUMN_CREATED_AT      = "created_at"
	COLUMN_UPDATED_AT      = "updated_at"
	COLUMN_SOFT_DELETED_AT = "soft_deleted_at"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

func NewStore ΒΆ

func NewStore(opts NewStoreOptions) (*store, error)

NewStore creates a new setting store

Types ΒΆ

type NewStoreOptions ΒΆ

type NewStoreOptions struct {
	SettingTableName   string
	DB                 *sql.DB
	DbDriverName       string
	AutomigrateEnabled bool
	DebugEnabled       bool
	SqlLogger          *slog.Logger
}

NewStoreOptions define the options for creating a new setting store

type Setting ΒΆ

type Setting struct {
	dataobject.DataObject
}

Setting type

func (*Setting) GetCreatedAt ΒΆ

func (setting *Setting) GetCreatedAt() string

func (*Setting) GetCreatedAtCarbon ΒΆ

func (setting *Setting) GetCreatedAtCarbon() *carbon.Carbon

func (*Setting) GetID ΒΆ

func (setting *Setting) GetID() string

func (*Setting) GetKey ΒΆ

func (setting *Setting) GetKey() string

func (*Setting) GetSoftDeletedAt ΒΆ

func (setting *Setting) GetSoftDeletedAt() string

func (*Setting) GetSoftDeletedAtCarbon ΒΆ

func (setting *Setting) GetSoftDeletedAtCarbon() *carbon.Carbon

func (*Setting) GetUpdatedAt ΒΆ

func (setting *Setting) GetUpdatedAt() string

func (*Setting) GetUpdatedAtCarbon ΒΆ

func (setting *Setting) GetUpdatedAtCarbon() *carbon.Carbon

func (*Setting) GetValue ΒΆ

func (setting *Setting) GetValue() string

func (*Setting) IsSoftDeleted ΒΆ

func (o *Setting) IsSoftDeleted() bool

func (*Setting) SetCreatedAt ΒΆ

func (setting *Setting) SetCreatedAt(createdAt string) SettingInterface

func (*Setting) SetID ΒΆ

func (setting *Setting) SetID(id string) SettingInterface

func (*Setting) SetKey ΒΆ

func (setting *Setting) SetKey(key string) SettingInterface

func (*Setting) SetSoftDeletedAt ΒΆ

func (setting *Setting) SetSoftDeletedAt(deletedAt string) SettingInterface

func (*Setting) SetUpdatedAt ΒΆ

func (setting *Setting) SetUpdatedAt(updatedAt string) SettingInterface

func (*Setting) SetValue ΒΆ

func (setting *Setting) SetValue(value string) SettingInterface

type SettingInterface ΒΆ

type SettingInterface interface {
	Data() map[string]string
	DataChanged() map[string]string
	MarkAsNotDirty()

	IsSoftDeleted() bool

	GetCreatedAt() string
	GetCreatedAtCarbon() *carbon.Carbon
	SetCreatedAt(createdAt string) SettingInterface

	GetID() string
	SetID(id string) SettingInterface

	GetKey() string
	SetKey(key string) SettingInterface

	GetSoftDeletedAt() string
	GetSoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(deletedAt string) SettingInterface

	GetUpdatedAt() string
	GetUpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) SettingInterface

	GetValue() string
	SetValue(value string) SettingInterface
}

func NewSetting ΒΆ

func NewSetting() SettingInterface

func NewSettingFromExistingData ΒΆ

func NewSettingFromExistingData(data map[string]string) SettingInterface

type SettingQueryInterface ΒΆ

type SettingQueryInterface interface {
	Validate() error

	IsCountOnly() bool

	Columns() []string
	SetColumns(columns []string) SettingQueryInterface

	HasCreatedAtGte() bool
	CreatedAtGte() string
	SetCreatedAtGte(createdAtGte string) SettingQueryInterface

	HasCreatedAtLte() bool
	CreatedAtLte() string
	SetCreatedAtLte(createdAtLte string) SettingQueryInterface

	HasID() bool
	ID() string
	SetID(id string) SettingQueryInterface

	HasIDIn() bool
	IDIn() []string
	SetIDIn(idIn []string) SettingQueryInterface

	HasKey() bool
	Key() string
	SetKey(key string) SettingQueryInterface

	HasOffset() bool
	Offset() int
	SetOffset(offset int) SettingQueryInterface

	HasLimit() bool
	Limit() int
	SetLimit(limit int) SettingQueryInterface

	HasSortOrder() bool
	SortOrder() string
	SetSortOrder(sortOrder string) SettingQueryInterface

	HasOrderBy() bool
	OrderBy() string
	SetOrderBy(orderBy string) SettingQueryInterface

	HasCountOnly() bool
	SetCountOnly(countOnly bool) SettingQueryInterface

	HasSoftDeletedIncluded() bool
	SoftDeletedIncluded() bool
	SetSoftDeletedIncluded(withSoftDeleted bool) SettingQueryInterface
}

func NewSettingQuery ΒΆ

func NewSettingQuery() SettingQueryInterface

NewSettingQuery creates a new setting query

func SettingQuery ΒΆ

func SettingQuery() SettingQueryInterface

SettingQuery is a shortcut version of NewSettingQuery to create a new query

type StoreInterface ΒΆ

type StoreInterface interface {
	// AutoMigrate creates the settings table if it does not exist
	//
	// Parameters:
	// - ctx: the context
	//
	// Returns:
	// - error - nil if no error, error otherwise
	AutoMigrate(ctx context.Context) error

	// EnableDebug - enables the debug option
	//
	// # If enabled will log the SQL statements to the provided logger
	//
	// Parameters:
	//   - debug: true to enable, false otherwise
	//
	// Returns:
	//   - void
	EnableDebug(debug bool)

	// SettingCount counts the settings based on the provided query.
	//
	// Parameters:
	// - ctx: the context
	// - query: the query
	//
	// Returns:
	// - int64: the count
	// - error: nil if no error, error otherwise
	SettingCount(ctx context.Context, query SettingQueryInterface) (int64, error)

	// SettingCreate creates a new setting
	//
	// Parameters:
	// - ctx: the context
	// - setting: the setting
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingCreate(ctx context.Context, setting SettingInterface) error

	// SettingDelete deletes a setting
	//
	// Parameters:
	// - ctx: the context
	// - setting: the setting
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingDelete(ctx context.Context, setting SettingInterface) error

	// SettingDeleteByID deletes a setting by id
	//
	// Parameters:
	// - ctx: the context
	// - id: the id of the setting to delete
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingDeleteByID(ctx context.Context, settingID string) error

	// SettingFindByID finds a setting by id
	//
	// Parameters:
	// - ctx: the context
	// - id: the id of the setting to find
	//
	// Returns:
	// - SettingInterface: the setting
	// - error: nil if no error, error otherwise
	SettingFindByID(ctx context.Context, settingID string) (SettingInterface, error)

	// SettingFindByKey finds a setting by key
	//
	// Parameters:
	// - ctx: the context
	// - key: the key of the setting to find
	//
	// Returns:
	// - SettingInterface: the setting
	// - error: nil if no error, error otherwise
	SettingFindByKey(ctx context.Context, settingKey string) (SettingInterface, error)

	// SettingList retrieves a list of settings
	//
	// Parameters:
	// - ctx: the context
	// - query: the query
	//
	// Returns:
	// - []SettingInterface: the list of settings
	// - error: nil if no error, error otherwise
	SettingList(ctx context.Context, query SettingQueryInterface) ([]SettingInterface, error)

	// SettingSoftDelete soft deletes a setting
	//
	// Parameters:
	// - ctx: the context
	// - setting: the setting
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingSoftDelete(ctx context.Context, setting SettingInterface) error

	// SettingSoftDeleteByID soft deletes a setting by id
	//
	// Parameters:
	// - ctx: the context
	// - id: the id of the setting to soft delete
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingSoftDeleteByID(ctx context.Context, settingID string) error

	// SettingUpdate updates a setting
	//
	// Parameters:
	// - ctx: the context
	// - setting: the setting
	//
	// Returns:
	// - error: nil if no error, error otherwise
	SettingUpdate(ctx context.Context, setting SettingInterface) error

	// Delete is a shortcut method to delete a value by key
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to delete
	//
	// Returns:
	// - error - nil if no error, error otherwise
	Delete(ctx context.Context, settingKey string) error

	// Get is a shortcut method to get a value by key, or a default, if not found
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to get
	// - valueDefault: the default value to return if the setting is not found
	//
	// Returns:
	// - string - the value of the setting, or the default value if not found
	// - error - nil if no error, error otherwise
	Get(ctx context.Context, settingKey string, valueDefault string) (string, error)

	// GetAny is a shortcut method to get a value by key as an interface, or a default if not found
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to get
	// - valueDefault: the default value to return if the setting is not found
	//
	// Returns:
	// - interface{}, error
	GetAny(ctx context.Context, key string, valueDefault any) (any, error)

	// GetMap is a shortcut method to get a value by key as a map, or a default if not found
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to get
	// - valueDefault: the default value to return if the setting is not found
	//
	// Returns:
	// - map[string]any - the value of the setting, or the default value if not found
	// - error - nil if no error, error otherwise
	GetMap(ctx context.Context, key string, valueDefault map[string]any) (map[string]any, error)

	// Has is a shortcut method to check if a setting exists by key
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to check
	//
	// Returns:
	// - bool - true if the setting exists, false otherwise
	// - error - nil if no error, error otherwise
	Has(ctx context.Context, settingKey string) (bool, error)

	// MergeMap is a shortcut method to merge a map with an existing map
	//
	// Parameters:
	// - ctx: the context
	// - key: the key of the setting to merge
	// - mergeMap: the map to merge with the existing map
	//
	// Returns:
	// - error - nil if no error, error otherwise
	MergeMap(ctx context.Context, key string, mergeMap map[string]any) error

	// Set is a shortcut method to save a value by key, use Get to extract
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to save
	// - value: the value to save
	//
	// Returns:
	// - error - nil if no error, error otherwise
	Set(ctx context.Context, settingKey string, value string) error

	// SetAny is a shortcut method to save any value by key, use GetAny to extract
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to save
	// - value: the value to save
	//
	// Returns:
	// - error - nil if no error, error otherwise
	SetAny(ctx context.Context, key string, value interface{}, seconds int64) error

	// SetMap is a shortcut method to save a map by key, use GetMap to extract
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to save
	// - value: the value to save
	//
	// Returns:
	// - error - nil if no error, error otherwise
	SetMap(ctx context.Context, key string, value map[string]any) error

	// SettingDeleteByKey deletes a setting by id
	//
	// Parameters:
	// - ctx: the context
	// - settingKey: the key of the setting to delete
	//
	// Returns:
	// - error - nil if no error, error otherwise
	SettingDeleteByKey(ctx context.Context, settingKey string) error
}

StoreInterface defines the interface for a setting store.

Jump to

Keyboard shortcuts

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