boltdb

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 4 Imported by: 2

README

boltdb

tests Go Report Card Go Reference Go Coverage

Go module github.com/devilcove/boltdb is a generic abstractions layer for basic crud operations on a go.etcd.io/bbolt key/value store

Installing

To start using, install Go and run go get:

go get github.com/devilcove/boltdb@latest

Functions

Initialization

call initialize with the path to store and list of tables. Store and tables will be created if they do not exist.

import "github.com/devilcove/bbolt"

if err := boltdb.Initialize(path, []string{"users", "networks"}); err != nil{
  return err
}
defer boltd.Close()

Create/Update

pass the key/value pair along with table name

Save -- save key/value always (overwrite existing or create new)
Insert -- save only iff key does not exist
Update -- save only iff key exists
cont userTable = "users"

user := models.User {
  Username: "admin",
  Password: "encrypted password",
  IsAdmin: true,
}

if err := boltdb.Save(user, user.Username, userTable); err != nil {
  return err
}

Read

return value of key in table

user, err := boltdb.Get[models.User]("admin", userTable)
if err != nil {
  return err
}

retrieve all values from table

users, err := boltdb.GetAll[models.User](userTable)
if err != nil {
  return err
}

Delete

delete value of key in table

if err := boltdb.Delete[models.User]("admin", userTable); err != nil {
  return err
}
Advanced Usage

the db connection is made available if more advanced queries are needed

import (
  "encoding/json"
  "errors"

  "github.com/devilcove/boltdb"
  "go.etcd.io/bbolt"
)

func AdminExists() bool {
	var user models.User
	var found bool
	db := boltdb.Connection()
	if db == nil {
		return found
	}
	if err := db.View(func(tx *bbolt.Tx) error {
		b := tx.Bucket([]byte(UserTable))
		if b == nil {
			return boltdb.ErrNoResults
		}
		_ = b.ForEach(func(k, v []byte) error {
			if err := json.Unmarshal(v, &user); err != nil {
				return err
			}
			if user.IsAdmin {
				found = true
			}
			return nil
		})
		return nil
	}); err != nil {
		return false
	}
	return found
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResults        = errors.New("no results found") // ErrNoResults indicates query found no results
	ErrInvalidTableName = errors.New("invalid table")    // ErrInvalidTableName indicates that specified table does not exist
	ErrNoConnection     = errors.New("no db connection") //
	ErrExists           = errors.New("key existss")
)

Generic error results

Functions

func Close

func Close() error

Close closes the database

func Connection

func Connection() *bbolt.DB

Connection returns the connection to the store for more advanced queries by caller

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/devilcove/boltdb"
	"go.etcd.io/bbolt"
)

const UserTable = "users"

type User struct {
	UserName string
	Password string
	IsAdmin  bool
}

func main() {
	if AdminExists() {
		fmt.Println("admin exists")
	} else {
		fmt.Println("admin does not exist")
	}
}

func AdminExists() bool {
	var user User
	var found bool
	db := boltdb.Connection()
	if db == nil {
		return found
	}
	if err := db.View(func(tx *bbolt.Tx) error {
		b := tx.Bucket([]byte(UserTable))
		if b == nil {
			return boltdb.ErrNoResults
		}
		_ = b.ForEach(func(k, v []byte) error {
			if err := json.Unmarshal(v, &user); err != nil {
				return err
			}
			if user.IsAdmin {
				found = true
			}
			return nil
		})
		return nil
	}); err != nil {
		return false
	}
	return found
}
Output:

admin does not exist

func Delete

func Delete[T any](key, table string) error

Delete deletes the entry in table corresponding to key

func Get

func Get[T any](key, table string) (T, error)

Get retrieves a value for key in specified table

func GetAll

func GetAll[T any](table string) ([]T, error)

GetAll retrieves all values from table

func Initialize

func Initialize(file string, tables []string) error

Initialize sets up bbolt db using file path and creates tables if required

func Insert added in v0.1.3

func Insert(value any, key, table string) error

Insert saves a value only if key does not exist

func Save

func Save(value any, key, table string) error

Save saves a value under key in the specified table

func Update added in v0.1.3

func Update(value any, key, table string) error

Update save a value only if key already exists

Types

This section is empty.

Jump to

Keyboard shortcuts

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