Documentation
¶
Index ¶
- type Collection
- func (c *Collection[T]) AdditionalKey(key string, value indexFn[T]) Getter[T]
- func (c *Collection[T]) GetBy(key string) Getter[T]
- func (c *Collection[T]) Invalidate()
- func (c *Collection[T]) Load(writer DBWriter)
- func (c *Collection[T]) MapBy(key string, ref indexFn[T]) Query[T]
- func (c *Collection[T]) PrimaryKey(key string, value indexFn[T]) Getter[T]
- func (c *Collection[T]) Scalar(key, value string) Scalar[T]
- func (c *Collection[T]) Scan(consume func(T) bool, filters ...func(T) bool)
- func (c *Collection[T]) With(opts ...CollectionOpt[T]) *Collection[T]
- type CollectionOpt
- type DB
- type DBViewer
- type DBWriter
- type Derivative
- type Getter
- type InferredCollection
- type Query
- type Scalar
- type Scanner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection[T any] struct { // contains filtered or unexported fields }
Collection is like a typed access layer to the db defined by a schema but because the purpose of this repository is IoC of the data, it also defines how the data is loaded from the cold source.
func NewCollection ¶
func NewCollection[T any](db DB, kind string, opts ...CollectionOpt[T]) (c *Collection[T])
NewCollection creates a Collection of T with the provided opts; PrimaryKey and Extractor are mandatory
func (*Collection[T]) AdditionalKey ¶
func (c *Collection[T]) AdditionalKey(key string, value indexFn[T]) Getter[T]
AdditionalKey creates an additional index on the collection using an indexFn
for example; c.AdditionalKey("name", func(f Foo, val func(string)) { val(f.name) })
func (*Collection[T]) GetBy ¶
func (c *Collection[T]) GetBy(key string) Getter[T]
GetBy creates a getter from existing index
func (*Collection[T]) Invalidate ¶
func (c *Collection[T]) Invalidate()
Invalidate reloads all data from the origin source, defined by the Extractor
func (*Collection[T]) Load ¶
func (c *Collection[T]) Load(writer DBWriter)
Load loads all data from the origin source, defined by the Extractor
func (*Collection[T]) MapBy ¶
func (c *Collection[T]) MapBy(key string, ref indexFn[T]) Query[T]
MapBy creates a Query from the provided key mapped by the provided indexFn, to be used for querying the collection by a non-unique attribute
func (*Collection[T]) PrimaryKey ¶
func (c *Collection[T]) PrimaryKey(key string, value indexFn[T]) Getter[T]
PrimaryKey creates a primary index on the collection using an indexFn
for example; c.PrimaryKey("id", func(f Foo, val func(string)) { val(f.id) })
func (*Collection[T]) Scalar ¶
func (c *Collection[T]) Scalar(key, value string) Scalar[T]
Scalar creates a "static" Getter that will require no key
func (*Collection[T]) Scan ¶
func (c *Collection[T]) Scan(consume func(T) bool, filters ...func(T) bool)
Scan iterates over all items in the collection, not sorted
func (*Collection[T]) With ¶
func (c *Collection[T]) With(opts ...CollectionOpt[T]) *Collection[T]
With instruments the collection with the provided opts
type CollectionOpt ¶
type CollectionOpt[T any] func(*Collection[T])
func AdditionalKey ¶
func AdditionalKey[T any](name string, value indexFn[T]) CollectionOpt[T]
AdditionalKey adds a secondary index of the collection
func Extractor ¶
func Extractor[T any](x extractFn[T]) CollectionOpt[T]
Extractor sets the extractFn of the collection. extractFn is a function that extracts the data from the origin source
func PrimaryKey ¶
func PrimaryKey[T any](name string, value indexFn[T]) CollectionOpt[T]
PrimaryKey sets the primary index of the collection
type DB ¶
type DB interface { DBViewer DBWriter // View provides atomic read-only access to the db for the scope of the // provided callback, isolated from other operations on the db. View(func(viewer DBViewer) error) error // Update provides atomic read-write access to the db for the scope of // the provided callback. Update(func(writer DBWriter) error) error // GetOrFill is a nice utility that wraps get, put if not exist and return // the value, GetOrFill(key string, fill func() (any, error), tags ...string) (val any, err error) // Invalidate deletes all keys related to the provided tags Invalidate(tags ...string) (deleted []string) }
DB maintains items under keys indexed also by tags in order to be able to evict all items under a specific tag
type DBViewer ¶
type DBViewer interface { // Get safely retrieves a val identified by the provided key Get(key string) (val any, ok bool) // Iter retrieves all the keys under a tag and let you access each item in each key Iter(tag string, fn func(key string, getVal func() (any, bool)) (proceed bool)) }
DBViewer represents isolated read access handle to the db
type DBWriter ¶
type DBWriter interface { DBViewer // Put safely sets the provided val under the provided key indexed by the // provided tags Put(key string, val any) // Tag simply adds tag on a key Tag(key string, tags ...string) // Invalidate deletes all keys related to the provided tags Invalidate(tags ...string) (deleted []string) }
DBWriter represents isolated write access handle to the db
type Derivative ¶
Derivative is a fetch method that is lazily initiated from another item but only once
func Derive ¶
func Derive[In, Out any](collection *Collection[In], name string, fn func(in In) (out Out, err error)) Derivative[In, Out]
Derive creates a Derivative item fetcher that is stored under the provided subKey in relation to the provided item
type InferredCollection ¶
type InferredCollection[T any] struct { *Collection[T] // contains filtered or unexported fields }
func Infer ¶
func Infer[Base, Inferred any](baseCol *Collection[Base], mapBy string, mapFn mapFn[Base, Inferred]) *InferredCollection[Inferred]
Infer creates a "chained" collection in a way that for every loaded item on the base collection, the provided mapFn is called to load the inferred item
func (*InferredCollection[T]) Query ¶
func (i *InferredCollection[T]) Query(key string, filters ...func(T) bool) ([]T, error)
Query is the Query that created by the inferencee. the key is necessarily the primary key of the base collection.
func (*InferredCollection[T]) With ¶
func (i *InferredCollection[T]) With(opts ...CollectionOpt[T]) *InferredCollection[T]
With is just a proxy of the underlying Collection's method