Chadtech / elm-relational-database / Db

Short for "Database", Db item is basically a glorified Dict String item. It takes Id item, and it if has an item under that Id item, it gives it to you.

users : Db User
users =
    --

maybeBob : (Id User, Maybe User)
maybeBob =
    Db.get users (Id.fromString "bob")

The functions in the Db module are much like those of Dict, but they are specialized for the use case of a relational database.

Db


type Db item

Short for "Database", it stores data by unique identifiers

empty : Db item

An empty Db with no entries


type alias Row item =
( Id item, item )

A single row in the Db; an item paired with its Id

List

fromList : List (Row item) -> Db item

Initialize a Db from a list of id-value pairs

toList : Db item -> List (Row item)

Turn your Db into a list

Insert

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

Insert an item into the Db under the given Id

insertMany : List (Row item) -> Db item -> Db item

Insert many items into the Db under their given Ids

Get

get : Db item -> Id item -> Maybe item

Get the item under the provided Id

getWithId : Db item -> Id item -> ( Id item, Maybe 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 item -> List (Id item) -> List ( Id item, Maybe item )

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

Update

update : Id item -> (Maybe item -> Maybe item) -> Db item -> Db 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 Nothing. If a Nothing comes out of the update function, the value under that id will be removed.

Map

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

Map a Db to a different data type.

mapItem : Id item -> (item -> item) -> Db item -> Db 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 item -> Db item -> Db item

Remove the item at the given Id, if it exists

Filter

filter : (Row item -> Basics.Bool) -> Db item -> Db item

Filter out items from a Db

filterMissing : List ( Id item, Maybe item ) -> List (Row item)

Take a List of items, presumably ones you just retrieved using getMany, and filter out the ones that werent present in the Db

myFriendsList
    |> Db.getMany people
    |> Db.filterMissing
    -->: List (Row Person)

allPresent : List ( Id item, Maybe item ) -> Result (List (Id item)) (List (Row item))

Verify that all the items are present, and if not, fail with a list of the items that are missing

allPresent [ (id1, Just user1), (id2, Just user2) ]
--> Ok [ (id1, user1), (id2, user2) ]

allPresent [ (id1, Nothing), (id2, Just user2), (id3, Nothing) ]
--> Err [ id1, id3 ]