Skip to main content

Microsoft SQL Server

@sqb/mssql is the SQB adapter for Microsoft SQL Server. It's built on top of mssql, which itself wraps the pure-JavaScript tedious driver — no native build step is required.

Install

npm install @sqb/mssql mssql

@sqb/connect and @sqb/builder are peer dependencies pulled in transitively if you already depend on them directly; otherwise install them alongside.

Registering the adapter

Importing @sqb/mssql registers it as a side effect — there's nothing else to wire up:

import '@sqb/mssql';
import { SqbClient } from '@sqb/connect';

const client = new SqbClient({
dialect: 'mssql', // or driver: 'mssql'
host: 'localhost',
database: 'mydb',
user: 'myuser',
password: 'mypassword',
});

Under the hood, @sqb/mssql's entry point imports @sqb/mssql-dialect (which teaches @sqb/builder T-SQL syntax) and registers a MssqlAdapter instance with AdapterRegistry. MssqlAdapter reports driver: 'mssql' and dialect: 'mssql'.

Configuration

host (mapped to server), port, user, password, and database from ClientConfiguration map onto the mssql driver's own config object. driverOptions.encrypt and driverOptions.trustServerCertificate set config.options.encrypt/.trustServerCertificate (defaulting to false/true respectively when not given), and driverOptions.options is merged into config.options on top of those two defaults:

new SqbClient({
dialect: 'mssql',
host: 'localhost',
database: 'mydb',
driverOptions: {
encrypt: true,
trustServerCertificate: false,
},
});
note

The rest of driverOptions is spread directly onto the top-level mssql config object, so it can carry any option the driver supports (e.g. pool, connectionTimeout) that isn't already covered by the standard fields. Because of the merge order, a top-level driverOptions.options object replaces the encrypt/trustServerCertificate defaults wholesale rather than merging with them — set encrypt/trustServerCertificate at the top level of driverOptions (as above) rather than nested under driverOptions.options if you want them merged with the defaults.

Feature notes

  • Cursors: supported (features.cursor: true), backed by the driver's request streaming — see Cursors & Streaming.
  • Schemas: not exposed through features.schemasetSchema()/getSchema() aren't implemented by this adapter.
  • RETURNING: SQB's RETURNING clause is translated to T-SQL's OUTPUT INSERTED.col / OUTPUT DELETED.col syntax, inserted before the statement's VALUES/WHERE clause.
  • Named parameters: SQB's :paramName placeholders are normalized to T-SQL's @paramName syntax, with awareness of [bracketed identifiers] so a :-like sequence inside one isn't mistaken for a parameter.

Migrator support

warning

@sqb/migrator currently only implements its migration adapter for PostgreSQL — see Running migrations. SQL Server migrations must be managed outside @sqb/migrator today.

See also