joltDB

package module
v0.0.0-...-7e2f7ff Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2017 License: MIT Imports: 5 Imported by: 1

README

json + boltDB = joltDB

An api written in Go to simplify working with boltdb/bolt. JoltDB saves structs as json and retrieves the stored data as json.

Please have a look at the example.

Usage

package main

import (
	"fmt"
	"github.com/jasonmain/joltDB"
)

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Age  string `json:"age"`
}

func main() {

	// Open boltDB database in working dir.
	// If one isn't there it will be created.
	joltDB.Open("./data.db")

	// Add some users.
	users := []*User{
		{"100", "Ken", "39"},
		{"101", "Max", "25"},
		{"102", "Bill", "23"},
		{"103", "Martin", "54"},
		{"104", "Kross", "33"},
	}

	// Save users.
	for _, user := range users {
		// With simple key
		if err := joltDB.Save("users", user.ID, user); err != nil {
			fmt.Println(err)
		}
		// With a prefixed key.
		if err := joltDB.Save("users", user.Name+":"+user.ID, user); err != nil {
			fmt.Println(err)
		}
	}

	// Get one user.
	getUser, err := joltDB.GetOne("users", "103")
	if err != nil {
		fmt.Println(err)
	}

	// Get full user list.
	userList, err := joltDB.List("users")
	if err != nil {
		fmt.Println(err)
	}

	// Get user list by range.
	userListRange, err := joltDB.ListRange("users", "101", "103")
	if err != nil {
		fmt.Println(err)
	}

	// Get user list by Prefix.
	userListPrefix, err := joltDB.ListPrefix("users", "K")
	if err != nil {
		fmt.Println(err)
	}

	// Print the saved user lists.
	fmt.Printf("\n\rGet One User:\n\r%s\n\r\n\r", getUser)
	fmt.Printf("Full User List:\n\r%s\n\r\n\r", userList)
	fmt.Printf("User List By Range:\n\r%s\n\r\n\r", userListRange)
	fmt.Printf("User List By Prefix:\n\r%s", userListPrefix)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

func CloseReadOnly

func CloseReadOnly()

func Copy

func Copy(dir string) error

func GetOne

func GetOne(bucket, key string) ([]byte, error)

func GetOneReadOnly

func GetOneReadOnly(bucket, key string) ([]byte, error)

func List

func List(bucket string) ([]byte, error)

func ListPrefix

func ListPrefix(bucket, prefix string) ([]byte, error)

func ListPrefixReadOnly

func ListPrefixReadOnly(bucket, prefix string) ([]byte, error)

func ListRange

func ListRange(bucket, start, stop string) ([]byte, error)

func ListRangeReadOnly

func ListRangeReadOnly(bucket, start, stop string) ([]byte, error)

func ListReadOnly

func ListReadOnly(bucket string) ([]byte, error)

func Open

func Open(dir string) error

func OpenReadOnly

func OpenReadOnly(dir string) error

func Save

func Save(bucket, key string, data interface{}) error

Types

type BoltDB

type BoltDB struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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