Initial commit
This commit is contained in:
9
internal/storm/error.go
Normal file
9
internal/storm/error.go
Normal file
@ -0,0 +1,9 @@
|
||||
package storm
|
||||
|
||||
import (
|
||||
"github.com/asdine/storm/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = storm.ErrNotFound
|
||||
)
|
51
internal/storm/option.go
Normal file
51
internal/storm/option.go
Normal file
@ -0,0 +1,51 @@
|
||||
package storm
|
||||
|
||||
type Option struct {
|
||||
Path string
|
||||
Objects []interface{}
|
||||
ReIndex bool
|
||||
Init bool
|
||||
}
|
||||
|
||||
type OptionFunc func(*Option)
|
||||
|
||||
func DefaultOption() *Option {
|
||||
return MergeOption(
|
||||
&Option{},
|
||||
WithPath("data.db"),
|
||||
WithInit(true),
|
||||
WithReIndex(true),
|
||||
)
|
||||
}
|
||||
|
||||
func MergeOption(opt *Option, funcs ...OptionFunc) *Option {
|
||||
for _, fn := range funcs {
|
||||
fn(opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
func WithPath(path string) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.Path = path
|
||||
}
|
||||
}
|
||||
|
||||
func WithReIndex(reindex bool) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.ReIndex = reindex
|
||||
}
|
||||
}
|
||||
|
||||
func WithInit(init bool) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.Init = init
|
||||
}
|
||||
}
|
||||
|
||||
func WithObjects(objects ...interface{}) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.Objects = objects
|
||||
}
|
||||
}
|
48
internal/storm/provider.go
Normal file
48
internal/storm/provider.go
Normal file
@ -0,0 +1,48 @@
|
||||
package storm
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
func ServiceProvider(funcs ...OptionFunc) service.Provider {
|
||||
opt := MergeOption(
|
||||
DefaultOption(),
|
||||
funcs...,
|
||||
)
|
||||
|
||||
db, err := storm.Open(opt.Path)
|
||||
|
||||
if err == nil && opt.Objects != nil {
|
||||
err = migrate(db, opt.Objects, opt.Init, opt.ReIndex)
|
||||
}
|
||||
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
}
|
||||
|
||||
func migrate(db *storm.DB, objects []interface{}, init, reindex bool) error {
|
||||
for _, o := range objects {
|
||||
if init {
|
||||
if err := db.Init(o); err != nil {
|
||||
return errors.Wrapf(err, "could not init object '%s'", reflect.TypeOf(o).String())
|
||||
}
|
||||
}
|
||||
|
||||
if reindex {
|
||||
if err := db.ReIndex(o); err != nil {
|
||||
return errors.Wrapf(err, "could not reindex object '%s'", reflect.TypeOf(o).String())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
34
internal/storm/service.go
Normal file
34
internal/storm/service.go
Normal file
@ -0,0 +1,34 @@
|
||||
package storm
|
||||
|
||||
import (
|
||||
"github.com/asdine/storm/v3"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
const ServiceName service.Name = "storm"
|
||||
|
||||
// From retrieves the storm service in the given container
|
||||
func From(container *service.Container) (*storm.DB, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*storm.DB)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the storm service in the given container or panic otherwise
|
||||
func Must(container *service.Container) *storm.DB {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
Reference in New Issue
Block a user