Turso.Db.Decode

Decode results of SQL queries 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:

Turso.Db.getOne connection
    { query = "SELECT * FROM users WHERE id = :id"
    , parameters = 
        [ Turso.Db.Encode.int 12345
        ]
    , decoder = 
        Db.Decode.map2
            (\id name -> 
                { id = id
                , name = name 
                }
            )
            (Db.Decode.int "id")
            (Db.Decode.string "name")
    }
type Decoder a

A decoder for a database row.

Decoding Fields

Use these functions to decode individual fields in a row. You can combine multiple fields to construct Gren records (or other values) with the mapN functions.

string : String -> Decoder String

Decode a string field.

Turso.Db.Decode.string "name"
int : String -> Decoder Int

Decode an integer field.

Turso.Db.Decode.int "count"
float : String -> Decoder Float

Decode a float field.

Turso.Db.Decode.float "cost"
bool : String -> Decoder Bool

Decode a boolean field.

Turso.Db.Decode.bool "is_active"

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.

Turso.Db.Decode.posixe "created_at"

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 Turso.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:

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

Mapping

To combine multiple fields to construct Gren values like records, you can use the the mapN functions.

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

Transform a single decoded value into another value. For example:

Turso.Db.Decode.string "email"
    |> Turso.Db.Decode.map Email.fromString
map2 : (a -> b -> c) -> Decoder a -> Decoder b -> Decoder c

Transform two decoded values into another value. For example:

Turso.Db.Decode.map2
    (\email password ->
        { email = email
        , password = password
        }
    )
    (Turso.Db.Decode.string "email")
    (Turso.Db.Decode.string "password")
map3 :
(a -> b -> c -> d)
-> Decoder a
-> Decoder b
-> Decoder c
-> Decoder d

Transform three decoded values into another value.

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

Transform four decoded values into another value.

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

Transform five decoded values into another value.

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

Transform six decoded values into another value.

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

Transform seven decoded values into another value.

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

Transform eight decoded values into another value.

If you need more than 8, you can tack on more with andThen.

Fancy Decoding

Build more complex decoders with these functions.

succeed : a -> Decoder a

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

For example:

Turso.Db.Decode.succeed "abc"

Will always decode to "abc" regardless of input.

This is most often used with andThen.

fail : String -> Decoder a

Force a decoder to fail.

This is most often used with andThen.

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

Chain decoders together.

This is most often used this conjunction with succeed and fail. For example:

Turso.Db.Decode.string "email"
    |> Turso.Db.Decode.andThen
        (\email ->
            when Email.fromString email is
                Just email -> 
                    Turso.Db.Decode.succeed email

                Nothing -> 
                    Turso.Db.Decode.fail "invalid email"
        )

Internals

unwrap : Decoder a -> Decoder a

Unwrap a Decoder and turns it into a normal Json.Decode.Decoder

This is used by other modules in this package and not needed to properly decode data returned from queries.