Db.Decode

Decode SQL results into Gren values.

These are typically used in the Db.get* functions via the decoder field, where the string value matches the name of the field in the query. For example:

-- Assuming a table with id INTEGER, name TEXT
Db.getOne connection
    { query = "select * from users where id = :id"
    , parameters = [ Db.Encode.int 1 ]
    , decoder = 
        Db.Decode.map2
            (Db.Decode.int "id")
            (Db.Decode.string "name")
            (\id name -> { id = id, name = name })
    }
type Decoder a

A decoder for a full record.

This is what's passed to the decoder field.

type FieldDecoder a

A decoder for an individual field.

This is what's passed to your map functions.

Fields

string : String -> FieldDecoder String

Decode a string field.

int : String -> FieldDecoder Int

Decode an integer field.

float : String -> FieldDecoder Float

Decode a float field.

bool : String -> FieldDecoder Bool

Decode a boolean field.

posix : String -> FieldDecoder Posix

Decode an integer field into a Time.Posix value.

The integer in the database should represent the number of milliseconds since 1970 January 1 at 00:00:00 UTC. (See Db.Encode.posix.

maybe :
(String -> FieldDecoder a)
-> String
-> FieldDecoder (Maybe a)

Decode a nullable field in the database.

The first parameter is the decoder function for the field type if the value is not null. For example, to decode a nullable INTEGER field:

Db.Decode.maybe Db.Decode.int "myField"

Mapping

Use these map functions to turn one or more field decoders into a full decoder.

map : FieldDecoder a -> (a -> b) -> Decoder b

Map a single field.

map2 :
FieldDecoder a
-> FieldDecoder b
-> (a -> b -> c)
-> Decoder c

Map 2 fields.

map3 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> (a -> b -> c -> d)
-> Decoder d

Map 3 fields.

map4 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> FieldDecoder d
-> (a -> b -> c -> d -> e)
-> Decoder e

Map 4 fields.

map5 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> FieldDecoder d
-> FieldDecoder e
-> (a -> b -> c -> d -> e -> f)
-> Decoder f

Map 5 fields.

map6 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> FieldDecoder d
-> FieldDecoder e
-> FieldDecoder f
-> (a -> b -> c -> d -> e -> f -> g)
-> Decoder g

Map 6 fields.

map7 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> FieldDecoder d
-> FieldDecoder e
-> FieldDecoder f
-> FieldDecoder g
-> (a -> b -> c -> d -> e -> f -> g -> h)
-> Decoder h

Map 7 fields.

map8 :
FieldDecoder a
-> FieldDecoder b
-> FieldDecoder c
-> FieldDecoder d
-> FieldDecoder e
-> FieldDecoder f
-> FieldDecoder g
-> FieldDecoder h
-> (a -> b -> c -> d -> e -> f -> g -> h -> i)
-> Decoder i

Map 8 fields.

Util

You shouldn't need these unless you are bypassing this package's API to interact with the ws4sql server directly.

ws4sqlQueryResponse : Decoder a -> Decoder (Array a)

Get a json decoder for a raw ws4sql query response.

ws4sqlStatementResponse : Decoder (Array Int)

Get a json decoder for a raw ws4sql statement response.