Chadtech / elm-relational-database / Remote.Db

This module combines Kris Jenkin's RemoteData package (https://package.elm-lang.org/packages/krisajenkins/remotedata/latest) with the Db type in this package. The kinds of things you store in a relational database are usually fetched from a remote source, and the RemoteData type represents whether your data is loading, loaded, or failed to load. By storing data as RemoteData, this module can make the assumption that Id that are missing from the Dbare RemoteData.NotAsked. This assumption permits our get function to return a RemoteData error item instead of a Maybe item or even a Maybe (RemoteData error item).

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 =
( Remote.Id.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.

List

fromList : List (Row error item) -> Db error item

Initialize a Db from a list of id-value pairs

toList : Db error item -> List (Row error item)

Turn your Db into a list

Insert

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

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

succeedMany : List ( Remote.Id.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 : Remote.Id.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 (Remote.Id.Id error item) -> Db error item -> Db error item

Mark a List of Id as Loading in a Db

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

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

failMany : List ( Remote.Id.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 -> Remote.Id.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 -> Remote.Id.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 (Remote.Id.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.

getSuccesses : Db error item -> List ( Remote.Id.Id error item, item )

Get only the succcessfully loaded item in a Db

getErrors : Db error item -> List ( Remote.Id.Id error item, error )

Get a list of Id that have failed to load and the error that occured when loading that Id.

getLoading : Db error item -> List (Remote.Id.Id error item)

Get a list of Ids that are still RemoteData.Loading

Update

update : Remote.Id.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 : Remote.Id.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 : Remote.Id.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

Filter

filter : (( Remote.Id.Id error item, item ) -> Basics.Bool) -> Db error item -> Db error item

Filter out items from a Db, only the successfully loaded item are considered.