Documentation
¶
Overview ¶
Package clerk provides file persistence for your Go structures.
It can replace external databases for small projects that can keep data in memory and don't make a lot of writes.
Example ¶
package main
import "github.com/arthurwhite/clerk"
type User struct {
Name string
}
var db = new(struct {
clerk.DB
Users []*User
})
func init() {
clerk.Init("example.gob", db)
}
func main() {
db.Lock()
defer db.Unlock()
db.Users = append(db.Users, &User{"Crowe"}, &User{"Jones"}, &User{"Owen"})
if err := db.Save(); err != nil {
panic(err)
}
}
Index ¶
- Variables
- func Init(filename string, db DBInterface)
- type DB
- func (db *DB) Lock()
- func (db *DB) Migrate(timestamp string, oldDB DBInterface, do func() error) error
- func (db *DB) MigrateOrRebase(timestamp string, oldDB DBInterface, do func() error)
- func (db *DB) RLock()
- func (db *DB) RUnlock()
- func (db *DB) Rebase() error
- func (db *DB) Remove() error
- func (db *DB) Save() error
- func (db *DB) Touch()
- func (db *DB) Unlock()
- type DBInterface
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDBInited = errors.New("clerk: database inited multiple times") ErrDBNotInited = errors.New("clerk: database not inited") ErrOldMigration = errors.New("clerk: database in file is newer than migration timestamp") )
Errors.
Functions ¶
Types ¶
type DB ¶
type DB struct {
UpdatedAt time.Time // UpdatedAt is the timestamp of the last data modification (by calling DB.Lock).
// contains filtered or unexported fields
}
DB provides methods to persist your data.
func (*DB) Migrate ¶
func (db *DB) Migrate(timestamp string, oldDB DBInterface, do func() error) error
Migrate runs a migration if provided date is after the one registered in the data file. It rebases the database file in oldDB, runs function do and saves database. timestamp format is:
2006-01-02 15:04:05 -07
func (*DB) MigrateOrRebase ¶
func (db *DB) MigrateOrRebase(timestamp string, oldDB DBInterface, do func() error)
MigrateOrRebase runs Migrate. If the migration has not been used, database is rebased. It panics on error.
func (*DB) Rebase ¶
Rebase replaces the data with the content of the file set on init. Be sure the database is locked for writing.
type DBInterface ¶
type DBInterface interface {
Touch()
Save() error
Migrate(string, DBInterface, func() error) error
MigrateOrRebase(string, DBInterface, func() error)
Rebase() error
Remove() error
Lock()
RLock()
Unlock()
RUnlock()
// contains filtered or unexported methods
}
DBInterface represents an underlying database.