SqbConnection
SqbConnection wraps a single live Adapter.Connection checked out
of an SqbClient's pool. It runs queries, manages transactions and
savepoints, and reads/changes the active schema. See
Acquiring Connections,
Executing Queries,
Transactions and
Schemas for full usage guides.
SqbConnection extends an AsyncEventEmitter and emits close, execute, error, retain,
release, start-transaction, set-savepoint, release-savepoint, rollback-savepoint,
commit and rollback.
Constructor
new SqbConnection(client: SqbClient, adapterConnection: Adapter.Connection, options?: ConnectionOptions)
You don't normally construct a SqbConnection yourself — instances are returned by
SqbClient.acquire().
Properties
| Property | Type | Description |
|---|---|---|
client | SqbClient | The client this connection was acquired from. |
sessionId | string | Adapter-assigned identifier for the underlying session. |
refCount | number | Current reference count — see Acquiring Connections. |
inTransaction | boolean | true if a transaction is currently open on this connection. |
Methods
close()
close(): Promise<void>
Immediately releases the underlying adapter connection back to the pool, regardless of
refCount, and emits close.
commit()
commit(): Promise<void>
Commits the open transaction, sets inTransaction to false, and emits commit.
execute()
execute(query: string | Query, options?: QueryExecuteOptions): Promise<QueryResult>
Executes a query or Query builder object on this connection. See
Executing Queries for the full
option/defaults reference.
const result = await connection.execute(Select().from('customers'), { fetchRows: 200 });
getRepository()
getRepository<T>(entity: Type<T> | string, opts?: { schema?: string }): Repository<T>
Constructs a Repository bound to this connection. See the
ORM guides.
getSchema()
getSchema(): Promise<string>
Returns the connection's current active schema. Throws if the adapter doesn't support schemas.
release()
release(): boolean
Decrements refCount; once it reaches 0, closes the connection (returning it to the pool).
Returns true if the connection was closed by this call.
retain()
retain(): void
Increments refCount, keeping the connection open across an extra unit of work (e.g. an open
Cursor).
rollback()
rollback(): Promise<void>
Rolls back the open transaction, sets inTransaction to false, and emits rollback.
rollbackSavepoint()
rollbackSavepoint(savepoint: string): Promise<void>
Rolls back to a named savepoint without ending the whole transaction. Throws if the adapter doesn't support savepoints.
releaseSavepoint()
releaseSavepoint(savepoint: string): Promise<void>
Releases (discards) a named savepoint. Throws if the adapter doesn't support savepoints.
setSavepoint()
setSavepoint(savepoint: string): Promise<void>
Creates a named savepoint, starting a transaction first if one isn't already open. Throws if the adapter doesn't support savepoints.
setSchema()
setSchema(schema: string): Promise<void>
Switches the connection's active schema. Throws if the adapter doesn't support schemas.
startTransaction()
startTransaction(): Promise<void>
Begins a transaction on this connection, sets inTransaction to true, and emits
start-transaction.
test()
test(): Promise<void>
Delegates to the adapter connection's own test() method.