Om-wrapped PostgreSQL operations for PureScript, on top of yoga-postgres.
spago install yoga-postgres-om yoga-om yoga-om-layer
bun add postgres uuidYoga.Postgres.PostgresOm takes the connection from the Om context, rows come back as records
read with yoga-json, and a row that doesn't fit lands in the parseError channel:
module Main where
import Prelude
import Effect (Effect)
import Effect.Aff (launchAff_, throwError)
import Effect.Class (liftEffect)
import Effect.Class.Console (logShow)
import Effect.Exception (error)
import Yoga.Om as Om
import Yoga.Postgres as PG
import Yoga.Postgres.PostgresOm as Postgres
main :: Effect Unit
main = launchAff_ do
postgresConnection <- PG.postgres { connection: PG.ConnectionString "postgres://postgres:postgres@localhost:5433/test_playground" } # liftEffect
sums <- Om.runOm { postgresConnection }
{ exception: throwError, parseError: \errors -> throwError (error (show errors)) }
(Postgres.query @{ n :: Int, square :: Int } (PG.SQL "SELECT n, n * n AS square FROM generate_series(1, $1::int) AS n") [ PG.toPGValue 3 ])
logShow sums
PG.end postgresConnection[{ n: 1, square: 1 },{ n: 2, square: 4 },{ n: 3, square: 9 }]
Yoga.Postgres.ClientOm has the table-level helpers (create, findBy, updateWhere,
transactionally, ...), Yoga.Postgres.OmLayer a layer that opens the connection.
All of it runs on Go too, with googoo: point
your spago.yaml at the Go package set
and the program above prints the same thing from a Go binary.
workspace:
packageSet:
url: https://raw.githubusercontent.com/rowtype-yoga/purescript-go-package-sets/go-0.1.18-20261126.5/packages.jsonIf you're writing a yoga-go-http-om
server, its handlers are OmIO, not Om. yoga-postgres-omio is the same API for those:
Yoga.Postgres.OmIO, Yoga.Postgres.PostgresOmIO and Yoga.Postgres.TypedQueryOmIO, each query
on the request's goroutine. A handler can only fail with the responses its route has, so
throwParseErrors turns a row that doesn't fit into a 500:
getProduct :: Handler GetProduct (postgresConnection :: PG.Connection)
getProduct = handle do
{ path } <- ask
found <- executeSqlOne @_ @Product productBySku { sku: path.sku } # throwParseErrors
maybe (reject @"notFound" (unknown path.sku)) (respond @"ok") found
productBySku :: SQLQuery (sku :: Sku)
productBySku = sql $ "SELECT sku, name, stock FROM products WHERE sku = " ^ arg @Sku @"sku"examples/inventory-api is a whole shop like that: four typed routes on a Postgres table, one Go binary.
Start Postgres with docker compose -f docker-compose.test.yml up -d --wait, then
spago test -p yoga-postgres-om # JavaScript
googoo run -p yoga-postgres-om --main Test.Main # Go
googoo run -p yoga-postgres-omio --main Test.Postgres.OmIO.Main # Go, the OmIO versionMIT