Documentation
¶
Overview ¶
Package nutz is a helper for interactions with bolt databases.
Index ¶
- type Storage
- func (s Storage) Create(bucket, key string, value []byte, nested ...string) Storage
- func (s Storage) Delete(bucket, key string, nested ...string) Storage
- func (s Storage) DeleteBucket(bucket string) Storage
- func (s Storage) DeleteDatabase() error
- func (s *Storage) Execute(bucket, key string, value []byte, nested []string, fn StorageFunc) Storage
- func (s Storage) Get(bucket, key string, nested ...string) Storage
- func (s Storage) GetAll(bucket string, nested ...string) Storage
- func (s Storage) Update(bucket, key string, value []byte, nested ...string) Storage
- type StorageFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Storage ¶
type Storage struct {
// Data holds the value returned from the database.
Data []byte
// DataList holds key value pairs, retrievend by the GetAll method. These are the
// values found in the bucket.
DataList map[string][]byte
// Error stores the last encountered error.
Error error
// DBName filename which will be used as the database.
DBName string
// contains filtered or unexported fields
}
Storage contains metadata, which enable accessing the underlying bolt databases.
func NewStorage ¶
NewStorage initializes a new storage object
func (Storage) Create ¶
Create stores a given key value pairs . It takes an optional coma separated strings to act as nested buckets.
Example ¶
c := NewStorage("mydatabase.bdb", 0600, nil)
// Without nesting
c.Create("my-bucket", "my-key", []byte("my-value"))
// With nested buckets
c.Create("my-bucket", "my-key", []byte("my-value"), "bucket-one", "bucket-two")
// When you nest buckets, the order of the optional coma separated strings matter.
// That is, on the above code snippet.
// * A bucket named "my-bucket" will be created"
// * Inside "my-bucket" a bucket "bucket-one" will be created.
// * Inside "bucket-one" a bucket "bucket-two" will be created.
// * Inside "bucket-two" a new record will be stored with key "my-key" and value "my-value"
func (Storage) DeleteBucket ¶
DeleteBucket removes a bucket and all child buckets from the database
func (Storage) DeleteDatabase ¶
DeleteDatabase removes the given database file as specified in the NewStorage function from the filesystem
func (*Storage) Execute ¶
func (s *Storage) Execute(bucket, key string, value []byte, nested []string, fn StorageFunc) Storage
Execute opens a database, and pass all arguments together with a Storage object that has an open bolt database to a given StorageFunc. This is an ideal way to tap into the *bolt.DB object and do the whole stuffs you would like do do, without even bothering if the database exist of if the connection was closed.
It makes sure the database is closed after the function exits. Note that, the nested buckets should be provided as a slice of strings.
func (Storage) Get ¶
Get retrives a record from the database. The order of the optional nested buckets matter. if a key does not exist it returns an error
func (Storage) GetAll ¶
GetAll retrieves all key value pairs in a bucket, it stores the map of key value pairs inside the DataList attribute.
func (Storage) Update ¶
Update replaces the old data stored in a given key with a new one. If the Key is not found it returns an error. Update does not attempt to create missing buckets, instead it returns an error
This shares the same api as the Create method, except it returns an error if the record does not exist, and in case of buckets it returns a error if they dont exist.
For a nested bucket list, and missing bucket in the list, or missarrangement result in an error.
