///|
async fn _pool_quick_start(
host : String,
user : String,
database : String,
password : String,
) -> Unit {
@async.with_task_group(group => {
let config = Config::new(
host,
user~,
dbname=database,
password~,
application_name="my-service",
pool=PoolConfig::new(2),
)
let pool = Pool::new(config, group)
let value : Int = pool.with_client(client => {
client.query_one("select 1::int4 as value").get_name("value")
})
ignore(value)
pool.close()
})
}///|
async fn _pool_cancellable_example(pool : Pool) -> Unit {
@async.with_task_group(group => {
ignore(
pool.with_client(client => {
client.run_cancellable((op, token) => {
group.spawn_bg(no_wait=true, () => {
@async.sleep(50)
token.cancel()
})
let value : Int = op
.query_one("select pg_sleep(5), 1::int4 as value")
.get_name("value")
value
})
}),
) catch {
_ => ()
}
})
}///|
fn _multi_target_config() -> Config raise {
Config::new(
"primary.db",
user="moon",
dbname="app",
application_name="my-service",
hosts=["replica.db"],
ports=[5432, 5432],
target_session_attrs=TargetSessionAttrs::read_write(),
load_balance_hosts=Random,
pool=PoolConfig::new(
4,
timeouts=Timeouts::new(
wait_ms=Some(500),
create_ms=Some(1000),
recycle_ms=Some(250),
),
queue_mode=QueueMode::lifo(),
recycling_method=RecyclingMethod::verified(),
),
)
}///|
fn _pool_options_hook_example(group : @async.TaskGroup[Unit]) -> Pool raise {
let config = Config::new(
"db.example",
user="moon",
dbname="app",
application_name="my-service",
pool=PoolConfig::new(2),
)
let options = PoolOptions::new(
post_create=client => client.batch_execute("set search_path to app, public"),
pre_recycle=client => client.check_connection(),
)
Pool::new(config, group, options~)
}///|
async fn _statement_cache_example(pool : Pool) -> Unit {
pool.with_client(client => {
client.with_prepared_cached("select $1::int4 as value", prepared => {
let value = 7
let params : Array[&@client.ToSql] = [value as &@client.ToSql]
let row = prepared.query_one(params~)
let decoded : Int = row.get_name("value")
ignore(decoded)
})
})
|> ignore
let cache_size = pool.with_client(client => client.statement_cache().size())
ignore(cache_size)
pool.manager().statement_caches().clear()
}///|
async fn _detach_raw_example(pool : Pool) -> Unit {
let lease = pool.get()
let raw = lease.detach_raw()
let value : Int = raw.query_one("select 1::int4 as value").get_name("value")
ignore(value)
raw.close()
}pub(open) trait GenericClient {
async fn query_all(Self, String, params? : Array[&ToSql]) -> Array[Row]
async fn query_one(Self, String, params? : Array[&ToSql]) -> Row
async fn query_opt(Self, String, params? : Array[&ToSql]) -> Row?
async fn query_typed_all(Self, String, Array[Type], params? : Array[&ToSql]) -> Array[Row]
async fn execute(Self, String, params? : Array[&ToSql]) -> Int
async fn batch_execute(Self, String) -> Unit
async fn check_connection(Self) -> Unit
}pub suberror PoolError {
Closed
Timeout(TimeoutKind)
LeaseReleased
OperationInProgress
RowCount(String)
InvalidConfig(String)
} derive(Eq, Debug)type Clientimpl GenericClient for Clientasync fn[T] Client::run_cancellable(self : Client, f : async (Operation, OperationCancelToken) -> T) -> Tasync fn[T] Client::with_prepared(self : Client, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Client::with_prepared_cached(self : Client, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Client::with_prepared_typed(self : Client, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Client::with_prepared_typed_cached(self : Client, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Client::with_simple_query(self : Client, sql : String, f : async (SimpleQueryStream) -> T) -> Tasync fn[T] Client::with_transaction(self : Client, f : async (Transaction) -> T, options? : TransactionOptions) -> Tpub struct Config {
user : String
password : String?
dbname : String
options : String?
application_name : String
ssl_mode : SslMode?
ssl_root_cert : String?
channel_binding : ChannelBinding?
host : String?
hosts : Array[String]?
hostaddr : String?
hostaddrs : Array[String]?
port : Int?
ports : Array[Int]?
connect_timeout_ms : Int?
keepalives : Bool?
keepalives_idle_s : Int?
target_session_attrs : TargetSessionAttrs?
load_balance_hosts : LoadBalanceHosts?
pool : PoolConfig
} derive(Eq, Debug)fn Config::new(host : String, hostaddr? : String, port? : Int, user~ : String, dbname? : String, password? : String, ssl_mode? : SslMode, ssl_root_cert? : String, channel_binding? : ChannelBinding, application_name~ : String, options? : String, connect_timeout_ms? : Int, keepalives? : Bool, keepalives_idle_s? : Int, hosts? : Array[String], hostaddrs? : Array[String], ports? : Array[Int], target_session_attrs? : TargetSessionAttrs, load_balance_hosts? : LoadBalanceHosts, pool~ : PoolConfig) -> Configtype Connectortype CopyInSinktype Managertype Operationasync fn[T] Operation::with_prepared(self : Operation, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Operation::with_prepared_cached(self : Operation, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Operation::with_prepared_typed(self : Operation, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Ttype OperationCancelTokentype Poolpub struct PoolConfig {
max_size : Int
timeouts : Timeouts
queue_mode : QueueMode
recycling_method : RecyclingMethod
} derive(Eq, Debug)fn PoolConfig::new(max_size : Int, timeouts? : Timeouts, queue_mode? : QueueMode, recycling_method? : RecyclingMethod) -> PoolConfig raisefn PoolOptions::new(connector? : Connector, post_create? : async (Client) -> Unit, pre_recycle? : async (Client) -> Unit, post_recycle? : async (Client) -> Unit) -> PoolOptionsasync fn PreparedStatement::query_all(self : PreparedStatement, params? : Array[&ToSql]) -> Array[Row]async fn[T] PreparedStatement::with_portal(self : PreparedStatement, params? : Array[&ToSql], f : async (Portal) -> T) -> Ttype SimpleQueryStreamtype StatementCacheasync fn StatementCache::remove(self : StatementCache, sql : String, param_types? : Array[Type]) -> Unittype StatementCachesasync fn StatementCaches::remove(self : StatementCaches, sql : String, param_types? : Array[Type]) -> Unittype Transactionimpl GenericClient for Transactionasync fn Transaction::query_all(self : Transaction, sql : String, params? : Array[&ToSql]) -> Array[Row]async fn Transaction::query_typed_all(self : Transaction, sql : String, param_types : Array[Type], params? : Array[&ToSql]) -> Array[Row]async fn[T] Transaction::with_prepared(self : Transaction, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_prepared_cached(self : Transaction, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_prepared_typed(self : Transaction, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_prepared_typed_cached(self : Transaction, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_savepoint(self : Transaction, name : String, f : async (Transaction) -> T) -> Tasync fn[T] Transaction::with_stream(self : Transaction, sql : String, params? : Array[&ToSql], f : async (RowStream) -> T) -> Tasync fn[T] Transaction::with_typed_stream(self : Transaction, sql : String, param_types : Array[Type], params? : Array[&ToSql], f : async (RowStream) -> T) -> Tpub struct TransactionOptions {
isolation_level : IsolationLevel?
read_only : Bool?
deferrable : Bool?
} derive(Eq, Debug)fn TransactionOptions::new(isolation_level? : IsolationLevel, read_only? : Bool, deferrable? : Bool) -> TransactionOptionsInstall
Download zipA secure, easy-to-use PostgreSQL client library for MoonBit with an included connection pool.
Dependencies