Code Monkey home page Code Monkey logo

settingstore's Introduction

Settings Store Open in Gitpod

Tests Status Go Report Card PkgGoDev

Saves settings in an SQL database.

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.

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 = settingstore.NewStore(settingstore.WithDb(databaseInstance), settingstore.WithTableName("settings"), entitystore.WithAutoMigrate(true))


// as multiple lines
settingStore = settingstore.NewStore(settingstore.WithDb(databaseInstance), settingstore.WithTableName("settings"))
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 ...StoreOption) *Store - creates a new setting store

  • WithAutoMigrate(automigrateEnabled bool) StoreOption - a store option. sets whether database migration will run on setup

  • WithDb(db *sql.DB) StoreOption - a store option (required). sets the DB to be used by the store

  • WithTableName(settingsTableName string) StoreOption - a store option (required). sets the table name for the setting store in the DB

  • AutoMigrate() 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)

  • SqlCreateTable() string - SQL string for creating the tables (you may use this string if you want to set your own migrations)

Setting Methods

  • Delete() bool - deletes the entity
  • FindByKey(key string) *Setting - finds a Setting by key
  • Get(key string, valueDefault string) string - gets a value from key-value setting pair
  • GetJSON(key string, valueDefault interface{}) interface{} - gets a value as JSON from key-value setting pair
  • Keys() ([]string, error) - gets all keys sorted alphabetically (useful if you want to list these in admin panel)
  • Remove(key string) error - removes a setting from store
  • Set(key string, value string) (bool, error) - sets new key value pair
  • SetJSON(key string, value interface{}) (bool, error) - sets new key JSON value pair

settingstore's People

Contributors

lesichkovm avatar vmistry-repo avatar

Stargazers

Sinevia avatar  avatar

Watchers

 avatar  avatar

Forkers

vmistry-repo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.