IzumiSy / elm-firestore / Firestore.Codec

Codec for Firestore

type alias User =
    { name : String
    , age : Int
    }

codec : Codec.Codec User
codec =
    Codec.document User
        |> Codec.required "name" .name Codec.string
        |> Codec.required "age" .age Codec.int
        |> Codec.build

getDocument : Firestore.Firestore -> Cmd Msg
getDocument firestore =
    firestore
        |> Firestore.root
        |> Firestore.collection "users"
        |> Firestore.document "user0"
        |> Firestore.build
        |> ExResult.toTask
        |> Task.andThen (Firestore.get (Codec.asDecoder codec))
        |> Task.attempt GotDocument

insertDocument : Firestore.Firestore -> Cmd Msg
insertDocument firestore =
    firestore
        |> Firestore.root
        |> Firestore.collection "users"
        |> Firestore.build
        |> ExResult.toTask
        |> Task.andThen
            (Firestore.insert
                (Codec.asDecoder codec)
                (Codec.asEncoder codec { name = "thomas", age = 26 })
            )
        |> Task.attempt InsertedDocument

Definitions


type Codec a

asEncoder : Codec a -> a -> Firestore.Encode.Encoder

encode : Codec a -> a -> Json.Encode.Value

asDecoder : Codec a -> Firestore.Decode.Decoder a

decode : Codec a -> Json.Decode.Decoder a

Constructors


type Document a cons

document : cons -> Document a cons

build : Document a a -> Codec a

required : String -> (a -> b) -> Field b -> Document a (b -> cons) -> Document a cons

optional : String -> (a -> b) -> Field b -> b -> Document a (b -> cons) -> Document a cons

Field


type Field a

bool : Field Basics.Bool

bytes : Field String

int : Field Basics.Int

string : Field String

list : Field a -> Field (List a)

dict : Field a -> Field (Dict String a)

null : Field ()

maybe : Field a -> Field (Maybe a)

timestamp : Field Time.Posix

geopoint : Field Firestore.Types.Geopoint.Geopoint

reference : Field Firestore.Types.Reference.Reference

Advanced

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

andThen : (a -> Field b) -> (b -> a) -> Field a -> Field b

succeed : a -> Field a

fail : String -> Field a

construct : Firestore.Decode.Decoder a -> (a -> Firestore.Encode.Encoder) -> Codec a