Documentation ¶
Index ¶
- Variables
- type DB
- func (db *DB) Clean(sectionPath ...string) error
- func (db *DB) DirLocation(sectionPath ...string) string
- func (db *DB) FileLocation(sectionPath ...string) string
- func (db *DB) Get(destination interface{}, id string, sectionPath ...string) error
- func (db *DB) List(sectionPath ...string) []Record
- func (db *DB) Put(object interface{}, id string, sectionPath ...string) error
- func (db *DB) RemoveItem(id string, sectionPath ...string) error
- func (db *DB) Section(sectionPath ...string) *Section
- type Event
- type Record
- type RecordEvent
- type Section
- func (s *Section) Clean() error
- func (s *Section) Get(id string, object interface{}) error
- func (s *Section) List() []Record
- func (s *Section) Location() string
- func (s *Section) Notification() <-chan RecordEvent
- func (s *Section) Put(id string, object interface{}) error
- func (s *Section) RemoveItem(id string) error
- func (s *Section) StartNotification() error
- func (s *Section) StopNotification()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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) DirLocation ¶
DirLocation - get real directory (relative to Root) location of specified sections
func (*DB) FileLocation ¶
FileLocation - get real file (relative to Root) location of specified sections with format extension This function doesn't check file
func (*DB) Get ¶
Get signle item from filesystem and unmarshall it by JSON decoder. Decoder may be changed in future releases
func (*DB) RemoveItem ¶
RemoveItem - removes saved item from filesystem
func (*DB) 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
type Record ¶
type Record struct { Section []string Name string Subsection bool // contains filtered or unexported fields }
Record 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) 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) RemoveItem ¶
RemoveItem - remove one item in current section
func (*Section) StartNotification ¶
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)