boltdb

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 5 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

read table names

tables := boltdb.Tables()

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 indicates query found no results.
	ErrNoResults = errors.New("no results found")
	// ErrInvalidPath indicates that specified bucket does not exist.
	ErrInvalidPath = errors.New("invalid path")
	// ErrNoConnection indicates that database is not open.
	ErrNoConnection = errors.New("no db connection")
	// ErrExists indicates that a key exists.
	ErrExists = errors.New("key exists")
)

Generic error results.

Functions

This section is empty.

Types

type Path added in v0.2.0

type Path []string

Path represents a nested bucket path.

func (Path) Last added in v0.2.0

func (p Path) Last() string

Last returns the last element of a Path.

func (Path) Parent added in v0.2.0

func (p Path) Parent() Path

Parent returns a Path with last element removed.

type Store added in v0.1.9

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

Store represents a bbolt db store.

func Initialize

func Initialize(file string, buckets []Path) (*Store, error)

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

func Open added in v0.2.0

func Open(file string) (*Store, error)

Open opens a bbolt db file, creating it if it does not exist.

func (*Store) Close added in v0.1.9

func (s *Store) Close() error

Close closes the database.

func (*Store) Connection added in v0.1.9

func (s *Store) 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"
)

type User struct {
	UserName string
	Password string
	IsAdmin  bool
}

const UserTable = "users"

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

func AdminExists() bool {
	var user User
	var found bool
	s := boltdb.Store{}
	db := s.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 (*Store) CopyBucket added in v0.2.0

func (s *Store) CopyBucket(src, dest Path) error

CopyBucket copies a bucket from src path to dest path.

func (*Store) CopyKey added in v0.2.0

func (s *Store) CopyKey(sKey, dKey Path) error

CopyKey copies a key from src parent bucket to dest bucket.

func (*Store) CreateBucket added in v0.2.0

func (s *Store) CreateBucket(path Path) error

CreateBucket creates a new bucket at given path.

func (*Store) Delete added in v0.2.0

func (s *Store) Delete(key string, parent Path) error

Delete deletes a key in a bucket.

func (*Store) DeleteBucket added in v0.2.0

func (s *Store) DeleteBucket(path Path) error

DeleteBucket deletes the bucket at path.

func (*Store) EmptyBucket added in v0.2.0

func (s *Store) EmptyBucket(path Path) error

EmptyBucket deletes all of a buckets children.

func (*Store) Get added in v0.2.0

func (s *Store) Get[T any](key string, bucket Path) (T, error)

Get retrieves a value for key in specified bucket.

func (*Store) GetAll added in v0.2.0

func (s *Store) GetAll[T any](path Path) ([]T, error)

GetAll retrieves all values from bucket.

func (*Store) GetAllRaw added in v0.2.0

func (s *Store) GetAllRaw(path Path) ([][]byte, error)

func (*Store) GetRaw added in v0.2.0

func (s *Store) GetRaw(key []byte, bucket Path) ([]byte, error)

GetRaw retrieves the value of a key in specified bucket.

func (*Store) Insert added in v0.1.9

func (s *Store) Insert(value any, key string, bucket Path) error

Insert saves a value only if key does not exist.

func (*Store) MoveBucket added in v0.2.0

func (s *Store) MoveBucket(src, dest Path) error

MoveBucket copies bucket to new location and deletes original.

func (*Store) MoveKey added in v0.2.0

func (s *Store) MoveKey(src, dest Path) error

MoveKey copies a key to new destination and deletes original.

func (*Store) RenameBucket added in v0.2.0

func (s *Store) RenameBucket(path Path, name string) error

RenameBucket renames a bucket.

func (*Store) RenameKey added in v0.2.0

func (s *Store) RenameKey(path Path, name string) error

RenameKey renames a Key.

func (*Store) Save added in v0.1.9

func (s *Store) Save(value any, key string, parent Path) error

Save saves a generic value under key in the specified bucket.

func (*Store) SaveRaw added in v0.2.0

func (s *Store) SaveRaw(value, key []byte, parent Path) error

SaveRaw saves a byte value in a bucket.

func (*Store) Update added in v0.1.9

func (s *Store) Update(value any, key string, bucket Path) error

Update save a value only if key already exists.

Jump to

Keyboard shortcuts

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