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
}
}
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.
Decode a string field.
Decode an integer field.
Decode a float field.
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
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.
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.
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
}
Create a decoder that's hard-coded to a specific value.
For example:
Decode.succeed "abc"
will always decode to "abc" regardless of input.
Force a decoder to fail.
Map the result of a decoder with another function.
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.
Get a json decoder for a raw ws4sql query response.
Get a json decoder for a raw ws4sql statement response.