@eltjs/sdk-microsoft-sql-server
v0.13.38
Published
Reads one SQL Server database: discovery, exact reads, Change Tracking and change signals. It knows nothing of elt.
Maintainers
Readme
@eltjs/sdk-microsoft-sql-server
Reads one SQL Server database, and gives the sessions that write it. This SDK knows nothing of elt. It works with SQL Server 2016 or later, Azure SQL Database and Azure SQL Managed Instance. @eltjs/source-microsoft-sql-server uses it to make the streams of a pipeline.
import { SqlServerDatabase } from '@eltjs/sdk-microsoft-sql-server';
const database = new SqlServerDatabase({
server: 'localhost',
port: 1433,
database: 'sales',
user: 'reader',
password: 'secret',
});
await using session = await database.open();
for (const table of await session.tables()) {
console.log(table.quoted, table.changeTracking);
}SqlServerDatabasetakes the parts of a login (server,port,database,user,passwordandtrustServerCertificate), or an ADO.NET connection string, the form that the tools of SQL Server print. Both must name the server and the database. Anmssql://URL does not work. Use the parts when you have them: a connection string cuts a value at its first;and removes its braces, so a password that holds them gets to the server only through the parts. The connection is encrypted. SettrustServerCertificatefor a server whose certificate no authority signed, such as SQL Server in Docker.open({ signal })gives aSqlServerSession. Dispose the session to close its connections. When the signal aborts,opencancels its query, closes its connections and fails with the reason of the signal. The connection itself does not take the signal: the driver stops trying to connect after 15 seconds. Neither does theSELECT 1with which the pool checks a connection before it lends it: when the server stops sending, the pool stops waiting after 30 seconds. The session has up to four connections. The pool keeps a connection that is idle for 5 minutes, so a session that runs a query each 30 seconds, as the watch of a source does, logs in only once (ADR 0001).connect(clock)gives aSqlServerConnection: one session of the server, for work that keeps state from one statement to the next, such as a#temptable, the session context, a session lock or a transaction over several statements.query(text, parameters, { signal })runs one batch and gives the rows of each result set. Each parameter is annvarchar(max)orNULL, which the batch casts. A batch with parameters runs throughsp_executesql: it must end with the transaction count that it began with, and SQL Server drops each#temptable that it makes at its end. A batch with no parameters runs as it is, soBEGIN TRANSACTION,COMMITand a#temptable that the session keeps go in such a batch. When thesignalaborts, the connection sends SQL Server an attention (the message of the TDS protocol that cancels the batch that runs), andqueryfails with the reason of the signal after the server stops the batch. The session stays open, so the nextqueryruns on it. When the signal is already aborted,querysends nothing and fails with its reason. The connection has no time limit for a batch, so useAbortSignal.timeout(ms)to stop a batch after a time. When the connection runs no statement for a minute onclock, it runsSELECT 1, so a gateway that ends idle sessions, such as that of Azure SQL, keeps the session while its owner holds it. An app givesglobalThisas the clock. The connection never connects again: when the server or the network ends the session, it emitsend, and each laterqueryfails withSqlServerUnavailableError.pool()logs in once to check the login and gives aSqlServerPool, for statements that each stand alone, such as the renewal of a lease every few seconds. A login that the server refuses fails as it does foropen. Dispose the pool to close its connections.connect()gives aSqlServerPoolSession. Eachquery(text, parameters)of the session runs on a connection that the pool lends for that statement alone, so the session keeps no#temptable, session lock or transaction from one statement to the next. Each parameter is annvarchar(max).on('error')gets the errors of the connections of the pool untilclose(), and a closed session runs no statement. The pool has up to four connections and keeps an idle connection for 5 minutes, as the pool ofopendoes, so sessions a few seconds apart log in once. The session has the shape of the session ofSqlServerStoreof@zukhruf/mutex/sqlserver, which runs one statement on each session and closes it.tables({ signal })gives each user table that the login can read. A view is not in the list. EachSqlServerTablehas its columns, its primary key and its Change Tracking state.unreadablenames the columns that the login cannot read. Its queries read the metadata of every table, so an openTRUNCATEorALTERof any table blocks them. When the signal aborts, the session cancels them, asstatisticsdoes, andtablesfails with the reason of the signal.rows,page,rowversionsandchangestake aSqlServerRead: aclock, which an app gives asglobalThis, and asignal, which stops the read also when the server sends nothing. A read that gets no row between two checks an hour apart fails withSqlServerUnavailableError.rows,pageandrowversionsread rows of a table.changesgives the rows that changed after aChangePosition, each row once.version,statistics,rowCountsandlastWritesgive the values that show that a table changed.statistics,rowCountsandlastWritesread every table of the database, so an openTRUNCATEorALTERof any table blocks them. Each takes{ signal }: when the signal aborts, the session sends SQL Server an attention, asqueryofSqlServerConnectiondoes, and the read fails with the reason of the signal after the server stops the query.statistics,rowCountsandlastWritesalso takelockTimeout, the number of milliseconds that the query waits for a lock that another session holds. When the query waits longer, it fails withSqlServerLockTimeoutError(SQL Server error 1222). WithoutlockTimeout, the query waits as long as the lock is held. The lock timeout applies to that query only: the query runs throughsp_executesql, and SQL Server restoresSET LOCK_TIMEOUTat the end of that scope, also when the query fails or is cancelled. Thus the next statement on the pooled connection waits again as long as the lock is held.
Errors
| Error | The cause |
| --------------------------- | ------------------------------------------------------------------------------------------------- |
| SqlServerUnavailableError | The server cannot be reached. |
| SqlServerLoginError | The server refuses the login. |
| SqlServerPermissionError | The login does not have a permission. grant names the statement that gives the permission. |
| SqlServerLockTimeoutError | A query waited longer for a lock than its lockTimeout allows. A later attempt can succeed. |
| SqlServerVersionError | The server is older than SQL Server 2016. |
| ChangeHistoryExpiredError | The Change Tracking history no longer includes the position. Read the table again from the start. |
