txMode.TransactionMode

new TransactionMode(tiLevelopt, readOnlyopt, deferrableopt) → {txMode.TransactionMode}

Alternative Syntax: TransactionMode({tiLevel, readOnly, deferrable}) ⇒ TransactionMode

Constructs a complete transaction-opening command, based on the parameters:

  • isolation level
  • access mode
  • deferrable mode

The type is available from the txMode namespace.

Parameters:
Name Type Attributes Description
tiLevel txMode.isolationLevel | object <optional>

Transaction Isolation Level, or an object with parameters, if the alternative syntax is used.

readOnly boolean <optional>

Sets transaction access mode based on the read-only flag:

  • undefined - access mode not specified (default)
  • true - access mode is set to READ ONLY
  • false - access mode is set to READ WRITE
deferrable boolean <optional>

Sets transaction deferrable mode based on the boolean value:

  • undefined - deferrable mode not specified (default)
  • true - mode is set to DEFERRABLE
  • false - mode is set to NOT DEFERRABLE

It is used only when tiLevel=isolationLevel.serializable and readOnly=true, or else it is ignored.

Source:
See:
Returns:
Type
txMode.TransactionMode
Example
const TransactionMode = pgp.txMode.TransactionMode;
const isolationLevel = pgp.txMode.isolationLevel;

// Create a reusable transaction mode (serializable + read-only + deferrable):
const tmSRD = new TransactionMode({
    tiLevel: isolationLevel.serializable,
    readOnly: true,
    deferrable: true
});

const myTransaction = t => {
    return t.any('SELECT * FROM table');
}

myTransaction.txMode = tmSRD; // assign transaction mode;

db.tx(myTransaction)
    .then(data => {
        // success;
    })
    .catch(error => {
        // error
    });

// Instead of the default BEGIN, such transaction will start with:

// BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE

Methods

begin(capopt) → {string}

Returns a complete BEGIN statement, according to all the parameters passed into the class.

This method is primarily for internal use by the library.

Parameters:
Name Type Attributes Default Description
cap boolean <optional>
false

Indicates whether the returned SQL must be capitalized.

Source:
Returns:
Type
string