database

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 2 Imported by: 0

README

database-go

A simple key-value database for simple projects.

Install

$ go get github.com/davipatricio/database-go

Usage

package main

import (
    "fmt"

    "github.com/davipatricio/database-go"
)

func main() {
    db := database.New("mydatabase.json")
    db.Load()

    db.Set("key", "value")
    db.Save()

    value := db.Get("key")
    fmt.Println(db.Get("key"))

    db.Delete("key")
    db.Save()
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	Path   string
	Data   map[string]interface{}
	Loaded bool
}

Represents a database

func New

func New(path string) *Database

Initializes a instance of a database Database should be loaded before using it!

db := New("database.json")
db.Load()

func (*Database) Clear

func (db *Database) Clear()

Deletes all keys from the database

db.Clear()
db.Save()

func (*Database) Delete

func (db *Database) Delete(key string)

Deletes a key from the database

db.Delete("key")
db.Save()

func (*Database) Get

func (db *Database) Get(key string) interface{}

Returns the value of a key

val := db.Get("key")
if val == nil {
  fmt.Println("key not found")
}

fmt.Println(val)

func (*Database) Has

func (db *Database) Has(key string) bool

Whether the key exists in the database

if db.Has("key") {
  fmt.Println("key exists")
}

func (*Database) IsEmpty

func (db *Database) IsEmpty() bool

Whether the database is empty

if db.IsEmpty() {
  fmt.Println("database is empty")
}

func (*Database) IsLoaded

func (db *Database) IsLoaded() bool

Whether the database is loaded

if db.IsLoaded() {
  fmt.Println("database is loaded")
} else {
  db.Load()
  defer db.Save()
  db.Set("key", "value")
}

func (*Database) Keys

func (db *Database) Keys() []string

Returns a slice of all keys in the database

keys := db.Keys()
for _, key := range keys {
  fmt.Println(key)
}

func (*Database) Load

func (db *Database) Load() error

Loads the database from the database file

func (*Database) Save

func (db *Database) Save() error

Saves the database to the database file You should call this method everytime you make changes

func (*Database) Set

func (db *Database) Set(key string, value interface{})

Sets the value of a key

db.Set("key", "value")
db.Set("ints", 12)
db.Set("maps", map[string]string{"a": "b"})
db.Set("slices", []string{"a", "b"})
db.Save()

func (*Database) Size

func (db *Database) Size() int

Returns the number of keys in the database

fmt.Println(db.Len())

func (*Database) Values

func (db *Database) Values() []interface{}

Returns a slice of all values in the database

values := db.Values()
for _, value := range values {
  fmt.Println(value)
}

Jump to

Keyboard shortcuts

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