scribble

package module
v4.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2018 License: MPL-2.0 Imports: 7 Imported by: 0

README

Scribble (FireScribble Edition) GoDoc Go Report Card Build Status

A tiny GOB based database in Golang - behaviour is very similar to Google Cloud Firestore

Decode into reflect.Value is also supported

Note If you would rather use JSON instad of GOB please use a version prior to 3.0.0. You will have less functionality and a slower db but it will be human readable.

Installation

Install using go get github.com/creativeguy2013/scribble.

Usage

// a new scribble document, providing the directory where it will be writing to
db, err := scribble.New(dir)
if err != nil {
  fmt.Println("Error", err)
}

// open a collection from the base document
fishCollection := db.Collection("fish")

// open the document we want to write to
onefishDocument := fishCollection.Document("onefish")

// write the data to the document
fish := Fish{}
if err := onefishDocument.Write(fish); err != nil {
  fmt.Println("Error", err)
}

// Read a data from the database
onefish := Fish{}
if err := onefishDocument.Read(&onefish); err != nil {
  fmt.Println("Error", err)
}

// Read all fish from the database, returning an array of documents.
records, err := fishCollection.GetAlDocuments()
if err != nil {
  fmt.Println("Error", err)
}

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

// Read a select view of the fish from the database, in this case everything from index 1 to 3
records, err = fishCollection.GetDocuments(1, 3)
if err != nil {
	fmt.Println("Error 5", err)
}
// records has length 2
fmt.Println(len(records))

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

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

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

// Make a subcollection in a document
fishBabiesCollection := onefishDocument.Collection("babies")

// Make a make a document in a collection
firstbabyDocument := Document("firstbaby")

It is also possible to store a subcollection and data in the same document:

starFish := db.Collection("fish").Document("starFish")

starFish.Write(map[string]bool{
  "isAwesome": true,
})

starFish.Collection("properties").Document("arms").Write(6)

Documentation

  • Complete documentation is available on godoc.
  • Coverage Report is available on gocover

Todo/Doing

  • Support for windows
  • More methods to allow different types of reads/writes
  • More tests (you can never have enough!)
  • loading part into memory/caching

Documentation

Overview

Package scribble is a tiny gob database

Index

Constants

View Source
const Version = "4.4.0"

Version is the current version of the project

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

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

Collection a collection of documents

func (*Collection) Check

func (c *Collection) Check() (bool, error)

Check if there is an error while getting the collection

func (*Collection) Delete

func (c *Collection) Delete() error

Delete removes a collection and all of its childeren

func (*Collection) Document

func (c *Collection) Document(key string) *Document

Document gets a document from a collection

func (*Collection) GetAllDocuments

func (c *Collection) GetAllDocuments() ([]*Document, error)

GetAllDocuments gets all documents in a collection.

func (*Collection) GetDocuments

func (c *Collection) GetDocuments(start, end int) ([]*Document, error)

GetDocuments gets documents in a collection starting from start til end, if start

func (*Collection) PreGen

func (c *Collection) PreGen() (bool, error)

PreGen does a check to see if there is an error while getting the collection and make them if they dont exist yet.

type Document

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

Document a single document which can have sub collections

func New

func New(dir string) (*Document, error)

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

func (*Document) Check

func (d *Document) Check() (bool, error)

Check if there is an error while getting the document

func (*Document) Collection

func (d *Document) Collection(name string) *Collection

Collection gets a collction from in a document

func (*Document) Delete

func (d *Document) Delete() error

Delete locks that database and removes the document including all of its sub documents

func (*Document) PreGen

func (d *Document) PreGen() (bool, error)

PreGen does a check to see if there is an error while getting the documents and make them if they dont exist yet.

func (*Document) Read

func (d *Document) Read(v interface{}) error

Read a record from the database

func (*Document) Write

func (d *Document) Write(v interface{}) error

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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