bBoltClient

package module
v0.0.0-...-0ae5317 Latest Latest
Warning

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

Go to latest
Published: May 29, 2021 License: MIT Imports: 2 Imported by: 0

README

Bbolt Client

Overview

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.

Usage

Create a New Client/DB
package 
import (
  bc "gitlab.com/hooksie1/bboltClient"
)
func main() {
 client := bc.NewClient()
 client.NewDB("mydb.db")
}
Create a Bucket
bucket := bc.NewBucket("test")
client.Write(bucket)
Create a New KV
kv := bc.NewKV().SetBucket(bucket.Name).
	SetKey("testkey").SetValue("testvalue")
client.Write(kv)
Read KV

Reading a KV sets the value in the KV passed to read.

kv := bc.NewKV().SetBucket(bucket.Name).
	SetKey("testkey")
client.Read(kv)
fmt.Println(kv.Value)
Write a Slice of KVs
kv1 := bc.NewKV().SetBucket(bucket.Name).
	SetKey("test").SetValue("test")
kv2 := bc.NewKV().SetBucket(bucket.Name).
	SetKey("somekey").SetValue("somevalue")

kvs := bc.KVs {
	kv1,
	kv2,
}

client.Write(kvs)
Read All KVs from a Bucket

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)
}
Delete a Bucket/KV/KVs

Use the same process for each type.

bucket := bc.Newbucket("test")
client.Delete(bucket)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoltClient

type BoltClient struct {
	DB *bbolt.DB
}

BoltClient holds a Bolt DB connection

func NewClient

func NewClient() *BoltClient

NewClient returns a new BoltClient

func (BoltClient) Delete

func (b BoltClient) Delete(r boltDeleter) error

Delete is the entrypoint to delete a database resource.

func (*BoltClient) NewDB

func (b *BoltClient) NewDB(name string) error

NewDB creates a new database or opens an existing database and attaches it to the client.

func (BoltClient) Read

func (b BoltClient) Read(r boltReader) error

Read is the entrypoint to read a KV or slice of KVs.

func (BoltClient) ReadAll

func (b BoltClient) ReadAll(r boltReader) (KVs, error)

ReadAll is the entrypoint to read all KVs from a bucket.

func (BoltClient) Write

func (b BoltClient) Write(w boltWriter) error

Write is the entrypoint to write either a KV, a slice of KVs, or a Bucket.

type Bucket

type Bucket struct {
	Name string
}

Bucket holds the name of a bucket in a BoltDB database.

func NewBucket

func NewBucket(name string) *Bucket

NewBucket returns a Bucket with the specified name.

type KV

type KV struct {
	Bucket string
	Key    string
	Value  string
}

func NewKV

func NewKV() *KV

NewKV returns a new empty KV.

func (*KV) SetBucket

func (kv *KV) SetBucket(b string) *KV

SetBucket sets the bucket value for a KV.

func (*KV) SetKey

func (kv *KV) SetKey(k string) *KV

SetKey sets the key for a KV.

func (*KV) SetValue

func (kv *KV) SetValue(v string) *KV

SetValue sets the value for a KV.

type KVs

type KVs []*KV

Jump to

Keyboard shortcuts

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