Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNoResults indicates query found no results. ErrNoResults = errors.New("no results found") // ErrInvalidTableName indicates that specified table does not exist. ErrInvalidTableName = errors.New("invalid table") // ErrNoConnection indicates that database is not open. ErrNoConnection = errors.New("no db connection") // ErrExists indicates that a key exists. ErrExists = errors.New("key existss") )
Generic error results.
Functions ¶
Types ¶
type Store ¶ added in v0.1.9
type Store struct {
// contains filtered or unexported fields
}
func Initialize ¶
Initialize sets up bbolt db using file path and creates tables if required.
func (*Store) Connection ¶ added in v0.1.9
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
Click to show internal directories.
Click to hide internal directories.