filedb

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 15 Imported by: 0

README

FileDB A dumb but usefull database

Have you hever thought "Wait a second i'm using a key value store as nosql database because i want to store flexable document but have to meany of them to store them in one json file"?
Well then you where probebly searching for this, this database aims to solve that problem by providing an easy to use nosql database that is server less.

NOTE: This is probebly not a stable database and thus you should not use this in a production envourment where the data you are storing is important.

Feathers

  • Easy to use
  • Small with less than 1000 lines of go code and 1 dependnecy go.uuid

Limitations

  • You cannot make OR queries nor make complicated number queries like greather than, etc...
  • The db is currently really dependent on the file system so if the fs is shitty or the hardware this db wont work well
  • Every object is stored as json using go's json package so the json field tag will be used what might cause some problems

Get started

func main() {
	db, err := filedb.NewDB("db")
	handleErr(err)

	type User struct{
		fileDB.M
		Username string `json:"Username"`
		Password string `json:"Password"`
	}

	// Set the search keys
	userKV := filedb.NewKV("Username")
	passKV := filedb.NewKV("Password")

	// Add database
	err = db.Whitelist(
		&User{},
		passKV,
		userKV,
	)
	handleErr(err)

	// Insert a user
	newUser := &User{
		Username: "root",
		Password: "my-secret-password",
	}
	err = db.Save(newUser)
	handleErr(err)
	fmt.Println("User ID:", newUser.ID)

	// Find multiple documents
	users := []User{}
	err = db.Find(&users)
	handleErr(err)
	fmt.Println("First user:", users[0].Username, ",Total Users:", len(users))

	// Find single document, with a filter
	user := &User{}
	err = db.Find(user, filedb.IDkv.V(newUser.ID))
	handleErr(err)
	fmt.Println("Found user:", user.Username)

	// Remove everything
	// You can also add fillin indexable data in the user and if so that will be used as filter
	// Like: &User{Username: "root"}
	err = db.Delete(&User{})
	handleErr(err)
}

What are these KV things?

TL;DR Short for key and value, they are simply a pair of a key and value used for searching ment to be set global and used by everything.

Since this database is limited to the naming of the created files we cannot just search for everything inside a struct.
Because of that i want these things pre defined and a KV (key value) object seems to be the best option for this.
With a KV you can pre define the key and later set the value.
The creation of a KV is ment to be dumb long because then a api user will refactor them away globally somewhat automaticly what hopefully results in less user error in the long run.

Compaired to other types of databases

* FileDB SqlLite Most NoSql DBs Key-value stores
Easy to use Yes No (Setting up and using requires quite a bit of nolange and time) Yes Yes
Can query more than a key Yes Yes Yes No
Can make advanced queries No Yes Yes No
Serverless & C-less (no imported c code) Yes Yes No (Most projects imports c code and thus adding complexity to a program) Yes
Code size Small Medium Huge Huge

But why this over something like gorm combined with sqlite

Mostly personal preference, gorm is an amazing library but has some major limitations that remove the fun working with it for me at least.

  • You cannot use maps (In this library you can)
  • Every nested struct is a database table and thus every nested struct needs an ID with as result you need to reference the orignal struct in the nested one, WHAAAA this is soo dumb (This library doesn't create a new collection for every struct)
  • The oldschool sql relations makes it so you need to inform gorm about how tables are called when joined (Here you don't have to because there are no relations)
  • Your code needs to match sql's way of thinking, sounds dumb but you can't just start creating structs and gorm will figure out the relations as long as there is a gorm.m, You can arguee that this is for the good and i agree if you are running a sql database but that's exacly my problem i don't want a sql database because of these problems.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// IDkv is the key value for M.ID
	IDkv = KV{/* contains filtered or unexported fields */}

	// ErrNoDocumentFound explains itself mostly
	ErrNoDocumentFound = errors.New("Did not found requested document")
)

Functions

This section is empty.

Types

type DB

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

DB contains the database information

func NewDB

func NewDB(location string) (*DB, error)

NewDB creates a new db

func (*DB) Delete

func (db *DB) Delete(in interface{}) error

Delete removes an item from the database

NOTE: It's currently not possible to delete a slice of items though

it will delete ALL items matching a set struct field expect
if ID is set then only 1 will be removed and all
other fields will be ignored

NOTE: If the input struct has a filledin M.ID all other fields will

be ignored

func (*DB) Find

func (db *DB) Find(bindTo interface{}, keyValues ...KV) error

Find finds entries in the database based on the KVs (key values) provided

func (*DB) Save

func (db *DB) Save(in interface{}) error

Save creates or updates a database entry

func (*DB) Whitelist

func (db *DB) Whitelist(in interface{}, searchKeys ...KV) error

Whitelist Whitelists a struct to be allowed inside the database This also adds searchKeys so the struct can be searched for, M.ID is automatily always added as search key

type KV

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

KV is a key value store

func NewKV

func NewKV(k string) KV

NewKV creates a new key value store where k is the key

func (KV) V

func (k KV) V(v interface{}) KV

V returns the keyvalue instace with a value

type M

type M struct {
	ID        string    `json:"id"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

M needs to be inside a struct for it to be a valid

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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