Turso.Db.Encode

Encode SQL parameters.

Encoded parameters are passed in an array via the parameters field, where the string matches the colon-prefixed named of the parameter in the sql:

Turso.Db.execute connection
    { statement = "INSERT INTO users (id, name) VALUES (:id, :name)"
    , parameters =
        [ Turso.Db.Encode.int "id" 1
        , Turso.Db.Encode.string "name" "Justin"
        ]
    }

Queries and statements where encoded parameters are used can be found in the Turso.Db module.

type Value

An encoded parameter value.

Encode Values

string : String -> String -> Value

Encode a String value.

Db.Encode.string "name" "Jessica Therme"
int : String -> Int -> Value

Encode an integer.

Db.Encode.int "count" 100
float : String -> Float -> Value

Encode a float.

Db.Encode.float "pi" 3.14159265359
bool : String -> Bool -> Value

Encode a boolean value.

Db.Encode.bool "is_admin" True
null : String -> Value

Encode null.

Db.Encode.null "nothing"
posix : String -> Posix -> Value

Encode a Time.Posix value.

This is what you should use for date fields. The database field should be an INTEGER type. It will be stored as the number of milliseconds since 1970 January 1 at 00:00:00 UTC (it was a Thursday).

Db.Encode.posix "created_at" (Time.millisToPosix 0)

Internals

unwrap : Value -> { key : String, value : Value, type_ : String }

Unwrap the Value type and turn it a record.

This is used by other modules in this package and not needed to properly encode parameters for queries.