filedb

package module
v0.0.0-...-58d4b60 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2016 License: MIT Imports: 10 Imported by: 0

README

File based database

See API and examples here https://godoc.org/github.com/reddec/filedb

Main features:

1. File based and human readable

All items are saved as single JSON (may be changed in future) file. Tables (in terms of RDB) is just sub-folder. Names encoded by URL encoding. So, any administrator can fix and update item just using favorite text editor.

2. Events and reactive design

You can manipulate with items outside application any time. Application may receive notification about any changes in sections.

3. Simple

Really simple structure of database engine in really simple language - Go. So pull requests are welcome

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotItem = errors.New("Not an item")

Record is not pointed to item

Functions

This section is empty.

Types

type DB

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

DB is file based database. Sections - level of folders. Items - JSON files. Names automatically url-encoded.

Example
// Create new database located to /tmp/example-db directory
db := DB{Root: "/tmp/example-db"}
// Create new item with ID = 001 into root section
err := db.Put("Hello world", "001")
if err != nil {
	panic(err)
}
// Complex item also supported
// it will be serialized by JSON encoder (may be changed in future)
var user struct {
	Name string
	Year int
}
user.Name = "Alex"
user.Year = 1900
err = db.Put(user, "002")
if err != nil {
	panic(err)
}

// Get item by ID in root section
var item string
err = db.Get(&item, "001")
if err != nil {
	panic(err)
}
fmt.Println(item)
// ... or get list of all items and subsections (only id's)
// db.List()

// Remove item by ID in root section
err = db.RemoveItem("001")
if err != nil {
	panic(err)
}
Output:

Hello world

func (*DB) Clean

func (db *DB) Clean(sectionPath ...string) error

Clean sectoions - remove all subsections and saved items

func (*DB) DirLocation

func (db *DB) DirLocation(sectionPath ...string) string

DirLocation - get real directory (relative to Root) location of specified sections

func (*DB) FileLocation

func (db *DB) FileLocation(sectionPath ...string) string

FileLocation - get real file (relative to Root) location of specified sections with format extension This function doesn't check file

func (*DB) Get

func (db *DB) Get(destination interface{}, id string, sectionPath ...string) error

Get signle item from filesystem and unmarshall it by JSON decoder. Decoder may be changed in future releases

func (*DB) List

func (db *DB) List(sectionPath ...string) []Record

List all saved records (items and subsections) in specified section

func (*DB) Put

func (db *DB) Put(object interface{}, id string, sectionPath ...string) error

Put or update item

func (*DB) RemoveItem

func (db *DB) RemoveItem(id string, sectionPath ...string) error

RemoveItem - removes saved item from filesystem

func (*DB) Section

func (db *DB) Section(sectionPath ...string) *Section

Section - encapsulate section to single object and provide notifications

Example
// Create new database located to /tmp/example-db directory
db := DB{Root: "/tmp/example-db"}
// For example, we have to save users separated by country
// We can do it directly
// Here: ID = 001, section = europe/germany
err := db.Put("Merkel", "001", "europe", "germany")
if err != nil {
	panic(err)
}
// Also we can use subsection
germany := db.Section("europe", "germany")
// Now add more people to Germany
if err := germany.Put("002", "Bismark"); err != nil {
	panic(err)
}
if err := germany.Put("003", "Kant"); err != nil {
	panic(err)
}
// And get list of people in Germany
for _, record := range germany.List() {
	// Get people
	var user string
	err := record.Get(&user)
	if err != nil {
		panic(err)
	}
	fmt.Print(user, " ")
}
Output:

Merkel Bismark Kant

type Event

type Event int

Event type of record's changes

const (
	Update Event = iota // Record content changed
	Remove              //Record removed
	Create              //Record created or copied
)

Record change events

func (Event) String

func (ev Event) String() string

type Record

type Record struct {
	Section    []string
	Name       string
	Subsection bool
	// contains filtered or unexported fields
}

Record in database

func (*Record) Get

func (rec *Record) Get(target interface{}) error

Get content of record. Returns ErrNotItem if record is subsection

func (*Record) Remove

func (rec *Record) Remove() error

Remove content of record. Returns ErrNotItem if record is subsection

func (*Record) Update

func (rec *Record) Update(target interface{}) error

Update content of record. Returns ErrNotItem if record is subsection

type RecordEvent

type RecordEvent struct {
	Record
	Event Event
}

RecordEvent in database

type Section

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

Section in database. Wrapper for Get/Put/Remove/List operations and provides notifications

func (*Section) Clean

func (s *Section) Clean() error

Clean section

func (*Section) Get

func (s *Section) Get(id string, object interface{}) error

Get single item in current item

func (*Section) List

func (s *Section) List() []Record

List subsections and items in current section

func (*Section) Location

func (s *Section) Location() string

Location in real filesystem

func (*Section) Notification

func (s *Section) Notification() <-chan RecordEvent

Notification channel. Will be created after StartNotification()

Example
// One of primary benefits of subsection it events
// Create new database located to /tmp/example-db directory
db := DB{Root: "/tmp/example-db"}
germany := db.Section("europe", "germany")
// Create notification listener
err := germany.StartNotification()
if err != nil {
	panic(err)
}
defer germany.StopNotification()
// Now we can listen Create/Update/Remove events
for eventRecord := range germany.Notification() {
	// We can operate with items even they are changed outside application
	fmt.Println(eventRecord.Event)
}
Output:

func (*Section) Put

func (s *Section) Put(id string, object interface{}) error

Put or update item in current item

func (*Section) RemoveItem

func (s *Section) RemoveItem(id string) error

RemoveItem - remove one item in current section

func (*Section) StartNotification

func (s *Section) StartNotification() error

StartNotification - create notification channel and starts listen to any file changes in current section (not in subsections).

func (*Section) StopNotification

func (s *Section) StopNotification()

StopNotification - stops notification (if it created)

Jump to

Keyboard shortcuts

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