db

package
v0.0.0-...-a7413d3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckService

func CheckService(ser Service) error

CheckService returns an error if the given service or its DB are nil.

Types

type BoltDatabase

type BoltDatabase struct {
	Bolt         *bolt.DB
	Buckets      []string
	ClearOnClose bool
}

BoltDatabase implements Database for boltDB.

func ConnectBoltDatabase

func ConnectBoltDatabase(conf *BoltDatabaseConfig) (*BoltDatabase, error)

ConnectBoltDatabase connects to the database file at the given path and returns a new BoltDatabase pointer.

func (*BoltDatabase) Bucket

func (db *BoltDatabase) Bucket(name string, tx Tx) (*bolt.Bucket, error)

Bucket returns the bucket with the given name

func (*BoltDatabase) Clear

func (db *BoltDatabase) Clear() error

Clear removes all buckets in the given database.

func (*BoltDatabase) Close

func (db *BoltDatabase) Close() error

Close closes the database connection.

func (*BoltDatabase) Create

func (db *BoltDatabase) Create(m Model, ser Service, tx Tx) (int, error)

Create persists the given Model.

func (*BoltDatabase) Delete

func (db *BoltDatabase) Delete(id int, ser Service, tx Tx) error

Delete deletes the model with the given ID.

func (*BoltDatabase) DoEach

func (db *BoltDatabase) DoEach(first *int, skip *int, ser Service, tx Tx,
	do func(Model, Service, Tx) (exit bool, err error), iff func(Model) bool) error

DoEach unmarshals and performs some function on each persisted element that passes the filter function.

func (*BoltDatabase) DoMultiple

func (db *BoltDatabase) DoMultiple(ids []int, ser Service, tx Tx,
	do func(Model, Service, Tx) (exit bool, err error), iff func(Model) bool) error

DoMultiple unmarshals and performs some function on the persisted elements that pass the given filter function specified by the given IDs.

func (*BoltDatabase) FindFirst

func (db *BoltDatabase) FindFirst(
	ser Service, tx Tx, match func(Model) (bool, error)) (Model, error)

FindFirst returns the first element that matches the conditions in the given function. Elements are iterated through in key order.

func (*BoltDatabase) GetByID

func (db *BoltDatabase) GetByID(id int, ser Service, tx Tx) (Model, error)

GetByID retrieves the persisted Model with the given ID. The given service and its DB should not be nil.

func (*BoltDatabase) GetRawByID

func (db *BoltDatabase) GetRawByID(id int, ser Service, tx Tx) ([]byte, error)

GetRawByID is a generic function that queries the given bucket in the given database for an entity of the given ID. The given DB pointer should not be nil.

func (*BoltDatabase) Transaction

func (db *BoltDatabase) Transaction(writable bool, logic func(Tx) error) error

Transaction is a wrapper method that begins a transaction and passes it to the given function.

func (*BoltDatabase) Update

func (db *BoltDatabase) Update(m Model, ser Service, tx Tx) error

Update replaces the value of the model with the given ID.

type BoltDatabaseConfig

type BoltDatabaseConfig struct {
	Path         string
	FileMode     os.FileMode
	Buckets      []string
	ClearOnClose bool
}

BoltDatabaseConfig defines a set of options to be passed when opening a boltDB instance.

type BoltTx

type BoltTx struct {
	DB *DatabaseService
	Tx *bolt.Tx
}

BoltTx implements Transaction for boltDB.

func (*BoltTx) Database

func (btx *BoltTx) Database() *DatabaseService

Database returns the database of the transaction.

func (*BoltTx) Unwrap

func (btx *BoltTx) Unwrap() interface{}

Unwrap returns the boltDB transaction object.

type DatabaseDriver

type DatabaseDriver interface {
	Transaction(writable bool, logic func(Tx) error) error
	Close() error

	DoMultiple(ids []int, ser Service, tx Tx,
		do func(Model, Service, Tx) (exit bool, err error), iff func(Model) bool) error
	DoEach(first *int, skip *int, ser Service, tx Tx,
		do func(Model, Service, Tx) (exit bool, err error), iff func(Model) bool) error
	FindFirst(ser Service, tx Tx, match func(Model) (exit bool, err error)) (Model, error)

	Create(m Model, ser Service, tx Tx) (int, error)
	Update(m Model, ser Service, tx Tx) error
	// Delete marks the model with the given ID as deleted.
	Delete(id int, ser Service, tx Tx) error
	GetByID(id int, ser Service, tx Tx) (Model, error)
	GetRawByID(id int, ser Service, tx Tx) ([]byte, error)
}

DatabaseDriver defines generic CRUD logic for a database backend.

type DatabaseService

type DatabaseService struct {
	DatabaseDriver
}

DatabaseService provides

func (*DatabaseService) Create

func (dbs *DatabaseService) Create(m Model, ser Service, tx Tx) (int, error)

Create persists a new instance of a Model type.

func (*DatabaseService) Delete

func (dbs *DatabaseService) Delete(id int, ser Service, tx Tx) error

Delete deletes an existing persisted instance of a Model type.

func (*DatabaseService) DeleteFilter

func (dbs *DatabaseService) DeleteFilter(ser Service, tx Tx,
	iff func(Model) bool) error

DeleteFilter deletes all the persisted instances of a Model type that pass the filer function.

func (*DatabaseService) DeleteMultiple

func (dbs *DatabaseService) DeleteMultiple(ids []int, first *int,
	ser Service, tx Tx, iff func(Model) bool) error

DeleteMultiple deletes the existing persisted instances of a Model type specified by the given IDs.

func (*DatabaseService) GetAll

func (dbs *DatabaseService) GetAll(first *int, skip *int, ser Service, tx Tx) ([]Model, error)

GetAll retrieves all persisted instances of a Model type with the given data layer service.

See GetFilter for details on `first` and `skip`.

func (*DatabaseService) GetFilter

func (dbs *DatabaseService) GetFilter(first *int, skip *int, ser Service, tx Tx,
	keep func(m Model) bool) ([]Model, error)

GetFilter retrieves all persisted instances of a Model type that pass the filter.

Collection begins on the first valid element after skipping the `skip` valid elements and continues for `first` valid elements that pass the filter. If `skip` is given as nil, collection begins with the first valid element. If `first` is given as nil, collection continues until the last persisted element is queried. The given service and its DB should not be nil. A nil filter function passes all.

func (*DatabaseService) GetMultiple

func (dbs *DatabaseService) GetMultiple(ids []int, ser Service, tx Tx,
	keep func(m Model) bool) ([]Model, error)

GetMultiple retrieves the persisted instances of a Model type with the given IDs.

See GetFilter for details on `first` and `skip`.

func (*DatabaseService) Update

func (dbs *DatabaseService) Update(m Model, ser Service, tx Tx) error

Update modifies an existing instance of a Model type.

type Model

type Model interface {
	Metadata() *ModelMetadata
}

Model encompasses all data models.

type ModelMetadata

type ModelMetadata struct {
	ID        int
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
	Version   int
}

ModelMetadata contains information about

type PersistHookFunc

type PersistHookFunc = func(m Model, ser Service, tx Tx) error

PersistHookFunc is a callback used as a hook function.

type PersistHooks

type PersistHooks struct {
	PreCreateHooks  []PersistHookFunc
	PostCreateHooks []PersistHookFunc
	PreUpdateHooks  []PersistHookFunc
	PostUpdateHooks []PersistHookFunc
	PreDeleteHooks  []PersistHookFunc
	PostDeleteHooks []PersistHookFunc
}

PersistHooks provides hook functions to be called before and after service operations.

func (*PersistHooks) PostCreateHook

func (hooks *PersistHooks) PostCreateHook(m Model, ser Service, tx Tx) error

PostCreateHook executes all hook functions designated to be called after create operations.

func (*PersistHooks) PostDeleteHook

func (hooks *PersistHooks) PostDeleteHook(m Model, ser Service, tx Tx) error

PostDeleteHook executes all hook functions designated to be called after delete operations.

func (*PersistHooks) PostUpdateHook

func (hooks *PersistHooks) PostUpdateHook(m Model, ser Service, tx Tx) error

PostUpdateHook executes all hook functions designated to be called after update operations.

func (*PersistHooks) PreCreateHook

func (hooks *PersistHooks) PreCreateHook(m Model, ser Service, tx Tx) error

PreCreateHook executes all hook functions designated to be called before create operations.

func (*PersistHooks) PreDeleteHook

func (hooks *PersistHooks) PreDeleteHook(m Model, ser Service, tx Tx) error

PreDeleteHook executes all hook functions designated to be called before delete operations.

func (*PersistHooks) PreUpdateHook

func (hooks *PersistHooks) PreUpdateHook(m Model, ser Service, tx Tx) error

PreUpdateHook executes all hook functions designated to be called before update operations.

type Service

type Service interface {
	Bucket() string

	Clean(m Model, tx Tx) error
	Validate(m Model, tx Tx) error
	Initialize(m Model, tx Tx) error
	PersistOldProperties(n Model, o Model, tx Tx) error

	PersistHooks() *PersistHooks

	Marshal(m Model) ([]byte, error)
	Unmarshal(buf []byte) (Model, error)
}

Service provides various functions to operate on Models. All implementations should use type assertions to guarantee prevention of runtime errors.

type Tx

type Tx interface {
	Database() *DatabaseService
	Unwrap() interface{}
}

Tx defines a wrapper for database transactions objects.

Jump to

Keyboard shortcuts

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