Module
Nonbili.Postgres
- Package
- purescript-nonbili-postgres
- Repository
- nonbili/purescript-nonbili-postgres
This module is enough for most common cases.
import Nonbili.Postgres as Pg
main = do
pool <- Pg.newPool Pg.defaultConfig
Aff.launchAff_ do
Pg.withTransaction pool \client -> do
(res :: Pg.Result { title :: String }) <-
Pg.query client "select * from post" unit
logShow res
Re-exports from Nonbili.Postgres.Class
#ToQueryParams Source
class ToQueryParams a
Query params can be:
unit
- no paramArray a
- an array of params with the same typeTuple a b
- an array of params with different types
All params are serialized to Json
when sending through FFI.
Instances
ToQueryParams Unit
(EncodeJson a) => ToQueryParams (Array a)
(EncodeJson a, ToQueryParams (Tuple b nested)) => ToQueryParams (Tuple a (Tuple b nested))
(EncodeJson a, EncodeJson b) => ToQueryParams (Tuple a b)
Re-exports from Nonbili.Postgres.Config
#Config Source
type Config = { connectionString :: Maybe String, connectionTimeoutMillis :: Maybe Int, database :: Maybe String, host :: Maybe String, idleTimeoutMillis :: Maybe Int, max :: Maybe Int, password :: Maybe String, port :: Maybe Int, ssl :: Maybe Boolean, user :: Maybe String }
All fields are optional. pg
will fallback to PGDATABASE
, PGHOST
,
PGUSER
, PGPASSWORD
environement variables. See
https://node-postgres.com/features/connecting.
#defaultConfig Source
defaultConfig :: Config
All fields are set to Nothing
.
Re-exports from Nonbili.Postgres.Pool
#withTransaction Source
withTransaction :: forall a. Pool -> (Client -> Aff a) -> Aff a
Acquire a client from the pool, then run queries as a transaction.
#query Source
query :: forall a p. ToQueryParams p => DecodeJson a => Client -> String -> p -> Aff (Result a)
pg
will construct SQL from provided query string and params. Check
ToQueryParams
for supported params.