datastore

package
v0.0.5-dev.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: MIT Imports: 17 Imported by: 3

Documentation

Index

Constants

View Source
const FileSytemBinaryStoreID = "file-system"
View Source
const InMemoryinaryStoreID = "memory"

Variables

View Source
var BinaryDataStoreProviders = cloudy.NewProviderRegistry[BinaryDataStore]()
View Source
var ErrInvalidConfiguration = errors.New("invalid configuration object")
View Source
var IndexerProviders = cloudy.NewProviderRegistry[Indexer[any]]()
View Source
var PrefixSeparator = "-"
View Source
var RootFSDir = ""

Functions

func BinaryDataStoreTest added in v0.0.2

func BinaryDataStoreTest(t *testing.T, ctx context.Context, ds BinaryDataStore)

func JsonDataStoreTest added in v0.0.2

func JsonDataStoreTest(t *testing.T, ctx context.Context, ds JsonDataStore[TestItem])

func QueryJsonDataStoreTest added in v0.0.2

func QueryJsonDataStoreTest(t *testing.T, ctx context.Context, ds JsonDataStore[TestQueryItem])

func RegisterJsonDatastore

func RegisterJsonDatastore(providerName string, factory JsonDataStoreFactory)

Types

type AfterGetInterceptor

type AfterGetInterceptor[T any] interface {
	AfterGet(ctx context.Context, dt *Datatype[T], item *T) (*T, error)
}

type AfterSaveInterceptor

type AfterSaveInterceptor[T any] interface {
	AfterSave(ctx context.Context, dt *Datatype[T], item *T) (*T, error)
}

type BeforeGetInterceptor

type BeforeGetInterceptor[T any] interface {
	BeforeGet(ctx context.Context, dt *Datatype[T], ID string) (string, error)
}

type BeforeSaveInterceptor

type BeforeSaveInterceptor[T any] interface {
	BeforeSave(ctx context.Context, dt *Datatype[T], item *T) (*T, error)
}

type BinaryDataStore

type BinaryDataStore interface {
	Open(ctx context.Context, config interface{}) error
	Close(ctx context.Context) error

	Save(ctx context.Context, data []byte, key string) error
	SaveStream(ctx context.Context, data io.ReadCloser, key string) (int64, error)
	Get(ctx context.Context, key string) ([]byte, error)
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) (bool, error)
}

type Datatype

type Datatype[T any] struct {
	Name      string
	Prefix    string
	IDField   string
	GetIDFunc func(dt *Datatype[T], item *T) string
	SetIDFunc func(dt *Datatype[T], item *T, id string) string

	DataStore JsonDataStore[T]
	Indexer   Indexer[T]
	ItemType  interface{}

	BeforeSave []BeforeSaveInterceptor[T]
	AfterSave  []AfterSaveInterceptor[T]
	BeforeGet  []BeforeGetInterceptor[T]
	AfterGet   []AfterGetInterceptor[T]
	// contains filtered or unexported fields
}

func (*Datatype[T]) Delete

func (dt *Datatype[T]) Delete(ctx context.Context, key string) error

func (*Datatype[T]) Exists

func (dt *Datatype[T]) Exists(ctx context.Context, id string) (bool, error)

func (*Datatype[T]) GenerateID

func (dt *Datatype[T]) GenerateID() string

func (*Datatype[T]) Get

func (dt *Datatype[T]) Get(ctx context.Context, ID string) (*T, error)

func (*Datatype[T]) GetAll

func (dt *Datatype[T]) GetAll(ctx context.Context) ([]*T, error)

func (*Datatype[T]) GetID

func (dt *Datatype[T]) GetID(ctx context.Context, item *T) string

func (*Datatype[T]) GetRaw

func (dt *Datatype[T]) GetRaw(ctx context.Context, ID string) (*T, error)

Retrieves the Raw Data from the data store and then attempts to unmarshall it using reflection

func (*Datatype[T]) Initialize

func (dt *Datatype[T]) Initialize(ctx context.Context) error

func (*Datatype[T]) NativeQuery

func (dt *Datatype[T]) NativeQuery(ctx context.Context, query interface{}) ([]T, error)

func (*Datatype[T]) Query

func (dt *Datatype[T]) Query(ctx context.Context, query *SimpleQuery) ([]*T, error)

func (*Datatype[T]) Save

func (dt *Datatype[T]) Save(ctx context.Context, item *T) (*T, error)

func (*Datatype[T]) SaveRaw

func (dt *Datatype[T]) SaveRaw(ctx context.Context, item *T) (*T, error)

func (*Datatype[T]) SetID

func (dt *Datatype[T]) SetID(ctx context.Context, item *T, id string)

func (*Datatype[T]) Shutdown

func (dt *Datatype[T]) Shutdown(ctx context.Context) error

type DatatypeCollection

type DatatypeCollection struct {
	// contains filtered or unexported fields
}
var Datatypes *DatatypeCollection

func NewDatatypeCollection

func NewDatatypeCollection() *DatatypeCollection

func (*DatatypeCollection) Add

func (dtc *DatatypeCollection) Add(dt *Datatype[any]) error

func (*DatatypeCollection) FindByName

func (dtc *DatatypeCollection) FindByName(name string) *Datatype[any]

func (*DatatypeCollection) FindByPrefix

func (dtc *DatatypeCollection) FindByPrefix(prefix string) *Datatype[any]

func (*DatatypeCollection) FindForId

func (dtc *DatatypeCollection) FindForId(id string) *Datatype[any]

func (*DatatypeCollection) Initialize

func (dtc *DatatypeCollection) Initialize(ctx context.Context) error

func (*DatatypeCollection) Shutdown

func (dtc *DatatypeCollection) Shutdown(ctx context.Context)

type FilesystemStore

type FilesystemStore struct {
	Dir   string
	Ext   string
	Perms os.FileMode
}

func NewFilesystemStore

func NewFilesystemStore(ext string, dir ...string) *FilesystemStore

func (*FilesystemStore) Close

func (fs *FilesystemStore) Close(ctx context.Context) error

func (*FilesystemStore) Delete

func (fs *FilesystemStore) Delete(ctx context.Context, key string) error

func (*FilesystemStore) Exists

func (fs *FilesystemStore) Exists(ctx context.Context, key string) (bool, error)

func (*FilesystemStore) Get

func (fs *FilesystemStore) Get(ctx context.Context, key string) ([]byte, error)

func (*FilesystemStore) Init

func (fs *FilesystemStore) Init() error

func (*FilesystemStore) Open

func (fs *FilesystemStore) Open(ctx context.Context, config interface{}) error

func (*FilesystemStore) Save

func (fs *FilesystemStore) Save(ctx context.Context, data []byte, key string) error

func (*FilesystemStore) SaveStream

func (fs *FilesystemStore) SaveStream(ctx context.Context, data io.ReadCloser, key string) (int64, error)

type FilesystemStoreConfig added in v0.0.2

type FilesystemStoreConfig struct {
	Dir   string
	Ext   string
	Perms os.FileMode
}

type FilesystemStoreFactory

type FilesystemStoreFactory struct{}

func (*FilesystemStoreFactory) Create

func (f *FilesystemStoreFactory) Create(cfg interface{}) (BinaryDataStore, error)

func (*FilesystemStoreFactory) FromEnv

func (f *FilesystemStoreFactory) FromEnv(env *cloudy.Environment) (interface{}, error)

type Filetype

type Filetype struct {
}

Handles Raw Files (including Images)

type IdGenerator

type IdGenerator[T any] struct{}

func (*IdGenerator[T]) BeforeSave

func (idgen *IdGenerator[T]) BeforeSave(ctx context.Context, dt *Datatype[T], item *T) (*T, error)

type InMemoryStore

type InMemoryStore struct {
	// contains filtered or unexported fields
}

func NewInMemoryStore

func NewInMemoryStore() *InMemoryStore

func (*InMemoryStore) Close

func (mem *InMemoryStore) Close(ctx context.Context) error

func (*InMemoryStore) Delete

func (mem *InMemoryStore) Delete(ctx context.Context, key string) error

func (*InMemoryStore) Exists

func (mem *InMemoryStore) Exists(ctx context.Context, key string) (bool, error)

func (*InMemoryStore) Get

func (mem *InMemoryStore) Get(ctx context.Context, key string) ([]byte, error)

func (*InMemoryStore) Open

func (mem *InMemoryStore) Open(ctx context.Context, config interface{}) error

func (*InMemoryStore) Save

func (mem *InMemoryStore) Save(ctx context.Context, data []byte, key string) error

func (*InMemoryStore) SaveStream

func (mem *InMemoryStore) SaveStream(ctx context.Context, data io.ReadCloser, key string) (int64, error)

type InMemoryStoreFactory

type InMemoryStoreFactory struct{}

func (*InMemoryStoreFactory) Create

func (f *InMemoryStoreFactory) Create(cfg interface{}) (BinaryDataStore, error)

func (*InMemoryStoreFactory) FromEnv

func (f *InMemoryStoreFactory) FromEnv(env *cloudy.Environment) (interface{}, error)

type Indexer

type Indexer[T any] interface {
	Open(ctx context.Context, config interface{}) error
	Close(ctx context.Context) error

	Index(ctx context.Context, id string, data []byte) error
	Remove(ctx context.Context, id string) error
	Search(ctx context.Context, query interface{}) ([]T, error)
}

type JsonDataStore

type JsonDataStore[T any] interface {

	// Open will open the datastore for usage. This should
	// only be done once per datastore
	Open(ctx context.Context, config interface{}) error

	// Close should be called to cleannly close the datastore
	Close(ctx context.Context) error

	// Save stores an item in the datastore. There is no difference
	// between an insert and an update.
	Save(ctx context.Context, item *T, key string) error

	// Get retrieves an item by it's unique id
	Get(ctx context.Context, key string) (*T, error)

	// Gets all the items in the store.
	GetAll(ctx context.Context) ([]*T, error)

	// Deletes an item
	Delete(ctx context.Context, key string) error

	// Checks to see if a key exists
	Exists(ctx context.Context, key string) (bool, error)

	// Sends a simple Query
	Query(ctx context.Context, query *SimpleQuery) ([]*T, error)
}

JsonDataStore stores data structures as JSON. The type argument can be any struct that the json package can marshal. The type argument must NOT be a pointer type. The config information is based on the driver being used. This will typically contain connection information and the necessary information for generating the table or index.

func NewJsonDatastore

func NewJsonDatastore[M any](providerName string, cfg interface{}) (JsonDataStore[M], error)

func NewTypedStore

func NewTypedStore[T any](store UntypedJsonDataStore) JsonDataStore[T]

type JsonDataStoreAdapter

type JsonDataStoreAdapter[T any] struct {
	DS    BinaryDataStore
	Model T
}

func ToJsonDataStore

func ToJsonDataStore[T any](ds BinaryDataStore) *JsonDataStoreAdapter[T]

func (*JsonDataStoreAdapter[T]) Get

func (j *JsonDataStoreAdapter[T]) Get(ctx context.Context, key string) (T, error)

func (*JsonDataStoreAdapter[T]) Save

func (j *JsonDataStoreAdapter[T]) Save(ctx context.Context, item T, key string) error

type JsonDataStoreFactory

type JsonDataStoreFactory interface {
	Create(cfg interface{}) (UntypedJsonDataStore, error)
	ToConfig(cfgMap map[string]interface{}) (interface{}, error)
}

type LoadInput

type LoadInput struct {
	Ctx  context.Context
	Id   string
	User string
}

type LoadOutput

type LoadOutput struct {
	Ctx  context.Context
	Id   string
	User string
	Item interface{}
}

type NativeQuerable

type NativeQuerable[T any] interface {
	NativeQuery(ctx context.Context, query interface{}) (interface{}, error)
}

type SimpleQuery

type SimpleQuery struct {
	Size       int
	Offset     int
	Colums     []string
	Conditions *SimpleQueryConditionGroup
	SortBy     []*SortBy
}

SimpleQuery is a simple way to describe conditions and a query

func NewQuery

func NewQuery() *SimpleQuery

type SimpleQueryCondition

type SimpleQueryCondition struct {
	Type    string
	Data    []string
	DataMap map[string]interface{}
}

func (*SimpleQueryCondition) GetDate

func (sqc *SimpleQueryCondition) GetDate(key string) (val time.Time)

func (*SimpleQueryCondition) GetFloat

func (sqc *SimpleQueryCondition) GetFloat(key string) float64

func (*SimpleQueryCondition) GetInt

func (sqc *SimpleQueryCondition) GetInt(key string) int

func (*SimpleQueryCondition) GetString

func (sqc *SimpleQueryCondition) GetString(key string) string

func (*SimpleQueryCondition) GetStringArr

func (sqc *SimpleQueryCondition) GetStringArr(key string) []string

func (*SimpleQueryCondition) Set

func (sqc *SimpleQueryCondition) Set(key string, value interface{})

type SimpleQueryConditionGroup

type SimpleQueryConditionGroup struct {
	Operator   string //and, or, no
	Conditions []*SimpleQueryCondition
	Groups     []*SimpleQueryConditionGroup
}

func (*SimpleQueryConditionGroup) After

func (cg *SimpleQueryConditionGroup) After(field string, value time.Time)

func (*SimpleQueryConditionGroup) And

func (*SimpleQueryConditionGroup) Before

func (cg *SimpleQueryConditionGroup) Before(field string, value time.Time)

func (*SimpleQueryConditionGroup) Between

func (cg *SimpleQueryConditionGroup) Between(field string, gte string, lte string)

func (*SimpleQueryConditionGroup) Contains

func (cg *SimpleQueryConditionGroup) Contains(field string, value string)

func (*SimpleQueryConditionGroup) Equals

func (cg *SimpleQueryConditionGroup) Equals(field string, value string)

func (*SimpleQueryConditionGroup) Exists

func (cg *SimpleQueryConditionGroup) Exists(field string, value string)

func (*SimpleQueryConditionGroup) GreaterThan

func (cg *SimpleQueryConditionGroup) GreaterThan(field string, value string)

func (*SimpleQueryConditionGroup) GreaterThanOrEqual

func (cg *SimpleQueryConditionGroup) GreaterThanOrEqual(field string, value string)

func (*SimpleQueryConditionGroup) Includes

func (cg *SimpleQueryConditionGroup) Includes(field string, values []string)

func (*SimpleQueryConditionGroup) LessThan

func (cg *SimpleQueryConditionGroup) LessThan(field string, value string)

func (*SimpleQueryConditionGroup) LessThanOrEqual

func (cg *SimpleQueryConditionGroup) LessThanOrEqual(field string, value string)

func (*SimpleQueryConditionGroup) Not

func (*SimpleQueryConditionGroup) Or

type SortBy

type SortBy struct {
	Field      string
	Descending bool
}

type TestItem added in v0.0.2

type TestItem struct {
	ID   string
	Name string
}

type TestQueryItem added in v0.0.2

type TestQueryItem struct {
	ID     string
	Name   string
	Val1   int
	Val2   int
	Date1  time.Time
	Date2  time.Time
	StrArr []string
	ObjArr []*TestQueryItemChild
}

type TestQueryItemChild added in v0.0.2

type TestQueryItemChild struct {
	Name string
}

type TypedJsonStore

type TypedJsonStore[T any] struct {
	// contains filtered or unexported fields
}

func (*TypedJsonStore[T]) Close

func (ts *TypedJsonStore[T]) Close(ctx context.Context) error

Close should be called to cleannly close the datastore

func (*TypedJsonStore[T]) Delete

func (ts *TypedJsonStore[T]) Delete(ctx context.Context, key string) error

Deletes an item

func (*TypedJsonStore[T]) Exists

func (ts *TypedJsonStore[T]) Exists(ctx context.Context, key string) (bool, error)

Checks to see if a key exists

func (*TypedJsonStore[T]) Get

func (ts *TypedJsonStore[T]) Get(ctx context.Context, key string) (*T, error)

Get retrieves an item by it's unique id

func (*TypedJsonStore[T]) GetAll

func (ts *TypedJsonStore[T]) GetAll(ctx context.Context) ([]*T, error)

Gets all the items in the store.

func (*TypedJsonStore[T]) Open

func (ts *TypedJsonStore[T]) Open(ctx context.Context, config interface{}) error

Open will open the datastore for usage. This should only be done once per datastore

func (*TypedJsonStore[T]) Query

func (ts *TypedJsonStore[T]) Query(ctx context.Context, query *SimpleQuery) ([]*T, error)

Sends a simple Query

func (*TypedJsonStore[T]) Save

func (ts *TypedJsonStore[T]) Save(ctx context.Context, item *T, key string) error

Save stores an item in the datastore. There is no difference between an insert and an update.

type UntypedJsonDataStore

type UntypedJsonDataStore interface {

	// Open will open the datastore for usage. This should
	// only be done once per datastore
	Open(ctx context.Context, config interface{}) error

	// Close should be called to cleannly close the datastore
	Close(ctx context.Context) error

	// Save stores an item in the datastore. There is no difference
	// between an insert and an update.
	Save(ctx context.Context, item interface{}, key string) error

	// Get retrieves an item by it's unique id
	Get(ctx context.Context, key string) (interface{}, error)

	// Gets all the items in the store.
	GetAll(ctx context.Context) ([]interface{}, error)

	// Deletes an item
	Delete(ctx context.Context, key string) error

	// Checks to see if a key exists
	Exists(ctx context.Context, key string) (bool, error)

	// Sends a simple Query
	Query(ctx context.Context, query *SimpleQuery) ([]interface{}, error)
}

Jump to

Keyboard shortcuts

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