jsondb

package module
v0.0.0-...-b9a2ca4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

JSONDB

JSONDB is a simple and very accessible database based on JSON files. Its niche is small projects that interact with the database exclusively through a single instance (simultaneous write access by multiple instances will lead to loss of data integrity). Basically, it is an in-memory key-value store that stores data locally in JSON files. If data is changed (in memory), these changes are also made in the file system. The big advantage in contrast to e.g. SQLite databases is that the data is available in plain text and thus extremely accessible for other programs. On the other hand, any logic for accessing the databases is missing or must be implemented by the user himself.

Pros

  • JSONDB doesn't care what data you put in it (string, int, struct, etc).
  • All data is always in memory, resulting in very short access times when reading.
  • All data is stored as simple JSON files in the file system

Cons

  • To avoid loss of data integrity, only one program instance should access the database at a time
  • As databases grow, write times increase as well, since the entire database is written to the file system each time
  • In addition, (too) large databases can have a negative impact on system performance, since memory consumption naturally grows in parallel with the database size

Usage

import (
	"git.0x0001f346.de/andreas/jsondb"
)

type Car struct {
    Manufacturer string
    Model string
    Horsepower int
    WeightInKG int
}

jsondb.SetLocationOfCluster("/mnt/storage/databases")    // optional, default is the path to the executable 
jsondb.SetNameOfCluster("iLikeJSONFiles")                // optional, default is "jsondb"
jsondb.LoadCluster()
jsondb.CreateDatabase("Cars")
jsondb.SetKeyInDatabase(
    "Cars",
    "My dream car",
    Car{
		Manufacturer: "Toyota",
		Model: "GR86",
		Horsepower: 234,
		WeightInKG: 1271,
	},
)

myDreamCar, _ := jsondb.GetKeyFromDatabase("Cars", "My dream car")    // returns (interface{}, err)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDatabase

func CreateDatabase(nameOfDatabase string) error

CreateDatabase creates a database or returns nil if it already exists

func DeleteDatabase

func DeleteDatabase(nameOfDatabase string) error

DeleteDatabase deletes a database

func DeleteKeyFromDatabase

func DeleteKeyFromDatabase(nameOfDatabase string, key string) error

DeleteKeyFromDatabase deletes a key in a database and write it to file

func GetKeyFromDatabase

func GetKeyFromDatabase(nameOfDatabase string, key string) (interface{}, error)

GetKeyFromDatabase returns a key from a database if both exist

func GetLocationOfCluster

func GetLocationOfCluster() string

GetLocationOfCluster returns locationOfCluster

func GetNameOfCluster

func GetNameOfCluster() string

GetNameOfCluster returns nameOfCluster

func LoadCluster

func LoadCluster() error

LoadCluster loads all databases from the current cluster

func ReloadDatabase

func ReloadDatabase(nameOfDatabase string) error

ReloadDatabase reloads a database from file

func RenameDatabase

func RenameDatabase(nameOfDatabase string, newNameOfDatabase string) error

RenameDatabase renames a database

func RenameKeyInDatabase

func RenameKeyInDatabase(nameOfDatabase string, keyOld string, keyNew string) error

RenameKeyInDatabase renames a key in a database and write it to file

func SetDatabase

func SetDatabase(nameOfDatabase string, database Database) error

SetDatabase sets a database and writes it to file

func SetKeyInDatabase

func SetKeyInDatabase(nameOfDatabase string, key string, value interface{}) error

SetKeyInDatabase sets a key from in a database and write it to file

func SetLocationOfCluster

func SetLocationOfCluster(location string) error

SetLocationOfCluster sets locationOfCluster if p is a valid folder

func SetNameOfCluster

func SetNameOfCluster(name string) error

SetNameOfCluster sets nameOfCluster if s is a valid cluster name

Types

type Cluster

type Cluster map[string]Database

type Database

type Database map[string]interface{}

func GetDatabase

func GetDatabase(nameOfDatabase string) (Database, error)

GetDatabase returns a database if it exists

Jump to

Keyboard shortcuts

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