Repository navigation
Connection.Class.Connection
tedious-plus / Connection / Connection
Defined in: Connection.ts:75
A wrapper class for the Tedious Connection class.
This class provides promisified methods and conveniences to work with a database connection.
Usage:
await using conn = new Connection(config);
await conn.open();
const rows = await conn.queryRows("select name from sys.databases");AsyncDisposable
new Connection(configOrConnection): Connection;Defined in: Connection.ts:88
Creates a Connection object. The connection is not yet opened.
Connection | ConnectionConfiguration
A Tedious connection configuration, from which a new Tedious Connection is created,
or an existing Tedious Connection object.
Connection
get catalog(): CatalogConnExt;Defined in: Connection.ts:98
Catalog functions, e.g. for resolving table names and querying primary keys. See CatalogConnExt.
get tediousConnection(): Connection;Defined in: Connection.ts:113
The underlying Tedious Connection object.
Connection
get transfer(): DataTransferConnExt;Defined in: Connection.ts:106
Data transfer functions, e.g. for exporting query results and tables. See DataTransferConnExt.
[asyncDispose](): Promise<void>;Defined in: Connection.ts:140
Closes the connection. Called implicitly at the end of the scope of an await using declaration.
Promise<void>
AsyncDisposable.[asyncDispose]
close(): Promise<void>;Defined in: Connection.ts:134
Closes the connection.
Promise<void>
A promise that is resolved when the connection has ended. The promise is always resolved, even if the connection has failed before.
execSql(sql, options?): Promise<number>;Defined in: Connection.ts:167
Executes SQL code that does not return a result set, e.g. insert, update or DDL statements.
Without parameters, the SQL code is executed as a batch (Tedious Connection.execSqlBatch()) in the
scope of the session. Changed settings and local temporary tables (#tmp) persist in the session.
With parameters, the SQL code is executed with sp_executesql (Tedious Connection.execSql())
in a nested scope. Local temporary tables created by the SQL code are dropped when it finishes,
and most changed settings are restored.
Usage:
await conn.execSql("insert into t1 (id, name) values (@id, @name)", {params: {id: 123, name: "abc"}});string
The SQL code to be executed.
Optional request options and SQL parameters.
Promise<number>
A promise for the number of rows affected. The promise is rejected when the execution fails.
open(): Promise<void>;Defined in: Connection.ts:124
Opens the connection to the database server.
See ConnectionPromises.connect for details.
Promise<void>
A promise that is resolved when the connection has been established and the login has completed.
query(sql, options?): Promise<RequestResult<"ObjectRow">>;Defined in: Connection.ts:195
Executes an SQL query and returns the rows of the result set, together with the row count and the metadata.
All rows are buffered in memory. Use Connection.queryIterator to process large result sets.
Usage:
const result = await conn.query("select * from t1 where id = @id", {params: {id: 123}});
console.log(result.metadata, result.rows);string
The SQL query to be executed.
QueryOptions<"ObjectRow">
Optional request options, SQL parameters and the row type.
Promise<RequestResult<"ObjectRow">>
A promise for the result of the query. The promise is rejected when the execution fails.
query<RT>(sql, options): Promise<RequestResult<RT>>;Defined in: Connection.ts:196
Executes an SQL query and returns the rows of the result set, together with the row count and the metadata.
All rows are buffered in memory. Use Connection.queryIterator to process large result sets.
Usage:
const result = await conn.query("select * from t1 where id = @id", {params: {id: 123}});
console.log(result.metadata, result.rows);RT extends RowType
string
The SQL query to be executed.
QueryOptions<RT> & object
Optional request options, SQL parameters and the row type.
Promise<RequestResult<RT>>
A promise for the result of the query. The promise is rejected when the execution fails.
queryIterator(sql, options?): AsyncRowIterator<"ObjectRow">;Defined in: Connection.ts:294
Executes an SQL query and returns an iterator to process the rows as soon as they arrive from the server.
Usage:
for await (const row of conn.queryIterator("select id, name from t1")) {
console.log(row);
}Only one request can be active on a connection. The iteration must be completed or terminated before
the next request is executed on the connection. Exiting the for await loop early terminates it.
See AsyncRowIterator for details.
string
The SQL query to be executed.
QueryIteratorOptions<"ObjectRow">
Optional request options, SQL parameters, the row type and settings for the iterator.
AsyncRowIterator<"ObjectRow">
An iterator for the rows. When the request fails, the error is thrown within the for await loop.
queryIterator<RT>(sql, options): AsyncRowIterator<RT>;Defined in: Connection.ts:295
Executes an SQL query and returns an iterator to process the rows as soon as they arrive from the server.
Usage:
for await (const row of conn.queryIterator("select id, name from t1")) {
console.log(row);
}Only one request can be active on a connection. The iteration must be completed or terminated before
the next request is executed on the connection. Exiting the for await loop early terminates it.
See AsyncRowIterator for details.
RT extends RowType
string
The SQL query to be executed.
QueryOptions<RT> & AsyncRowIteratorOptions<RT> & object
Optional request options, SQL parameters, the row type and settings for the iterator.
AsyncRowIterator<RT>
An iterator for the rows. When the request fails, the error is thrown within the for await loop.
queryRows(sql, options?): Promise<ObjectRow[]>;Defined in: Connection.ts:222
Executes an SQL query and returns the rows of the result set.
All rows are buffered in memory. Use Connection.queryIterator to process large result sets.
Usage:
const objectRows = await conn.queryRows("select id, name from t1");
// Output: [ { id: 123, name: 'abc' }, ... ]
const arrayRows = await conn.queryRows("select id, name from t1", {rowType: "ArrayRow"});
// Output: [ [ 123, 'abc' ], ... ]string
The SQL query to be executed.
QueryOptions<"ObjectRow">
Optional request options, SQL parameters and the row type.
Promise<ObjectRow[]>
A promise for the rows. The promise is rejected when the execution fails.
queryRows<RT>(sql, options): Promise<Row<RT>[]>;Defined in: Connection.ts:223
Executes an SQL query and returns the rows of the result set.
All rows are buffered in memory. Use Connection.queryIterator to process large result sets.
Usage:
const objectRows = await conn.queryRows("select id, name from t1");
// Output: [ { id: 123, name: 'abc' }, ... ]
const arrayRows = await conn.queryRows("select id, name from t1", {rowType: "ArrayRow"});
// Output: [ [ 123, 'abc' ], ... ]RT extends RowType
string
The SQL query to be executed.
QueryOptions<RT> & object
Optional request options, SQL parameters and the row type.
Promise<Row<RT>[]>
A promise for the rows. The promise is rejected when the execution fails.
queryScalar(sql, options?): Promise<any>;Defined in: Connection.ts:249
Executes an SQL query and returns a single value, i.e. the first column of the first row of the result set.
If the result set is empty, null is returned.
Without the dataType option, the value is returned with the type any, because its type is given
by the SQL expression. With the dataType option, the data type of the value is verified at runtime.
Usage:
const count = await conn.queryScalar("select count(*) from t1", {dataType: "number"});string
The SQL query to be executed.
QueryScalarOptions<"any">
Optional request options, SQL parameters and the expected data type.
Promise<any>
A promise for the value. The promise is rejected when the execution fails or when the
value does not match the dataType option.
queryScalar<DT>(sql, options): Promise<JsValue<DT>>;Defined in: Connection.ts:250
Executes an SQL query and returns a single value, i.e. the first column of the first row of the result set.
If the result set is empty, null is returned.
Without the dataType option, the value is returned with the type any, because its type is given
by the SQL expression. With the dataType option, the data type of the value is verified at runtime.
Usage:
const count = await conn.queryScalar("select count(*) from t1", {dataType: "number"});DT extends JsValueDataType
string
The SQL query to be executed.
QueryScalarOptions<JsValueDataType> & object
Optional request options, SQL parameters and the expected data type.
Promise<JsValue<DT>>
A promise for the value. The promise is rejected when the execution fails or when the
value does not match the dataType option.