Skip to main content

SQLTransactionState

@webda/sql


Class: SQLTransactionState

Defined in: packages/sql/src/transaction.ts:71

The transaction of a SQL store inside an operation: opened lazily by the first statement, then committed or rolled back once

It is the SQLClient of the repositories while the transaction is active; statements issued after its end (a promise left running by the operation) run on the fallback client, in autocommit.

Implements​

Constructors​

Constructor​

new SQLTransactionState(open, fallback, owner?): SQLTransactionState

Defined in: packages/sql/src/transaction.ts:99

Create a new SQLTransactionState instance

Parameters​

open​

(state) => Promise<SQLTransactionConnection>

open a connection and begin the transaction of this state on it

fallback​

() => SQLClient | undefined

the client of the statements issued after the end

owner?​

object

the object owning the state (the store that created it)

Returns​

SQLTransactionState

Properties​

connection?​

protected optional connection?: SQLTransactionConnection

Defined in: packages/sql/src/transaction.ts:75

The open connection, once the first statement opened it


failure?​

protected optional failure?: object

Defined in: packages/sql/src/transaction.ts:83

Error of the first failed statement: the transaction can only roll back

error​

error: unknown


fallback​

protected readonly fallback: () => SQLClient | undefined

Defined in: packages/sql/src/transaction.ts:101

the client of the statements issued after the end

Returns​

SQLClient | undefined


finished​

protected finished: boolean = false

Defined in: packages/sql/src/transaction.ts:87

True once committed or rolled back


open​

protected readonly open: (state) => Promise<SQLTransactionConnection>

Defined in: packages/sql/src/transaction.ts:100

open a connection and begin the transaction of this state on it

Parameters​

state​

SQLTransactionState

Returns​

Promise<SQLTransactionConnection>


opening?​

protected optional opening?: Promise<SQLTransactionConnection>

Defined in: packages/sql/src/transaction.ts:79

Pending or completed opening of the connection


owner?​

readonly optional owner?: object

Defined in: packages/sql/src/transaction.ts:102

the object owning the state (the store that created it)


rollbackHooks​

protected rollbackHooks: (() => void)[] = []

Defined in: packages/sql/src/transaction.ts:91

Called when the transaction is rolled back

Returns​

void

Methods​

commit()​

commit(): Promise<void>

Defined in: packages/sql/src/transaction.ts:181

Commit the transaction and release its connection; nothing to do when no statement opened it

An aborted transaction is rolled back and a SQLTransactionAbortedError thrown. When COMMIT fails, a ROLLBACK is attempted, the connection destroyed and the error thrown.

Returns​

Promise<void>


connect()​

protected connect(): Promise<SQLTransactionConnection>

Defined in: packages/sql/src/transaction.ts:133

Open the connection once, whatever the number of concurrent statements asking for it

Returns​

Promise<SQLTransactionConnection>

the connection


fallbackClient()​

protected fallbackClient(): SQLClient

Defined in: packages/sql/src/transaction.ts:167

Return the client of the statements issued after the end

Returns​

SQLClient

the client of the statements issued after the end


isFinished()​

isFinished(): boolean

Defined in: packages/sql/src/transaction.ts:109

Check whether finished

Returns​

boolean

true once committed or rolled back


isOpen()​

isOpen(): boolean

Defined in: packages/sql/src/transaction.ts:117

Check whether open

Returns​

boolean

true once a statement opened the connection


onRollback()​

onRollback(hook): void

Defined in: packages/sql/src/transaction.ts:125

Register a callback run when the transaction is rolled back (e.g. to forget a table created inside it)

Parameters​

hook​

() => void

the callback

Returns​

void


query()​

query(q, values?): Promise<{ rowCount: number; rows: any[]; }>

Defined in: packages/sql/src/transaction.ts:147

Run a statement in the transaction, opening it on the first statement

A failed statement aborts the transaction (PostgreSQL refuses any further statement, 25P02): the next ones throw a SQLTransactionAbortedError and the commit rolls back.

Parameters​

q​

string

the statement

values?​

any[]

the parameters

Returns​

Promise<{ rowCount: number; rows: any[]; }>

the result

Implementation of​

SQLClient.query


rollback()​

rollback(_error?): Promise<void>

Defined in: packages/sql/src/transaction.ts:218

Roll the transaction back and release its connection; nothing to do when no statement opened it

Parameters​

_error?​

unknown

the error failing the operation

Returns​

Promise<void>


rollbackConnection()​

protected rollbackConnection(connection): Promise<void>

Defined in: packages/sql/src/transaction.ts:241

ROLLBACK then release; a connection failing to roll back is destroyed and the error thrown

Parameters​

connection​

SQLTransactionConnection

the open connection

Returns​

Promise<void>


runRollbackHooks()​

protected runRollbackHooks(): void

Defined in: packages/sql/src/transaction.ts:255

Run the rollback hooks once

Returns​

void