keyv

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 4 Imported by: 1

README

keyv

Keyv provides a key-value interface to access database. By changing the adapter, you can change the database you use.
Inspired by lukechilds/keyv, a similar nodejs module.

Adapters

sqlite3

Usage

package main

import (
	"fmt"
	"os"

	"github.com/simba-fs/keyv"
	_ "github.com/simba-fs/keyvSqlite3"
)

// checkErr return true if err != nil, and print err to stderr
func checkErr(err error) bool {
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return true
	}
	return false
}

type user struct {
	Username string `json:"username"`
	Email    string `json:"email"`
}

func main() {
	// create a new connect with namesapce
	pw, err := keyv.New("sqlite3://database.sqlite", "password")
	if err != nil {
		fmt.Println(err)
		return
	}

	// create another connect with a different namesapce
	usr, err := keyv.New("sqlite3://database.sqlite", "user")
	if err != nil {
		fmt.Println(err)
		return
	}

	// write and read a string
	checkErr(pw.Set("peter", "P@ssw0Rd"))
	fmt.Println(pw.GetString("peter")) // P@ssw0Rd <nil>

	// write a struct
	sean := user{
		Username: "Sean",
		Email:    "seam@example.com",
	}
	checkErr(usr.Set("sean", sean))

	// read a struct
	u := user{}
	checkErr(usr.Get("sean", &u))
	fmt.Printf("%#v\n", u) // main.user{Username:"Sean", Email:"seam@example.com"}

	// list keys in namesapce
	fmt.Println(usr.Keys()) // [sean] <nil>
	fmt.Println(pw.Keys()) // [peter] <nil>
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAdapterNewerNotFound   = errors.New("adapter newer not found")
	ErrAdapterNewerNameExists = errors.New("adapter newer name exists")
)
View Source
var (
	ErrKeyNotFound = errors.New("key not found in namespace")
)

Functions

func Register

func Register(name string, adapterNewer AdapterNewer) error

Register add a new adapter newer with name. It should only be called in adapter package

Types

type Adapter

type Adapter interface {
	// Has checks if key exists
	Has(key string) bool
	// Get returns value by key
	Get(key string) (string, error)
	// Set sets value by key
	Set(key string, val string) error
	// Remove removes value by key
	Remove(key string) error
	// Clear remove all data in this namespace
	Clear(prefix string) error
	// Keys return all keys in this namespace
	Keys() ([]string, error)
}

type AdapterNewer

type AdapterNewer interface {
	// Cnnect return a keyv adapter by the given uri
	Connect(uri string) (Adapter, error)
}

type Keyv

type Keyv struct {
	// AdapterName is the name of used adapter
	AdapterName string
	// Adapter is the used adapter
	Adapter Adapter
	// Uri is the used uri
	Uri string
	// Namespace will be automatically add before key
	Namespace string
}

func New

func New(uri string, namespace string) (*Keyv, error)

New create a keyv object

func (*Keyv) Clear

func (k *Keyv) Clear() error

Clear remove all data in DB with the same namespace

func (*Keyv) Get

func (k *Keyv) Get(key string, v interface{}) error

Get value from DB with key

func (*Keyv) GetBool added in v1.3.8

func (k *Keyv) GetBool(key string) (bool, error)

func (*Keyv) GetInt added in v1.3.8

func (k *Keyv) GetInt(key string) (int, error)

func (*Keyv) GetString added in v1.3.8

func (k *Keyv) GetString(key string) (string, error)

func (*Keyv) Has added in v1.3.2

func (k *Keyv) Has(key string) bool

Has check if key exists in the db

func (*Keyv) Keys added in v1.3.4

func (k *Keyv) Keys() ([]string, error)

Keys return all keys in this namespace

func (*Keyv) Remove

func (k *Keyv) Remove(key string) error

Remove data with key from DB

func (*Keyv) Set

func (k *Keyv) Set(key string, value interface{}) error

Set value with key in DB

Jump to

Keyboard shortcuts

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