pog
Postgresql client
Gleam wrapper around pgo library
Types
The configuration for a pool of connections.
pub type Config {
Config(
host: String,
port: Int,
database: String,
user: String,
password: Option(String),
ssl: Bool,
connection_parameters: List(#(String, String)),
pool_size: Int,
queue_target: Int,
queue_interval: Int,
idle_interval: Int,
trace: Bool,
ip_version: IpVersion,
rows_as_map: Bool,
)
}
Constructors
-
Config( host: String, port: Int, database: String, user: String, password: Option(String), ssl: Bool, connection_parameters: List(#(String, String)), pool_size: Int, queue_target: Int, queue_interval: Int, idle_interval: Int, trace: Bool, ip_version: IpVersion, rows_as_map: Bool, )
Arguments
-
host
(default: 127.0.0.1): Database server hostname.
-
port
(default: 5432): Port the server is listening on.
-
database
Name of database to use.
-
user
Username to connect to database as.
-
password
Password for the user.
-
ssl
(default: False): Whether to use SSL or not.
-
connection_parameters
(default: []): List of 2-tuples, where key and value must be binary strings. You can include any Postgres connection parameter here, such as
#("application_name", "myappname")
and#("timezone", "GMT")
. -
pool_size
(default: 1): Number of connections to keep open with the database
-
queue_target
(default: 50) Checking out connections is handled through a queue. If it takes longer than queue_target to get out of the queue for longer than queue_interval then the queue_target will be doubled and checkouts will start to be dropped if that target is surpassed.
-
queue_interval
(default: 1000)
-
idle_interval
(default: 1000): The database is pinged every idle_interval when the connection is idle.
-
trace
trace (default: False): pgo is instrumented with OpenCensus and when this option is true a span will be created (if sampled).
-
ip_version
(default: Ipv4) Which internet protocol to use for this connection
-
rows_as_map
(default: False) By default, pgo will return a n-tuple, in the order of the query. By setting
rows_as_map
toTrue
, the result will beDict
.
-
A pool of one or more database connections against which queries can be made.
Created using the connect
function and shut-down with the disconnect
function.
pub type Connection
pub type Date {
Date(year: Int, month: Int, day: Int)
}
Constructors
-
Date(year: Int, month: Int, day: Int)
The internet protocol version to use.
pub type IpVersion {
Ipv4
Ipv6
}
Constructors
-
Ipv4
Internet Protocol version 4 (IPv4)
-
Ipv6
Internet Protocol version 6 (IPv6)
pub type QueryError {
ConstraintViolated(
message: String,
constraint: String,
detail: String,
)
PostgresqlError(code: String, name: String, message: String)
UnexpectedArgumentCount(expected: Int, got: Int)
UnexpectedArgumentType(expected: String, got: String)
UnexpectedResultType(DecodeErrors)
ConnectionUnavailable
}
Constructors
-
ConstraintViolated( message: String, constraint: String, detail: String, )
The query failed as a database constraint would have been violated by the change.
-
PostgresqlError(code: String, name: String, message: String)
The query failed within the database. https://www.postgresql.org/docs/current/errcodes-appendix.html
-
UnexpectedArgumentCount(expected: Int, got: Int)
-
UnexpectedArgumentType(expected: String, got: String)
One of the arguments supplied was not of the type that the query required.
-
UnexpectedResultType(DecodeErrors)
The rows returned by the database could not be decoded using the supplied dynamic decoder.
-
ConnectionUnavailable
No connection was available to execute the query. This may be due to invalid connection details such as an invalid username or password.
The rows and number of rows that are returned by a database query.
pub type Returned(t) {
Returned(count: Int, rows: List(t))
}
Constructors
-
Returned(count: Int, rows: List(t))
pub type Time {
Time(hours: Int, minutes: Int, seconds: Int, microseconds: Int)
}
Constructors
-
Time(hours: Int, minutes: Int, seconds: Int, microseconds: Int)
pub type Timestamp {
Timestamp(date: Date, time: Time)
}
Constructors
-
Timestamp(date: Date, time: Time)
pub type TransactionError {
TransactionQueryError(QueryError)
TransactionRolledBack(String)
}
Constructors
-
TransactionQueryError(QueryError)
-
TransactionRolledBack(String)
Functions
pub fn connect(a: Config) -> Connection
Start a database connection pool.
The pool is started in a new process and will asynchronously connect to the PostgreSQL instance specified in the config. If the configuration is invalid or it cannot connect for another reason it will continue to attempt to connect, and any queries made using the connection pool will fail.
pub fn connection_parameter(
config: Config,
name name: String,
value value: String,
) -> Config
Any Postgres connection parameter here, such as
"application_name: myappname"
and "timezone: GMT"
pub fn decode_date(
value: Dynamic,
) -> Result(Date, List(DecodeError))
pub fn decode_time(
value: Dynamic,
) -> Result(Time, List(DecodeError))
pub fn decode_timestamp(
value: Dynamic,
) -> Result(Timestamp, List(DecodeError))
pub fn default_config() -> Config
The default configuration for a connection pool, with a single connection. You will likely want to increase the size of the pool for your application.
pub fn error_code_name(error_code: String) -> Result(String, Nil)
Get the name for a PostgreSQL error code.
> error_code_name("01007")
Ok("privilege_not_granted")
https://www.postgresql.org/docs/current/errcodes-appendix.html
pub fn execute(
query query: Query(a),
on pool: Connection,
) -> Result(Returned(a), QueryError)
Run a query against a PostgreSQL database.
The provided dynamic decoder is used to decode the rows returned by
PostgreSQL. If you are not interested in any returned rows you may want to
use the dynamic.dynamic
decoder.
pub fn host(config: Config, host: String) -> Config
Database server hostname.
(default: 127.0.0.1)
pub fn idle_interval(
config: Config,
idle_interval: Int,
) -> Config
The database is pinged every idle_interval when the connection is idle.
default: 1000
pub fn ip_version(
config: Config,
ip_version: IpVersion,
) -> Config
Which internet protocol to use for this connection
pub fn parameter(query: Query(a), parameter: Value) -> Query(a)
Push a new query parameter value for the query.
pub fn password(
config: Config,
password: Option(String),
) -> Config
Password for the user.
pub fn pool_size(config: Config, pool_size: Int) -> Config
Number of connections to keep open with the database
default: 10
pub fn port(config: Config, port: Int) -> Config
Port the server is listening on.
(default: 5432)
pub fn query(sql: String) -> Query(Nil)
Create a new query to use with the execute
, returning
, and parameter
functions.
pub fn queue_interval(
config: Config,
queue_interval: Int,
) -> Config
Checking out connections is handled through a queue. If it takes longer than queue_target to get out of the queue for longer than queue_interval then the queue_target will be doubled and checkouts will start to be dropped if that target is surpassed.
default: 1000
pub fn queue_target(config: Config, queue_target: Int) -> Config
Checking out connections is handled through a queue. If it takes longer than queue_target to get out of the queue for longer than queue_interval then the queue_target will be doubled and checkouts will start to be dropped if that target is surpassed.
default: 50
pub fn returning(
query: Query(a),
decoder: fn(Dynamic) -> Result(b, List(DecodeError)),
) -> Query(b)
Set the decoder to use for the type of row returned by executing this query.
If the decoder is unable to decode the row value then the query will return
an error from the exec
function, but the query will still have been run
against the database.
pub fn rows_as_map(config: Config, rows_as_map: Bool) -> Config
By default, pgo will return a n-tuple, in the order of the query.
By setting rows_as_map
to True
, the result will be Dict
.
pub fn trace(config: Config, trace: Bool) -> Config
Trace pgo is instrumented with OpenTelemetry and when this option is true a span will be created (if sampled).
default: False
pub fn transaction(
pool: Connection,
callback: fn(Connection) -> Result(a, String),
) -> Result(a, TransactionError)
Runs a function within a PostgreSQL transaction.
If the function returns an Ok
then the transaction is committed.
If the function returns an Error
or panics then the transaction is rolled
back.
pub fn url_config(database_url: String) -> Result(Config, Nil)
Parse a database url into configuration that can be used to start a pool.