lyte

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

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

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 7 Imported by: 0

README

lytedb

lytedb is an easy to use on-disk database for Go applications. Data is divided into collections and accessed by a key. This package leverages bolt and the standard lib encoding/gob to handle storage and serialization. Read about motivations here.

Storing/retrieving structs on disk should be this easy:

// add a new user under the key 'user-id'
lytedb.Add("user-id", User{Age: 22, Name: "Andres"})

// our user struct
var user User

// write value assigned to 'user-id' onto our struct
lytedb.Get("user-id", &user)

Usage

Open DB

Simply supply a path to a file. If no file exists, it will be created automatically. Remember that this will obtain a file lock on the specified file so no other process can open it.


db, err := lyte.New("/path/to/file.db")
if err != nil {
    // handle
}
defer db.Close()

Collections

Data can be divided into different groups called collections. Collections are a convenient way to seperate data while maintaining unique keys. Meaning you can have entries with the same key as long as they are in different collections.

userDB, err := db.Collection("users")

userDB.Add("user1", user)

Add/Get data

lytedb uses collections to seperate data but also uses a "main" collection for general storage. Remember that the db encodes values so only exported values get written.

type User struct {
    ID string
    Name string
    Age int
}

user := User{
    ID: "Vote for Pedro",
    Name: "Napoleon",
    Age: 25,
}

db.Add("napoleon", user)

Similarly, getting values writes back onto a struct. This is why values need to be exported so that the underlying encoding package (encoding/gob) can read/write from structs.

The Get method must recieve a pointer to the struct to write onto.

var user User
db.Get("napoleon", &user)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrValueNotPtr ..
	ErrValueNotPtr = errors.New("Valued passed into Get needs to be a pointer")
)

Functions

This section is empty.

Types

type Col

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

Col refers to a collection for the DB

func (*Col) Add

func (c *Col) Add(key string, value interface{}) error

Add a key/value pair to DB under this collection

func (*Col) Get

func (c *Col) Get(key string, value interface{}) error

Get will write the value associated with the key to the given interface

type DB

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

DB is made up of 'collections' which hold certain 'types' which are stored on disk. It is up to the user to define these collections and add the relevant data

func New

func New(path string) (*DB, error)

New returns a new db object

func (*DB) Add

func (db *DB) Add(key string, data interface{}) error

Add data specified by a key to the "main" collection

func (*DB) Close

func (db *DB) Close() error

Close underlying DB

func (*DB) Collection

func (db *DB) Collection(name string) (*Col, error)

Collection will return a reference to the underlying collection or create a new one if already exists

func (*DB) Delete

func (db *DB) Delete(key string) error

Delete an entry from DB

func (*DB) Get

func (db *DB) Get(key string, data interface{}) error

Get will retreive the key from the "main" collection and write the data to the data interface

Jump to

Keyboard shortcuts

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