Documentation
¶
Index ¶
- Variables
- func Close() error
- func Connection() *bbolt.DB
- func Delete[T any](key, table string) error
- func Get[T any](key, table string) (T, error)
- func GetAll[T any](table string) ([]T, error)
- func Initialize(file string, tables []string) error
- func Insert(value any, key, table string) error
- func Save(value any, key, table string) error
- func Tables() []string
- func Update(value any, key, table string) error
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 Connection ¶
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 Initialize ¶
Initialize sets up bbolt db using file path and creates tables if required
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.