I use bbolt in a few of my projects and repeating the same boilerplate to interact with the database gets tedious. This is an attempt to make it a little less verbose to interact with bbolt.
package
import (
bc "github.com/hooksie1/bclient"
)
func main() {
client := bc.NewClient()
client.NewDB("mydb.db")
}
bucket := bc.NewBucket("test")
client.Write(bucket)
bucket := bc.NewBucket("test")
nested := bc.NewBucket("nested")
bucket.SetNestedBucket(nested)
bucket := bc.NewBucket("test")
kv := bc.NewKV().SetBucket(bucket).
SetKey("testkey").SetValue("testvalue")
client.Write(kv)
Reading a KV sets the value in the KV passed to read.
bucket := bc.NewBucket("test")
kv := bc.NewKV().SetBucket(bucket).
SetKey("testkey")
client.Read(kv)
fmt.Println(kv.Value)
bucket := bc.NewBucket("test")
kv1 := bc.NewKV().SetBucket(bucket).
SetKey("test").SetValue("test")
kv2 := bc.NewKV().SetBucket(bucket).
SetKey("somekey").SetValue("somevalue")
kvs := bc.KVs {
kv1,
kv2,
}
client.Write(kvs)
Returns a slice of KVs from the specified bucket.
bucket := bc.NewBucket("testing")
kvs, err := client.ReadAll(bucket)
if err != nil {
log.Println(err)
}
for _, v := range kvs {
fmt.Printf("bucket: %s, key: %s, value: %s", v.Bucket, v.Key, v.Value)
}
Use the same process for each type.
bucket := bc.Newbucket("test")
client.Delete(bucket)
Since the client just embeds a *bbolt.DB
you can access the View
and Update
methods directly.
package
import (
bc "gitlab.com/hooksie1/bclient"
)
func main() {
client := bc.NewClient()
client.NewDB("mydb.db")
client.DB.View()
}