Skip to content

API Connection

mlaanderson edited this page Dec 12, 2017 · 10 revisions

Syntax

new Connection( connection: string|ConnectionObject, driver?: DatabaseDriver )

Arguments

connection

Type: string or ConnectionObject

Defines the connection parameters, including driver name, host name, credentials, and database.

driver

Type: DatabaseDriver

An optional parameter. Used to pass in a specific driver class. If this is specified, the driver name is ignored in the connection argument.

Properties

URL

Type: string

Access: read only

Returns the derived connection URL string. Whether the connection is parsed from a passed string, or derived from a ConnectionObject, this property is always made up of the underlying ConnectionObject properties. This means it may be different from the passed value.

Driver

Type: DatabaseDriver

Access: read only

Returns a reference to the class used for the database driver.

Methods

createStatement

Syntax

connection.createStatement( sql: string ) => Statement
Arguments
sql

Type: string

The SQL string in the dialect of the underlying driver, with the exception that the parameter token is always a question mark ?.

Returns

Type: Statement

A statement object which will be used for further database actions. This statement is reusable.

prepareStatement

Syntax

connection.prepareStatement( sql: string ) => PreparedStatement
Arguments
sql

Type: string

The SQL string in the dialect of the underlying driver, with the exception that the parameter token is always a question mark ?.

Returns

Type: PreparedStatement

A prepared statement object which will be used for further database actions. This statement is reusable.

This is the preferred method for creating reusable statements as it forces the preparation of the statement which significantly reduces the risk of SQL injection attacks.

close

Syntax

connection.close() => Promise<boolean>

Closes the current connection, or releases it back to the pool if it was opened from a StaticPool or DynamicPool

Returns

Type: Promise

Returns a boolean promise that always resolves to true unless an error occurs in which case the promise is rejected.

isTransactionSupported

Syntax

connection.isTransactionSupported() => boolean

Indicates whether the underlying driver supports transactions.

Returns

Type: boolean

Returns true if the driver supports transactions, false if it does not or if the driver is a pre 1.3.0 driver which does not indicate transaction support.

inTransaction

Syntax

connection.inTransaction() => boolean

Indicates whether the underlying driver is currently in a transaction.

Returns

Type: boolean

Returns true if the driver is currently in a transaction, false if it is not in a transaction or does not support transactions.

beginTransaction

Syntax

connection.beginTransaction() => Promise<boolean>

Begins a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully started.

Returns

Type: Promise

Returns a boolean promise which resolves to true if a transaction was started and false if one was not started. Transactions can fail to start if the driver does not support transactions.

commit

Syntax

connection.commit() => Promise<boolean>

Commits a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully committed.

Returns

Type: Promise

Returns a boolean promise which resolves to true if a transaction was committed and false if one was not committed. Transactions can fail to commit if no transaction was started, or if the driver does not support transactions.

rollback

Syntax

connection.rollback() => Promise<boolean>

Rolls back a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully rolled back.

Returns

Type: Promise

Returns a boolean promise which resolves to true if a transaction was rolled back and false if one was not rolled back. Transactions can fail to rolled back if no transaction was started, or if the driver does not support transactions.