Chadtech/id/3.1.0

NOTE: this package is not compatible with Elm 0.19.1

Id

A simple Id type for your types that have ids.

Id

type Id
fromString : String -> Id

The only way to make an Id

Id.fromString "vq93rUv0A4"
encode : Id -> Value

Encode an Id

Encode.encode 0 (Id.encode id)
-- ""hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh"" : String


[ ("id", Id.encode id) ]
    |> Encode.object
    |> Encode.encode 0


-- {\"id\":\"hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh\"} : String
decoder : Decoder Id

Decode an Id

Decode.decodeString (Decode.field "id" Id.decoder) "{\"id\":\"19\"}"
-- Ok (Id 19) : Result String Id
generator : Generator Id

A way to generate random Ids

import Id exposing (Id)
import Random.Pcg as Random exposing (Seed)


user : Seed -> ( User, Seed )
user seed =
    let
        ( id, nextSeed ) =
            Random.step Id.generator seed
    in
    ( { id = id, email = "[email protected]" }
    , nextSeed
    )

Origin

type Origin
= Local
| Remote Id

Data that comes from a remote source usually has an id, while data that is made locally doesnt. If thats your situation, using this Origin type might be a better way of representing your datas id.

type alias Car =
    { origin : Origin
    , make : String
    }


saveCar : Car -> Http.Request (Result Err ())
saveCar car =
    case car.origin of
        Id id ->
            Car.update id car


        New ->
            Car.create car

Db

type Db a

A dictionary that uses Id as keys

get : Id -> Db a -> Maybe a

Get the item with a given id, if its in the Db

remove : Id -> Db item -> Db item

Remove an item from a Db

insert : item -> Db item -> Db item

Insert a new item into a Db

toDb : (item -> Id) -> List item -> Db item

Make a Db that uses Id as keys

type alias User =
    { id : Id
    , name : String
    }




toDb .id users : Db User
items : Db item -> List item

Get just the items in a Db

emptyDb : (item -> Id) -> Db item

An empty Db