SQL Server Driver for Database-js
raw JSON →database-js-mssql is a JavaScript driver for SQL Server designed to integrate with the `database-js` abstraction layer. It acts as a wrapper around the popular `node-mssql` library, enabling `database-js` applications to connect and query SQL Server databases (versions 2000-2017). The package is currently at version 0.3.0. As a specialized driver, its release cadence is tied to updates in `database-js` or critical fixes in its underlying `node-mssql` dependency, suggesting a maintenance-oriented development. Its key differentiator is providing a consistent `database-js` interface for SQL Server, allowing for database-agnostic code when using the `database-js` ecosystem, rather than directly interacting with `node-mssql`. This facilitates easier database switching and a unified API for data access across different SQL platforms.
Common errors
error Error: No driver found for "mssql" ↓
npm install database-js-mssql has been run, and add require('database-js-mssql'); early in your application's entry point before creating any database-js connections. error Login failed for user 'username'. ↓
error ConnectionError: Failed to connect to servername\instancename in 15000ms ↓
Warnings
breaking The `close()` method now closes all connections managed by the connection pool, rather than just a single connection. This changes behavior for applications that relied on granular control over individual connections via `close()`. ↓
gotcha This package is a driver for `database-js` and is primarily a CommonJS module, indicated by `require` examples and older `engines.node` specification (>=4). Using ES Modules (`import`) directly for `database-js-mssql` might require specific configuration or a CJS wrapper in a modern ESM-only environment. ↓
gotcha The `database-js-mssql` driver relies on `node-mssql` as its underlying SQL Server client. An upgrade of `node-mssql` to version 4.2.2 (in `database-js-mssql` v0.2.2) was necessary to address a critical bug (Tedious issue #427). Ensure your `database-js-mssql` version is sufficiently recent to benefit from this and other upstream `node-mssql` fixes. ↓
gotcha The `database-js` abstraction layer, and by extension this driver, does not inherently support connection pooling beyond what `node-mssql` provides internally. For advanced connection management or multi-database scenarios, direct interaction with `node-mssql`'s pooling features or an external pool manager might be required.
Install
npm install database-js-mssql yarn add database-js-mssql pnpm add database-js-mssql Imports
- Connection wrong
import { Connection } from 'database-js-mssql';correctimport { Connection } from 'database-js'; - database-js-mssql wrong
import 'database-js-mssql';correctrequire('database-js-mssql'); - MssqlDriver wrong
const MssqlDriver = require('database-js-mssql').MssqlDriver;correctimport { MssqlDriver } from 'database-js-mssql';
Quickstart
import { Connection } from 'database-js';
(async () => {
// Ensure the driver is loaded, even if not directly referenced
require('database-js-mssql');
let conn, statement, results;
try {
// Use environment variables for sensitive connection details
const user = process.env.DB_USER ?? 'username';
const password = process.env.DB_PASSWORD ?? 'password';
const server = process.env.DB_SERVER ?? 'localhost';
const database = process.env.DB_DATABASE ?? 'master';
// Connection string format: mssql:///user:password@server/database
conn = new Connection(`mssql:///${user}:${password}@${server}/${database}`);
console.log('Successfully connected to SQL Server.');
// Prepare a statement with a placeholder
statement = conn.prepareStatement("SELECT * FROM states WHERE state = ?");
// Execute the query with a parameter
results = await statement.query("South Dakota");
console.log('Query results:', results);
} catch (reason) {
console.error('An error occurred:', reason);
} finally {
if (conn) {
await conn.close();
console.log('Connection closed.');
}
}
})();