Db.Decode

Decode SQL results into Gren values.

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

Db.getOne connection
    { query = "select id, name from users where id = :id"
    , parameters = [ Db.Encode.int 1 ]
    , decoder = 
        Db.Decode.field Db.Decode.int "id" <| \id ->
        Db.Decode.field Db.Decode.string "name" <| \name ->
        Db.Decode.succeed
            { id = id
            , name = name
            }
    }
type Decoder a

A decoder for a database row.

Individual Field Decoders

Use these functions to decode an individual field in a row. You can also use these to build up decoders for multiple fields via the field function, or transform them with the map and andThen functions.

string : String -> Decoder String

Decode a string field.

int : String -> Decoder Int

Decode an integer field.

float : String -> Decoder Float

Decode a float field.

bool : String -> Decoder Bool

Decode a boolean field.

Booleans in sqlite can be stored as integers with 1 and 0 as True and False, or as a string with value "TRUE" or "FALSE". All other values will fail decoding. See https://www.sqlite.org/datatype3.html#boolean_datatype

posix : String -> Decoder Posix

Decode an integer field into a Time.Posix value.

The integer in the database should represent unix time in milliseconds (the number of milliseconds since 1970 January 1 at 00:00:00 UTC). This is how a Time.Posix value will be saved when using Db.Encode.posix.

maybe : (String -> Decoder a) -> String -> Decoder (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"

Composing Decoders

Use these functions to build up more complex decoders or decode multiple fields.

field :
(String -> Decoder a)
-> String
-> (a -> Decoder b)
-> Decoder b

Build chainable field decoders.

Often used with succeed and fail.

Db.Decode.field Db.Decode.int "id" <| \id ->
Db.Decode.field Db.Decode.bool "admin" <| \admin ->
Db.Decode.field Db.Decode.string "name" <| \name ->
Db.Decode.succeed
    { id = id
    , admin = admin
    , name = name
    }
succeed : a -> Decoder a

Create a decoder that's hard-coded to a specific value.

For example:

Decode.succeed "abc"

will always decode to "abc" regardless of input.

fail : String -> Decoder a

Force a decoder to fail.

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

Map the result of a decoder with another function.

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

Chain decoders together.

decodeEmail : String -> Decoder Email
decodeEmail emailField =
Db.Decode.string emailField
    |> Db.Decode.andThen
        (\email ->
            when Email.fromString is
                Just e -> Decode.succeed e
                Nothing -> Decode.fail "Invalid email"
        )

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.