A simple store with support for secondary indexes.
A store. Basically like a Dict (Id a) a
but that can be manipulated with
the functions in System
in order to support indices.
This should appear in your model.
{ initStore : Store a
, get : Id a -> Store a -> Maybe a
, create : a -> Store a -> Maybe ( Id a
, Store a )
, insert : Id a -> a -> Store a -> Maybe (Store a)
, toList : Store a -> List ( Id a
, a )
, getBy : (indices -> Index a) -> String -> Store a -> Maybe a
, getIdBy : (indices -> Index a) -> String -> Store a -> Maybe (Id a)
, remove : Id a -> Store a -> Store a
}
A set of functions to manipulate a Store
.
This should NOT be part of your model.
You can generate one with makeSystem
The available functions are :
initStore
: creates an empty store.get
: gets an item by its Id.create
: creates a new item in the store and returns its Id
. Fails if the new item breaks
unicity constraints.insert
: insert an item at a given id
. Fails if the new item breaks
unicity constraints.toList
: returns all items in a store as a list of (id, item)
.getBy
: gets an item by a secondary key.
Example : system.getBy .lowerCaseName "john doe" store
getIdBy
: gets an item's Id
by a secondary key.remove
: deletes an item from the store.An index on stored values. For example if your store contains MyValue
items, you could define
your indices like this:
type alias MyIndices =
{ name : Index MyValue
, anyArbitraryIndexName : Index MyValue
}
The record constructor MyIndices
would then be passed to initConfig
.
initConfig : indices -> Config a indices
Creates a new system configuration.
Typically indices
should be a record constructor so that each index can be
configured using withIndex
.
withIndex : (a -> String) -> Config a (Index a -> indices) -> Config a indices
Configures an index on the stored items.
This index adds a unicity constraint : no two items can have the same indexed key.
Example :
-- Given `type alias MyIndices = { lowerCaseName : Index MyValue }`
initConfig MyIndices
|> withIndex (.lowerCaseName >> String.toLower)
makeSystem : Config a indices -> System a indices
Makes a system from a given config.
Store.initConfig MyIndices
|> Store.withIndex .name
|> Store.withIndex (.age >> String.fromInt)
|> Store.makeSystem