Giter Site home page Giter Site logo

boltdb's Introduction

boltdb

tests Go Report Card Go Reference Go Coverage

Go module github.com/devilcove/boltdb is a generic abstractions layer for basic crud operations on a go.etcd.io/bbolt key/value store

Installing

To start using, install Go and run go get:

go get github.com/devilcove/boltdb@latest

Functions

Initialization

call initialize with the path to store and list of tables. Store and tables will be created if they do not exist.

import "github.com/devilcove/bbolt"

if err := boltdb.Initialize(path, []string{"users", "networks"}); err != nil{
  return err
}
defer boltd.Close()

Create/Update

pass the key/value pair along with table name

Save -- save key/value always (overwrite existing or create new)

Insert -- save only iff key does not exist

Update -- save only iff key exists

cont userTable = "users"

user := models.User {
  Username: "admin",
  Password: "encrypted password",
  IsAdmin: true,
}

if err := boltdb.Save(user, user.Username, userTable); err != nil {
  return err
}

Read

return value of key in table

user, err := boltdb.Get[models.User]("admin", userTable)
if err != nil {
  return err
}

retrieve all values from table

users, err := boltdb.GetAll[models.User](userTable)
if err != nil {
  return err
}

Delete

delete value of key in table

if err := boltdb.Delete[models.User]("admin", userTable); err != nil {
  return err
}

Advanced Usage

the db connection is made available if more advanced queries are needed

import (
  "encoding/json"
  "errors"

  "github.com/devilcove/boltdb"
  "go.etcd.io/bbolt"
)

func AdminExists() bool {
	var user models.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
}

boltdb's People

Contributors

dependabot[bot] avatar mattkasun avatar

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.