SQLTransactionState
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?
protectedoptionalconnection?:SQLTransactionConnection
Defined in: packages/sql/src/transaction.ts:75
The open connection, once the first statement opened it
failure?
protectedoptionalfailure?: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
protectedreadonlyfallback: () =>SQLClient|undefined
Defined in: packages/sql/src/transaction.ts:101
the client of the statements issued after the end
Returns
SQLClient | undefined
finished
protectedfinished:boolean=false
Defined in: packages/sql/src/transaction.ts:87
True once committed or rolled back
open
protectedreadonlyopen: (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?
protectedoptionalopening?:Promise<SQLTransactionConnection>
Defined in: packages/sql/src/transaction.ts:79
Pending or completed opening of the connection
owner?
readonlyoptionalowner?:object
Defined in: packages/sql/src/transaction.ts:102
the object owning the state (the store that created it)
rollbackHooks
protectedrollbackHooks: (() =>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()
protectedconnect():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()
protectedfallbackClient():SQLClient
Defined in: packages/sql/src/transaction.ts:167
Return the client of the statements issued after the end
Returns
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
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()
protectedrollbackConnection(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
the open connection
Returns
Promise<void>
runRollbackHooks()
protectedrunRollbackHooks():void
Defined in: packages/sql/src/transaction.ts:255
Run the rollback hooks once
Returns
void