bunt

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

README

db/bunt

Import path: github.com/InsideGallery/core/db/bunt

Package bunt provides a small BuntDB wrapper for JSON-backed key/value storage. It opens a github.com/tidwall/buntdb database, embeds the BuntDB handle in Wrapper, and adds Get and Set helpers that unmarshal and marshal values as JSON.

Main APIs

  • ConnectionConfig configures the BuntDB filename. The default filename is :memory:.
  • GetConnectionConfigFromEnv(prefix) reads PREFIX_FILENAME.
  • Open(config) opens a Wrapper from explicit config.
  • OpenFromEnv(prefix) reads config from an explicit environment prefix and opens the database.
  • Wrapper.Get(name, value) reads a JSON value by key.
  • Wrapper.Set(name, value) writes a JSON value by key.
  • GetConnection() is the legacy default constructor. It uses the DB prefix and is deprecated in favor of explicit config.

Usage

package example

import "github.com/InsideGallery/core/db/bunt"

func saveProfile() (err error) {
	store, err := bunt.Open(&bunt.ConnectionConfig{Filename: ":memory:"})
	if err != nil {
		return err
	}
	defer func() {
		if closeErr := store.Close(); err == nil {
			err = closeErr
		}
	}()

	if err := store.Set("profile:1", map[string]string{"name": "Ada"}); err != nil {
		return err
	}

	var profile map[string]string
	return store.Get("profile:1", &profile)
}

Configuration And Operations

Use Open when the application already owns configuration. Use OpenFromEnv when configuration should come from environment variables. Call Close on the returned wrapper during shutdown. Missing keys and storage errors are returned from BuntDB directly.

Documentation

Overview

Package bunt provides BuntDB connection helpers.

New code should pass explicit configuration or an explicit environment prefix:

import "github.com/InsideGallery/core/db/bunt"

db, err := bunt.Open(&bunt.ConnectionConfig{Filename: ":memory:"})

Compatibility: GetConnection remains available for consumers that still rely on the default DB environment prefix. Prefer Open or OpenFromEnv so ownership of configuration is visible at the call site.

Index

Constants

View Source
const EncPrefixDB = "DB"

EncPrefixDB contains profix of DB

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionConfig

type ConnectionConfig struct {
	Filename string `env:"_FILENAME" envDefault:":memory:"`
}

ConnectionConfig contains listen url for the server and additional options

func GetConnectionConfigFromEnv

func GetConnectionConfigFromEnv(prefix string) (*ConnectionConfig, error)

GetConnectionConfigFromEnv return server configs bases on environment variables

type Wrapper

type Wrapper struct {
	*buntdb.DB
}

Wrapper wrapper of buntdb

func GetConnection deprecated

func GetConnection() (*Wrapper, error)

GetConnection return new connection to buntdb

Deprecated: use Open or OpenFromEnv with explicit config ownership.

func Open added in v1.1.0

func Open(config *ConnectionConfig) (*Wrapper, error)

Open creates a BuntDB wrapper from explicit config.

func OpenFromEnv added in v1.1.0

func OpenFromEnv(prefix string) (*Wrapper, error)

OpenFromEnv creates a BuntDB wrapper from an explicit environment prefix.

func (*Wrapper) Get

func (w *Wrapper) Get(name string, value interface{}) error

Get get value by key

func (*Wrapper) Set

func (w *Wrapper) Set(name string, value interface{}) error

Set set value by key

Jump to

Keyboard shortcuts

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