jsondb

package module
v0.0.0-...-d88a979 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

jsondb

GoDoc Go Report Card

A tiny JSON database in Golang.

Forked from Steve Domino's Scribble.

I changed the name of the package because some of my changes are incompatible with the original source.

Installation

Add import "github.com/maloquacious/jsondb" to your file.

Usage

// a new driver, providing the directory where it will be writing to,
// and a qualified logger if desired
db, err := jsondb.New(dir)
if err != nil && !errors.Is(err, jsondb.ErrExists){
  fmt.Println("Error", err)
}

// Write a fish to the database
fish := Fish{}
if err := db.Write("fish", "onefish", fish); err != nil {
  fmt.Println("Error", err)
}

// Read a fish from the database (passing fish by reference)
onefish := Fish{}
if err := db.Read("fish", "onefish", &onefish); err != nil {
  fmt.Println("Error", err)
}

// Read all fish from the database, unmarshaling the response.
records, err := db.ReadAll("fish")
if err != nil {
  fmt.Println("Error", err)
}

fishies := []Fish{}
for _, f := range records {
  fishFound := Fish{}
  if err := json.Unmarshal([]byte(f), &fishFound); err != nil {
    fmt.Println("Error", err)
  }
  fishies = append(fishies, fishFound)
}

// Delete a fish from the database
if err := db.Delete("fish", "onefish"); err != nil {
  fmt.Println("Error", err)
}

// Delete all fish from the database
if err := db.Delete("fish", ""); err != nil {
  fmt.Println("Error", err)
}

Documentation

  • Complete documentation is available on godoc.

TODO

  • Support for windows
  • Better support for concurrency
  • Better support for sub collections
  • More methods to allow different types of reads/writes
  • More tests (you can never have enough!)

Documentation

Overview

Package jsondb implements a fork of Steve Domino's Scribble.

Index

Constants

View Source
const (
	ErrExists            = cerrors.Error("db already exists")
	ErrMissingCollection = cerrors.Error("missing collection - no place to save record")
	ErrMissingResource   = cerrors.Error("missing resource - unable to save record")
)
View Source
const Version = "2.0.0"

Version is the current version of the project

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger) func(*DB) error

Types

type DB

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

DB is what is used to interact with the JSONDB database. It runs transactions, and provides log output.

func New

func New(dir string, options ...Option) (*DB, error)

New creates a new database at the desired directory location, and returns a *DB to then use for interacting with the database.

func (*DB) Delete

func (db *DB) Delete(collection, resource string) error

Delete locks the database then attempts to remove the collection/resource specified by path

func (*DB) Read

func (db *DB) Read(collection, resource string, v any) error

Read a record from the database

func (*DB) ReadAll

func (db *DB) ReadAll(collection string) ([][]byte, error)

ReadAll records from a collection; this is returned as a slice of strings because there is no way of knowing what type the record is.

func (*DB) Write

func (db *DB) Write(collection, resource string, v any) error

Write locks the database and attempts to write the record to the database under the [collection] specified with the [resource] name given.

type Logger

type Logger interface {
	Fatal(format string, args ...any)
	Error(format string, args ...any)
	Warn(format string, args ...any)
	Info(format string, args ...any)
	Debug(format string, args ...any)
	Trace(format string, args ...any)
}

Logger is a generic logger interface.

type Option

type Option func(db *DB) error

Directories

Path Synopsis
Package cerrors implements constant error wrappers from Cheney.
Package cerrors implements constant error wrappers from Cheney.
Package frogger implements a very simple logger.
Package frogger implements a very simple logger.

Jump to

Keyboard shortcuts

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