SQLite for Gren
Use sqlite entirely in Gren without ports via ws4sql.
Note: this package expects the ws4sql fork of ws4sqlite. If you're using the npm package, you're all set. Otherwise, check the github releases of ws4sqlite for the latest ws4sql version (0.17.x).
Usage Example
Start a ws4sql database server:
npx ws4sql --db mydatabase.db
This will create mydatabase.db
if it doesn't exist and start a server available at http://localhost:12321/mydatabase
.
See docs for the npm package and ws4sql itself for details on running the server.
Then you can write code like:
import Db
import Db.Encode
import Db.Decode
import HttpClient
type alias User =
{ id : Int
, name : String
}
getUser : HttpClient.Permission -> Int -> Task Db.Error User
getUser httpPerm userId =
let
connection =
Db.init httpPerm "http://localhost:12321/mydatabase"
in
Db.getOne connection
{ query = "select * from users where id = :id"
, parameters = [ Db.Encode.int "id" userId ]
, decoder =
Db.Decode.map2
(Db.Decode.int "id")
(Db.Decode.string "name")
(\id name -> { id = id, name = name })
}
See the package docs for full usage details.