minidb

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: Unlicense Imports: 11 Imported by: 0

README

minidb

Test Status

a simple multi-flat-files json database

This has a really weird structure and api and I know it :happy:

Install

go get -u github.com/TheBoringDude/minidb

Usage

All operations are just appending and setting in a map[string]interface{} or append([]interface{}, interface{}).

In all operations, it writes to the json file. I think it is not a good idea?

Full Doc: https://pkg.go.dev/github.com/TheBoringDude/minidb
MiniDB

It takes a directory and manages all files within it. It is better only to use this when trying to manage multiple json files.

New files are created with a random uuid using ksuid

NOTE: this creates many json files in a specified directory
db := minidb.New("dirfolder")

// db.Keys("key"), nested minidbs
// db.Collections("key"), a json collections, []
// db.Store("key"), a simple json key-value store (not meant with nested maps)

cols := db.Collections("posts")
cols.Push(map[string]string{
    "title": "Hello World",
    "content": "This is just something, maybe a content or not. I don't know how it works though.",
})

// multiple elements is possible
cols.Push(100, 20, "sample", false, []int{1,2,3,4,5})

fmt.Println(cols)
MiniCollections

A simple collections json db file.

db := minidb.NewMiniCollections("cols.json")
db.Push(1)

fmt.Println(1)
MiniStore

A simple key-value store json db file.

db := minidb.NewMiniStore("store.json")
db.Set("key", "value")

fmt.Println(db.GetString("key"))

TODO

  • Improve concurrency support.
  • fixes, improvements
  • more changes..
  • ...future development

License

Documentation

Index

Constants

View Source
const Version = "0.1.6"

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseMiniDB

type BaseMiniDB struct {
	// contains filtered or unexported fields
}

BaseMiniDB is the base db structure.

type MiniCollections

type MiniCollections struct {
	BaseMiniDB
	// contains filtered or unexported fields
}

MiniCollections is a collections store.

func NewMiniCollections

func NewMiniCollections(f string) *MiniCollections

NewMiniCollections creates and returns a new collections json db.

func (*MiniCollections) FilterBool added in v0.1.4

func (c *MiniCollections) FilterBool() []bool

FilterBool returns all bool elements.

func (*MiniCollections) FilterFloat added in v0.1.4

func (c *MiniCollections) FilterFloat() []float64

FilterFloat returns all float elements (uses float64). Some integers (int) might be included.

func (*MiniCollections) FilterInt added in v0.1.4

func (c *MiniCollections) FilterInt() []int

FilterInt returns all int elements.

func (*MiniCollections) FilterString added in v0.1.4

func (c *MiniCollections) FilterString() []string

FilterString returns all string elements.

func (*MiniCollections) Find

func (c *MiniCollections) Find(v interface{}) int

Find attemps to find the value from the store and returns it's index. If it doesn't exist, it will return -1.

func (*MiniCollections) FindAll

func (c *MiniCollections) FindAll(v interface{}) []interface{}

FindAll returns all values that matches v.

func (*MiniCollections) First

func (c *MiniCollections) First() interface{}

First returns the first element of the collections.

func (*MiniCollections) Last

func (c *MiniCollections) Last() interface{}

Last returns the last element of the collections.

func (*MiniCollections) List added in v0.1.3

func (c *MiniCollections) List() []interface{}

List returns all of the contents of the collection

func (*MiniCollections) MatchString

func (c *MiniCollections) MatchString(v string) (string, error)

MatchString returns the first element that contains v.

func (*MiniCollections) MatchStringAll

func (c *MiniCollections) MatchStringAll(v string) ([]string, error)

MatchStringAll returns the first element that contains v.

func (*MiniCollections) Push

func (c *MiniCollections) Push(v ...interface{})

Push adds an item to the store slice.

func (*MiniCollections) Remove added in v0.1.5

func (c *MiniCollections) Remove(v interface{}) error

Remove removes the first element that is equal to v. It wraps around RemoveMany().

func (*MiniCollections) RemoveAll added in v0.1.5

func (c *MiniCollections) RemoveAll(v interface{}) error

RemoveAll removes all elements that are equal to v. It wraps around RemoveMany().

func (*MiniCollections) RemoveMany added in v0.1.5

func (c *MiniCollections) RemoveMany(v interface{}, l int) error

RemoveMany removes the number of elements corresponding to l. Use RemoveAll() for removing all elements and Remove() for a single element. `l` cannot be less than -1 or equal to 0.

type MiniDB

type MiniDB struct {
	BaseMiniDB
	// contains filtered or unexported fields
}

MiniDB is the base store file.

func New

func New(dir string) *MiniDB

New creates a new MiniDB struct. The dir will be created if it doesn't exist and a file named `__default.json` will also be generated. It is better to use this in managing multiple json files.

func (*MiniDB) Collections

func (db *MiniDB) Collections(key string) *MiniCollections

Collections creates a new key with an array / slice value.

func (*MiniDB) FindCollection

func (db *MiniDB) FindCollection(key string) (string, error)

FindCollection gets the key in the keys map and returns its corresponding filename. It returns nil if it exists.

func (*MiniDB) FindKey

func (db *MiniDB) FindKey(key string) (string, error)

FindKey gets the key in the keys map and returns its corresponding filename. It returns nil if it exists.

func (*MiniDB) FindStore

func (db *MiniDB) FindStore(key string) (string, error)

FindStore gets the key in the keys map and returns its corresponding filename. It returns nil if it exists.

func (*MiniDB) Key

func (db *MiniDB) Key(key string) *MiniDB

Key creates a new key in the json. It is better to use this for nesting and only if needed.

func (*MiniDB) ListCollections added in v0.1.1

func (db *MiniDB) ListCollections() []string

ListCollections returns the list of collections created.

func (*MiniDB) ListStores added in v0.1.1

func (db *MiniDB) ListStores() []string

ListCollections returns the list of collections created.

func (*MiniDB) RemoveCollection

func (db *MiniDB) RemoveCollection(key string) error

RemoveCollection removes the collection key and the files corresponding to it. It returns nil if it is successful.

func (*MiniDB) RemoveKey

func (db *MiniDB) RemoveKey(key string) error

RemoveKey removes the key and the files corresponding to it. It returns nil if it is successful.

func (*MiniDB) RemoveStore

func (db *MiniDB) RemoveStore(key string) error

RemoveStore removes the store key and the files corresponding to it. It returns nil if it is successful.

func (*MiniDB) Store

func (db *MiniDB) Store(key string) *MiniStore

Store creates a new key with a given value in the json.

type MiniDBContent added in v0.1.5

type MiniDBContent struct {
	Keys        map[string]string `json:"keys"`
	Collections map[string]string `json:"collections"`
	Store       map[string]string `json:"store"`
}

MiniDBContent is the types of MiniDB.store

type MiniStore

type MiniStore struct {
	BaseMiniDB
	// contains filtered or unexported fields
}

MiniStore is a key-value store.

func NewMiniStore

func NewMiniStore(f string) *MiniStore

NewMiniStore creates and returns a new key-store collection json db.

func (*MiniStore) FindKey added in v0.1.6

func (db *MiniStore) FindKey(key string) []string

FindKeys finds all the keys that contains `key`. It will return only all of the matched keys. This is useful for searching keys.

func (*MiniStore) Get

func (db *MiniStore) Get(key string) (interface{}, bool)

Get finds the key and returns an interface value. It also returns false if the key does not exist and true, otherwise.

func (*MiniStore) GetBool

func (db *MiniStore) GetBool(key string) bool

GetBool finds the key with bool value and returns if exits. It panics if there is an error in type assertion.

func (*MiniStore) GetBoolSlice

func (db *MiniStore) GetBoolSlice(key string) []bool

GetBoolSlice finds the key with the []bool value and returns if exits. It panics if there is an error in type assertion.

func (*MiniStore) GetFloat32

func (db *MiniStore) GetFloat32(key string) float32

GetFloat32 finds the key with the float32 value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetFloat32Slice

func (db *MiniStore) GetFloat32Slice(key string) []float32

GetFloat32Slice finds the key with the []float32 value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetFloat64

func (db *MiniStore) GetFloat64(key string) float64

GetFloat64 finds the key with the float64 value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetFloat64Slice

func (db *MiniStore) GetFloat64Slice(key string) []float64

GetFloat64Slice finds the key with the []float64 value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetInt

func (db *MiniStore) GetInt(key string) int

GetInt finds the key with the int value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetIntSlice

func (db *MiniStore) GetIntSlice(key string) []int

GetIntSlice finds the key with the []int value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetString

func (db *MiniStore) GetString(key string) string

GetString finds the key with the string value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) GetStringSlice

func (db *MiniStore) GetStringSlice(key string) []string

GetStringSlice finds the key with the []string value and returns if exists. It panics if there is an error in type assertion.

func (*MiniStore) IsExists added in v0.1.4

func (db *MiniStore) IsExists(key string) bool

IsExists asserts if the key exists. You should use Get() if you want to get the Raw value and if it exists.

func (*MiniStore) List added in v0.1.4

func (db *MiniStore) List() map[string]interface{}

List returns the content of db.store

func (*MiniStore) Read added in v0.1.1

func (db *MiniStore) Read(v interface{}) error

Read parses the contents of db.store to v which is a struct object. It just wraps around `json.Marshal` and `json.Unmarshal`.

func (*MiniStore) ReadKey added in v0.1.1

func (db *MiniStore) ReadKey(key string, v interface{}) error

ReadKey parses the contents of db.store[key] to v which is a struct object. It is better to use this if the value of key is a map. It just wraps around `json.Marshal` and `json.Unmarshal`.

func (*MiniStore) Remove

func (db *MiniStore) Remove(key string) error

Remove attemps to remove the key from the db if it exists. It returns nil if it is removed

func (*MiniStore) Set

func (db *MiniStore) Set(key string, v interface{}) error

Set sets the store[key] to v.

func (*MiniStore) Update

func (db *MiniStore) Update(key string, v interface{}) error

Update updates the key's value. It returns nil if updated.

func (*MiniStore) Write

func (db *MiniStore) Write(v interface{}) error

Write takes a struct and writes it to the json file. It accepts a new struct object and encodes and write it.

Jump to

Keyboard shortcuts

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