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 param
  • Array a - an array of params with the same type
  • Tuple a b - an array of params with different types

All params are serialized to Json when sending through FFI.

Instances

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

#Result Source

type Result a = Either String { rowCount :: Int, rows :: Array a }

#Pool Source

data Pool

pg.Pool object.

#Client Source

data Client

Pooled client boject.

#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.

#newPool Source

newPool :: Config -> Effect Pool

Create a new pool with configuration.

#execute Source

execute :: forall p. ToQueryParams p => Client -> String -> p -> Aff Unit

Same as query, but ignore all return values.