Chadtech/elm-relational-database/1.0.0

NOTE: the latest version is 1.2.2

Remote.Db

A way of storing your remote data by Id, and being able to representing its loading state

Db

type Db error item

Short for "Database", it stores data by unique identifiers. The error is for loading errors, such as if an http request fails.

empty : Db error item

An empty Db with no entries

type alias Row error item =
( Id error item, RemoteData error item )

A single row in the Db; an Id, corresponding to either an item or the error that happend when attempting to load it.

Insert

succeed : ( Id error item, item ) -> Db error item -> Db error item

Insert an item into a Db, when it has been successfully loaded

succeedMany : List ( Id error item, item ) -> Db error item -> Db error item

Insert many items into a Db, such as when you loaded many items successfully.

loading : Id error item -> Db error item -> Db error item

Mark the Id as Loading. After this function is used on a Db, it will return Loading for that Id

-- if the id is not in the `Db`
Remote.Db.get db id
--> RemoteData.NotAsked


-- but if it is..
Remote.Db.get (Remote.Db.loading id db) id
--> RemoteData.Loading
loadingMany : List (Id error item) -> Db error item -> Db error item

Mark a List of Id as Loading in a Db

fail : ( Id error item, error ) -> Db error item -> Db error item

When data fails to load, set it as Failure in the Db

failMany : List ( Id error item, error ) -> Db error item -> Db error item

Fail many Id, representing them as failing to load in the Db

insert : Row error item -> Db error item -> Db error item

A general insert function into Db. You can insert a RemoteData error item into a Db

-- if the id is not in the `Db`
Remote.Db.get db id
--> RemoteData.NotAsked


-- but if it is..
Remote.Db.get
    (Remote.Db.insert (id, RemoteData.Failure "Could not load") db)
    id
--> RemoteData.Failure "Could not load"

Get

get : Db error item -> Id error item -> RemoteData error item

Get an item out of a Db. Notice how it does not return a Maybe like Dict do. An Id that is not present in the Db is returned as RemoteData.NotAsked.

getWithId : Db error item -> Id error item -> Row error item

Just like get, except it comes with the Id, for those cases where you dont want the item separated from its Id

getMany : Db error item -> List (Id error item) -> List (Row error item)

Get many items from a Db. The (id, RemoteData.NotAsked) case represents the item under that Id being absent.

Update

Id error item
-> (RemoteData error item -> RemoteData error item)
-> Db error item
-> Db error item

Update an item in a Db, using an update function. If the item doesnt exist in the Db, it comes into the update as RemoteData.NotAsked. If a RemoteData.NotAsked comes out of the update function, the value under that id will be removed.

Map

map : (a -> b) -> Db error a -> Db error b

Map a Db to a different data type.

mapError : (a -> b) -> Db a item -> Db b item

Map the error type of a Db to something else.

mapItem : Id error item -> (item -> item) -> Db error item -> Db error item

Apply a change to just one item in the Db, assuming the item is in the Db in the first place. This function is just like update except deleting the item is not possible.

Remove

remove : Id error item -> Db error item -> Db error item

Simply remove an Id from a Db, resetting that item to RemoteData.NotAsked

Remote.Db.get (Remote.Db.remove id db) id
--> RemoteData.NotAsked