-
Notifications
You must be signed in to change notification settings - Fork 16
API Connection
A Connection provides a single connection to a database source. It provides the ability to interact with a database through a common set of commands.
new Connection( connection: string|ConnectionObject, driver?: DatabaseDriver )
Parameter | Type | Description |
---|---|---|
connection | string or ConnectionObject | Defines the connection parameters, including driver name, host name, credentials and database |
driver? | DatabaseDriver | Optional. Used to pass in a specific driver class. If this is specified, the driver name is ignored in the connection argument. |
Property | Type | Description |
---|---|---|
URL | string | 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 | DatabaseDriver | Returns a reference to the class used for the database driver. |
Creates a new statement for querying or executing against the database.
connection.createStatement( sql: string ) => Statement
Parameter | Type | Description |
---|---|---|
sql | string | The SQL string in the dialect of the underlying driver, with the exception that the parameter token is always a question mark ? . |
Type | Description |
---|---|
Statement | A statement object which will be used for further database actions. This statement is reusable. |
Creates a new prepared statement for querying or executing against the database.
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.
connection.prepareStatement( sql: string ) => PreparedStatement
Parameter | Type | Description |
---|---|---|
sql | string | The SQL string in the dialect of the underlying driver, with the exception that the parameter token is always a question mark ? . |
Type | Description |
---|---|
PreparedStatement | A prepared statement object which will be used for further database actions. This statement is reusable. |
Closes the current connection.
connection.close() => Promise<boolean>
Type | Description |
---|---|
Promise<boolean> | A boolean promise that always resolves to true unless an error occurs in which case the promise is rejected. |
Indicates whether the underlying driver supports transactions.
connection.isTransactionSupported() => boolean
Type | Description |
---|---|
boolean | 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. |
Indicates whether the underlying driver is currently in a transaction.
connection.inTransaction() => boolean
Type | Description |
---|---|
boolean | True if the driver is currently in a transaction. False if it is not in a transaction or does not support transactions. |
Begins a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully started.
connection.beginTransaction() => Promise<boolean>
Type | Description |
---|---|
Promise<boolean> | 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. |
Commits a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully committed.
connection.commit() => Promise<boolean>
Type | Description |
---|---|
Promise<boolean> | 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. |
Rolls back a transaction on the underlying driver. Returns a promise to indicate if the transaction was successfully rolled back.
connection.rollback() => Promise<boolean>
Type | Description |
---|---|
Promise<boolean> | 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. |