⚠️ BROKEN: This package runs into a bug in the recent versions of the compiler and platforms. I'm looking into fixing those, but it might take a while. If you want to use this now, reach out to me in Roc's Zulip, and I can help you find a combination of versions that works.
--
Interface with PostgreSQL databases from Roc.
This package implements a PostgreSQL client on pure Roc that depends only on a TCP effect from the platform.
It exposes a simple API that allows you to run SQL commands as strings and a query builder that helps you write composable type-safe queries against your schema.
I'd like this to become a stable PostgreSQL interface for Roc, but this project is currently a work in progress.
You can already use this to build useful applications. However, until we have a platform with TLS support, you should stick to experiments or simple apps where you run the database in the same machine.
The query builder is one of the most exciting features of this package, but more experimental than the lower-level API.
You can currently generate a Roc module from your schema that you can use through the functions exposed under Sql
to compose type-safe SELECT
statements.
The plan is to support all the other SQL commands, but that's coming later. In the meantime, you can perform those by creating a raw SQL command with Pg.Cmd.new
.
See an example of a simple HTTP API built with the query builder!
Connecting and performing a query
task : Task (List { name: Str, price: Dec }) _
task =
client <- Pg.Client.withConnect {
host: "localhost",
port: 5432,
user: "postgres",
database: "postgres",
auth: Password "password"
}
Pg.Cmd.new "select name, price from products"
|> Pg.Cmd.expectN (
Pg.Result.succeed {
name: <- Pg.Result.str "name" |> Pg.Result.apply,
price: <- Pg.Result.dec "price" |> Pg.Result.apply
}
)
|> Pg.Client.command client
The API has been in a state of flux until recently. I have now started working on documentation, but the examples is probably the best we have for now.
Feel free to DM me at Roc's Zulip, though!
* Requires new platform primitives
This list does not include features of the query builder as I'm still figuring out those.