Skip to content

@tsdrivers/mssql / MssqlConnection

Class: MssqlConnection ​

Defined in: projects/ts-mssql/core/connection.ts:51

A single connection to SQL Server.

Use await using for automatic cleanup:

ts
await using cn = await mssql.connect(connectionString);
const rows = await cn.query<User>("SELECT * FROM Users");

Implements ​

  • Disposable
  • AsyncDisposable

Accessors ​

blob ​

Get Signature ​

get blob(): BlobAccessor

Defined in: projects/ts-mssql/core/connection.ts:270

Cross-platform VARBINARY(MAX) blob streaming. Requires a transaction.

ts
cn.blob.filestream.read(tx, target)  // node:stream Readable
cn.blob.filestream.write(tx, target) // node:stream Writable
cn.blob.webstream.read(tx, target)   // Web ReadableStream
cn.blob.webstream.write(tx, target)  // Web WritableStream
Returns ​

BlobAccessor


fs ​

Get Signature ​

get fs(): FilestreamAccessor

Defined in: projects/ts-mssql/core/connection.ts:256

FILESTREAM access (Windows only).

ts
cn.fs.open(path, ctx, "read")    // node:stream Readable/Writable/Duplex
cn.fs.openWeb(path, ctx, "read") // Web ReadableStream/WritableStream
cn.fs.available()                // check availability
Returns ​

FilestreamAccessor

Methods ​

[asyncDispose]() ​

[asyncDispose](): Promise<void>

Defined in: projects/ts-mssql/core/connection.ts:299

Returns ​

Promise<void>

Implementation of ​

AsyncDisposable.[asyncDispose]


[dispose]() ​

[dispose](): void

Defined in: projects/ts-mssql/core/connection.ts:287

Returns ​

void

Implementation of ​

Disposable.[dispose]


beginTransaction() ​

beginTransaction(isolation?): Promise<Transaction>

Defined in: projects/ts-mssql/core/connection.ts:218

Begin a transaction with the given isolation level.

Parameters ​

isolation? ​

IsolationLevel = "READ_COMMITTED"

Returns ​

Promise<Transaction>


bulk() ​

bulk(table): BulkInsertBuilder

Defined in: projects/ts-mssql/core/connection.ts:212

Create a bulk insert builder for the given table.

Parameters ​

table ​

string

Returns ​

BulkInsertBuilder


close() ​

close(): Promise<void>

Defined in: projects/ts-mssql/core/connection.ts:279

Close this connection permanently. For pooled connections, the connection is evicted (NOT returned to pool). Cascades: disposes transactions, closes streams, then destroys the connection.

Returns ​

Promise<void>


exec() ​

exec(sql, params?, opts?): Promise<ExecResult>

Defined in: projects/ts-mssql/core/connection.ts:142

Execute a stored procedure and return a rich result with OUTPUT parameters and multiple result sets.

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<ExecResult>


execute() ​

execute(sql, params?, opts?): Promise<number>

Defined in: projects/ts-mssql/core/connection.ts:123

Execute a non-query and return the number of rows affected.

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<number>


query() ​

query<T>(sql, params?, opts?): Promise<T[]>

Defined in: projects/ts-mssql/core/connection.ts:68

Execute a query and return all rows.

Type Parameters ​

T ​

T = Record<string, unknown>

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<T[]>


queryFirst() ​

queryFirst<T>(sql, params?, opts?): Promise<T | undefined>

Defined in: projects/ts-mssql/core/connection.ts:84

Execute a query and return the first row, or undefined.

Type Parameters ​

T ​

T = Record<string, unknown>

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<T | undefined>


querySingle() ​

querySingle<T>(sql, params?, opts?): Promise<T>

Defined in: projects/ts-mssql/core/connection.ts:94

Execute a query and return exactly one row. Throws if not exactly 1.

Type Parameters ​

T ​

T = Record<string, unknown>

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<T>


queryStream() ​

queryStream<T>(sql, params?, opts?): Promise<QueryStream<T>>

Defined in: projects/ts-mssql/core/connection.ts:181

Open a streaming query for large result sets.

Type Parameters ​

T ​

T = Record<string, unknown>

Parameters ​

sql ​

string

params? ​

Params

opts? ​

StreamOptions

Returns ​

Promise<QueryStream<T>>


scalar() ​

scalar<T>(sql, params?, opts?): Promise<T | undefined>

Defined in: projects/ts-mssql/core/connection.ts:107

Execute a query and return the first column of the first row.

Type Parameters ​

T ​

T = unknown

Parameters ​

sql ​

string

params? ​

Params

opts? ​

CommandOptions

Returns ​

Promise<T | undefined>


sql() ​

sql<T>(strings, ...values): Promise<T[]>

Defined in: projects/ts-mssql/core/connection.ts:158

Tagged template for parameterized queries.

Type Parameters ​

T ​

T = Record<string, unknown>

Parameters ​

strings ​

TemplateStringsArray

values ​

...ParamValue[]

Returns ​

Promise<T[]>


sqlWith() ​

sqlWith(opts): <T>(strings, ...values) => Promise<T[]>

Defined in: projects/ts-mssql/core/connection.ts:167

Tagged template with options.

Parameters ​

opts ​

CommandOptions

Returns ​

<T>(strings, ...values): Promise<T[]>

Type Parameters ​
T ​

T = Record<string, unknown>

Parameters ​
strings ​

TemplateStringsArray

values ​

...ParamValue[]

Returns ​

Promise<T[]>