Coder Social home page Coder Social logo

bclient's Introduction

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 "github.com/hooksie1/bclient"
)
func main() {
 client := bc.NewClient()
 client.NewDB("mydb.db")
}

Create a Bucket

bucket := bc.NewBucket("test")
client.Write(bucket)

Create a Nested Bucket

bucket := bc.NewBucket("test")
nested := bc.NewBucket("nested")
bucket.SetNestedBucket(nested)

Create a New KV

bucket := bc.NewBucket("test")
kv := bc.NewKV().SetBucket(bucket).
	SetKey("testkey").SetValue("testvalue")
client.Write(kv)

Read 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)

Write a Slice of KVs

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)

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)

More Advanced Usage

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()
}   

bclient's People

Contributors

hooksie1 avatar

Watchers

 avatar  avatar

bclient's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.