https://github.com/devilcove/boltdb
Generic CRUD abstractions to bbolt (etc.io/bbolt) database
https://github.com/devilcove/boltdb
bbolt bolt boltdb database go
Last synced: 2 months ago
JSON representation
Generic CRUD abstractions to bbolt (etc.io/bbolt) database
- Host: GitHub
- URL: https://github.com/devilcove/boltdb
- Owner: devilcove
- License: apache-2.0
- Created: 2023-12-21T19:35:55.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-11-26T22:02:02.000Z (5 months ago)
- Last Synced: 2025-11-29T17:27:49.399Z (5 months ago)
- Topics: bbolt, bolt, boltdb, database, go
- Language: Go
- Homepage:
- Size: 82 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
boltdb
-------

[](https://goreportcard.com/report/github.com/devilcove/boltdb)
[](https://pkg.go.dev/github.com/devilcove/boltdb)
[](https://raw.githack.com/wiki/devilcove/boltdb/coverage/coverage.html)
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
}
````