Skip to main content

Connection

Constructor

new Connection([config: String | ConnectionConfiguration)

Properties

KeyTypeReadonlyDescription
configConnectionConfigurationtrueReturns configuration object
inTransactionbooleantrueReturns true if connection is in a transaction
stateConnectionStatetrueReturns current state of the connection
processIDnumbertrueReturns processId of current session
secretKeynumbertrueReturns secret key of current session
sessionParametersobjecttrueReturns information parameters for current session

Methods

connect()

Connects to the server

connect(): Promise<void>

import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
// ...

close()

For a single connection this call closes connection permanently. For a pooled connection it sends the connection back to the pool.

You can define how long time the connection will wait for active queries before closing. At the end of time, it forces to close/release and emits terminate event.

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

  • terminateWait: On the end of the given time, it forces to close the socket and than emits terminate event.
ArgumentTypeDefaultDescription
terminateWaitnumber10000Time in ms that the connection will wait for active queries before terminating
import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
connection.on('close', () => {
console.log('Connection closed');
});
connection.on('terminate', () => {
console.warn('Connection forced to terminate!');
});
// ...
await connection.close(30000); // will wait 30 secs before terminate the connection

Connection already supports TC30 Explicit Resource Management proposal.

import { Connection } from 'postgrejs';
{
// connection will be automatically closed when this scope ends
await using connection = new Connection('postgres://localhost');
await connection.connect();
}

execute()

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 { Connection } from 'postgrejs';

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

query()

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 { Connection } from 'postgrejs';

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

prepare()

Creates a PreparedStatement instance

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

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

const connection = new Connection('postgres://localhost');
await connection.connect();
const statement = await connection.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();

startTransaction()

Starts a transaction

startTransaction(): Promise<void>

import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
await connection.startTransaction();
const executeResult = await connection.execute(
'update my_table set ref=1 where id=1');
// ...... commit or rollback
await connection.close();

commit()

Commits current transaction

commit(): Promise<void>

import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
await connection.startTransaction();
const executeResult = await connection.execute(
'update my_table set ref=1 where id=1');
await connection.commit();
await connection.close();

rollback()

Rolls back current transaction

commit(): Promise<void>

import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
await connection.startTransaction();
const executeResult = await connection.execute(
'update my_table set ref=1 where id=1');
await connection.commit();
await connection.close();

savepoint()

Starts transaction and creates a savepoint

savepoint(name: string): Promise<void>

ArgumentTypeDefaultDescription
namestringName of the savepoint

rollbackToSavepoint()

Rolls back current transaction to given savepoint

savepoint(name: string): Promise<void>

ArgumentTypeDefaultDescription
namestringName of the savepoint
import { Connection } from 'postgrejs';

const connection = new Connection('postgres://localhost');
await connection.connect();
await connection.savepoint('my_save_point');
const executeResult = await connection.execute(
'update my_table set ref=1 where id=1');
await connection.rollbackToSavepoint('my_save_point');
await connection.close();

listen()

Registers the connection as a listener on the notification channel.

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

ArgumentTypeDefaultDescription
channelstringName of the channel
callbackNotificationCallbackListener callback function
await connection.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 connection.unListen('my_event');

unListenAll()

Removes existing registration for NOTIFY events for all channels.

unListenAll(): Promise<void>

await connection.unListenAll();

Events

error

Triggered when an error occurs.

(err: Error) => void

ArgumentTypeDefaultDescription
errErrorError instance

close

Triggered when after connection closed.

() => void

connecting

Triggered when establishing a connection.

() => void

ready

Triggered when connection is ready.

() => void

terminate

Triggered when the connection is terminated unintentionally.

() => void

notification

Triggered when notification is received from a registered channel.

(msg: NotificationMessage) => void

ArgumentTypeDefaultDescription
msgNotificationMessageNotification message instance