Skip to main content

Pool

new Pool([config: String | PoolConfiguration)

Properties

KeyTypeReadonlyDescription
configPoolConfiguration]trueReturns configuration object
acquiredConnectionsnumbertrueReturns number of connections that are currently acquired
idleConnectionsnumbertrueReturns number of unused connections in the pool
acquiredConnectionsnumbertrueReturns number of connections that are currently acquired
totalConnectionsnumbertrueReturns total number of connections in the pool regardless of whether they are idle or in use

Methods

acquire()

Obtains a connection from the connection pool

acquire(): Promise<PoolConnection>

import { Pool } from 'postgrejs';

const pool = new Pool('postgres://localhost');
const connection = await pool.acquire();
// ...
await connection.close();

close()

Shuts down the pool and destroys all resources.

close(terminateWait?: number): Promise<void>

import { Pool } from 'postgrejs';

const pool = new Pool('postgres://localhost');
const connection = await pool.acquire();
// ...
await pool.close(5000);

execute()

Acquires a connection from the pool and executes single or multiple SQL scripts using Simple Query protocol.

execute(sql: string, options?: ScriptExecuteOptions): Promise<ScriptResult>;

ArgumentTypeDefaultDescription
sqlstringSQL script that will be executed
optionsScriptExecuteOptionsExecute options
import { Pool } from 'postgrejs';

const pool = new Pool('postgres://localhost');
const executeResult = await pool.execute(
'BEGIN; update my_table set ref=1 where id=1; END;');
// ...
await pool.close();

query()

Acquires a connection from the pool and executes single SQL script using Extended Query protocol.

query(sql: string, options?: ScriptExecuteOptions): Promise<ScriptResult>;

ArgumentTypeDefaultDescription
sqlstringSQL script that will be executed
optionsQueryOptionsExecute options
import { Pool } from 'postgrejs';

const pool = new Pool('postgres://localhost');
const queryResult = await pool.query(
'select * from my_table', {
cursor: true,
utcDates: true
});
let row;
while ((row = await queryResult.cursor.next())) {
// ....
}
await pool.close();

prepare()

Acquires a connection from the pool and creates a PreparedStatement instance.

prepare(sql: string, options?: StatementPrepareOptions): Promise<PreparedStatement>

ArgumentTypeDefaultDescription
sqlstringSQL script that will be executed
optionsStatementPrepareOptionsOptions
import { Pool, DataTypeOIDs } from 'postgrejs';

const pool = new Pool('postgres://localhost');
const statement = await pool.prepare(
'insert into my_table (ref_number) ($1)', {
paramTypes: [DataTypeOIDs.Int4]
});
// Bulk insert 100 rows
for (let i = 0; i < 100; i++) {
await statement.execute({params: [i]});
}
await statement.close();

release()

Releases a connection

release(connection: Connection): Promise<void>

listen()

Registers the pool as a listener on the notification channel.

listen(channel: string, callback: NotificationCallback): Promise<void>

ArgumentTypeDefaultDescription
channelstringName of the channel
callbackNotificationCallbackListener callback function
await pool.listen('my_event', (msg: NotificationMessage)=>{
console.log(msg.channel+ ' event fired!. processId:', msg.processId, ' payload:', msg.payload);
});

unListen()

Removes existing registration for NOTIFY events for given channel.

unListen(channel: string): Promise<void>

ArgumentTypeDefaultDescription
channelstringName of the channel
await pool.unListen('my_event');

unListenAll()

Removes existing registration for NOTIFY events for all channels.

unListenAll(): Promise<void>

await pool.unListenAll();