sdeframond / elm-store / Store

A simple store with support for secondary indexes.

Main types


type Store a

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.


type alias System a indices =
{ 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 :

Indices Configuration


type Index a

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