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")
}
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.
Decode a string field.
Turso.Db.Decode.string "name"
Decode an integer field.
Turso.Db.Decode.int "count"
Decode a float field.
Turso.Db.Decode.float "cost"
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
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.
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.
Transform a single decoded value into another value. For example:
Turso.Db.Decode.string "email"
|> Turso.Db.Decode.map Email.fromString
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")
Transform three decoded values into another value.
Transform four decoded values into another value.
Transform five decoded values into another value.
Transform six decoded values into another value.
Transform seven decoded values into another value.
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.
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
.
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 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.