Skip to content

Commit

Permalink
Merge pull request #40 from w-h-a/store-seed
Browse files Browse the repository at this point in the history
feat: add seed option for store
  • Loading branch information
w-h-a committed Jul 8, 2024
2 parents cd315fc + a3c98f3 commit c408541
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions store/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/patrickmn/go-cache"
"github.com/w-h-a/pkg/store"
"github.com/w-h-a/pkg/telemetry/log"
)

type memoryStore struct {
Expand Down Expand Up @@ -193,5 +194,13 @@ func NewStore(opts ...store.StoreOption) store.Store {
store: cache.New(cache.NoExpiration, 5*time.Minute),
}

if len(options.Seed) != 0 {
for _, rec := range options.Seed {
if err := s.Write(rec); err != nil {
log.Fatalf("failed to seed database: %v", err)
}
}
}

return s
}
7 changes: 7 additions & 0 deletions store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type StoreOptions struct {
Nodes []string
Database string
Table string
Seed []*Record
Context context.Context
}

Expand All @@ -31,6 +32,12 @@ func StoreWithTable(tbl string) StoreOption {
}
}

func StoreWithSeed(rs ...*Record) StoreOption {
return func(o *StoreOptions) {
o.Seed = append(o.Seed, rs...)
}
}

func NewStoreOptions(opts ...StoreOption) StoreOptions {
options := StoreOptions{
Context: context.Background(),
Expand Down

0 comments on commit c408541

Please sign in to comment.