Skip to content

ConnectionPlus.Class.ConnectionPlus

Christian d'Heureuse edited this page Oct 28, 2026 · 1 revision

tedious-plus / ConnectionPlus / ConnectionPlus

Class: ConnectionPlus

Defined in: ConnectionPlus.ts:73

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 ConnectionPlus(config);
await conn.open();
const rows = await conn.queryRows("select name from sys.databases");

Implements

  • AsyncDisposable

Constructors

Constructor

new ConnectionPlus(configOrConnection): ConnectionPlus;

Defined in: ConnectionPlus.ts:84

Creates a ConnectionPlus object. The connection is not yet opened.

Parameters

configOrConnection

Connection | ConnectionConfiguration

A Tedious connection configuration, from which a new Tedious Connection is created, or an existing Tedious Connection object.

Returns

ConnectionPlus

Accessors

connection

Get Signature

get connection(): Connection;

Defined in: ConnectionPlus.ts:93

The underlying Tedious Connection object.

Returns

Connection

Methods

[asyncDispose]()

[asyncDispose](): Promise<void>;

Defined in: ConnectionPlus.ts:120

Closes the connection. Called implicitly at the end of the scope of an await using declaration.

Returns

Promise<void>

Implementation of

AsyncDisposable.[asyncDispose]


close()

close(): Promise<void>;

Defined in: ConnectionPlus.ts:114

Closes the connection.

Returns

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()

execSql(sql, options?): Promise<number>;

Defined in: ConnectionPlus.ts:147

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"}});

Parameters

sql

string

The SQL code to be executed.

options?

CommonRequestOptions

Optional request options and SQL parameters.

Returns

Promise<number>

A promise for the number of rows affected. The promise is rejected when the execution fails.


open()

open(): Promise<void>;

Defined in: ConnectionPlus.ts:104

Opens the connection to the database server.

See ConnectionPromises.connect for details.

Returns

Promise<void>

A promise that is resolved when the connection has been established and the login has completed.


query()

Call Signature

query(sql, options?): Promise<RequestResult<"ObjectRow">>;

Defined in: ConnectionPlus.ts:175

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 ConnectionPlus.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);
Parameters
sql

string

The SQL query to be executed.

options?

QueryOptions<"ObjectRow">

Optional request options, SQL parameters and the row type.

Returns

Promise<RequestResult<"ObjectRow">>

A promise for the result of the query. The promise is rejected when the execution fails.

Call Signature

query<RT>(sql, options): Promise<RequestResult<RT>>;

Defined in: ConnectionPlus.ts:176

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 ConnectionPlus.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);
Type Parameters
RT

RT extends RowType

Parameters
sql

string

The SQL query to be executed.

options

QueryOptions<RT> & object

Optional request options, SQL parameters and the row type.

Returns

Promise<RequestResult<RT>>

A promise for the result of the query. The promise is rejected when the execution fails.


queryIterator()

Call Signature

queryIterator(sql, options?): AsyncRowIterator<"ObjectRow">;

Defined in: ConnectionPlus.ts:274

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.

Parameters
sql

string

The SQL query to be executed.

options?

QueryIteratorOptions<"ObjectRow">

Optional request options, SQL parameters, the row type and settings for the iterator.

Returns

AsyncRowIterator<"ObjectRow">

An iterator for the rows. When the request fails, the error is thrown within the for await loop.

Call Signature

queryIterator<RT>(sql, options): AsyncRowIterator<RT>;

Defined in: ConnectionPlus.ts:275

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.

Type Parameters
RT

RT extends RowType

Parameters
sql

string

The SQL query to be executed.

options

QueryOptions<RT> & AsyncRowIteratorOptions<RT> & object

Optional request options, SQL parameters, the row type and settings for the iterator.

Returns

AsyncRowIterator<RT>

An iterator for the rows. When the request fails, the error is thrown within the for await loop.


queryRows()

Call Signature

queryRows(sql, options?): Promise<ObjectRow[]>;

Defined in: ConnectionPlus.ts:202

Executes an SQL query and returns the rows of the result set.

All rows are buffered in memory. Use ConnectionPlus.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' ], ... ]
Parameters
sql

string

The SQL query to be executed.

options?

QueryOptions<"ObjectRow">

Optional request options, SQL parameters and the row type.

Returns

Promise<ObjectRow[]>

A promise for the rows. The promise is rejected when the execution fails.

Call Signature

queryRows<RT>(sql, options): Promise<Row<RT>[]>;

Defined in: ConnectionPlus.ts:203

Executes an SQL query and returns the rows of the result set.

All rows are buffered in memory. Use ConnectionPlus.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' ], ... ]
Type Parameters
RT

RT extends RowType

Parameters
sql

string

The SQL query to be executed.

options

QueryOptions<RT> & object

Optional request options, SQL parameters and the row type.

Returns

Promise<Row<RT>[]>

A promise for the rows. The promise is rejected when the execution fails.


queryScalar()

Call Signature

queryScalar(sql, options?): Promise<any>;

Defined in: ConnectionPlus.ts:229

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"});
Parameters
sql

string

The SQL query to be executed.

options?

QueryScalarOptions<"any">

Optional request options, SQL parameters and the expected data type.

Returns

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.

Call Signature

queryScalar<DT>(sql, options): Promise<JsValue<DT>>;

Defined in: ConnectionPlus.ts:230

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"});
Type Parameters
DT

DT extends JsValueDataType

Parameters
sql

string

The SQL query to be executed.

options

QueryScalarOptions<JsValueDataType> & object

Optional request options, SQL parameters and the expected data type.

Returns

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.

Clone this wiki locally