mobility-accounts/storage/storage.go

77 lines
1.5 KiB
Go
Raw Normal View History

package storage
import (
"fmt"
"time"
"github.com/spf13/viper"
)
// Storage interface
type Storage struct {
DB DBStorage
KV KVStore
}
func NewStorage(cfg *viper.Viper) (Storage, error) {
dbstorage, err := NewDBStorage(cfg)
if err != nil {
return Storage{}, err
}
kvstore, err := NewKVStore(cfg)
if err != nil {
return Storage{}, err
}
return Storage{
DB: dbstorage,
KV: kvstore,
}, nil
}
type DBStorage interface {
GetAccount(id string) (*Account, error)
LocalAuthentication(namespace string, username *string, email *string, phone_number *string) (*Account, error)
GetAccounts(namespaces []string) ([]Account, error)
2022-08-11 15:14:21 +00:00
GetAccountsByIds(accountids []string) ([]Account, error)
CreateAccount(account Account) error
//TODO : remove UpdateAccount, implement UpdateAccountData and UpdateAccountLocalAuthentication
UpdateAccount(account Account) error
Migrate() error
}
func NewDBStorage(cfg *viper.Viper) (DBStorage, error) {
var (
storage_type = cfg.GetString("storage.db.type")
)
switch storage_type {
case "mongodb":
s, err := NewMongoDBStorage(cfg)
return s, err
2023-04-25 12:14:42 +00:00
case "psql":
s, err := NewPostgresqlStorage(cfg)
return s, err
default:
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
}
}
type KVStore interface {
Put(string, any) error
PutWithTTL(string, any, time.Duration) error
Get(string) (any, error)
Delete(string) error
}
func NewKVStore(cfg *viper.Viper) (KVStore, error) {
kv, err := NewEtcdKVStore(cfg)
return kv, err
}
func Ptr[T any](v T) *T {
return &v
}