{"library":"mssql","title":"Microsoft SQL Server Client for Node.js","description":"mssql is a robust and actively maintained Node.js client for Microsoft SQL Server, currently stable at version 12.3.1. It features a rapid release cadence, with multiple minor and patch versions frequently released, often within weeks, and major versions roughly annually. The library supports multiple underlying TDS drivers: the pure JavaScript `Tedious` driver (default) and the native `MSNodeSQLv8` driver, offering flexibility based on deployment environment and performance needs. It provides a modern API with support for async/await, Promises, ES6 tagged template literals, and streaming, alongside traditional callbacks. Key differentiators include its comprehensive API for managing connections, connection pools, and requests, as well as explicit support for SQL Server features like input/output parameters, stored procedure execution, and bulk inserts.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mssql"],"cli":null},"imports":["import sql from 'mssql'","import { TYPES } from 'mssql'","import { Request } from 'mssql'","import sql from 'mssql/msnodesqlv8'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import sql from 'mssql'; // Use require('mssql') for CommonJS\n\nconst sqlConfig = {\n  user: process.env.DB_USER ?? 'SA', // Use 'SA' or another user if not using Windows Auth\n  password: process.env.DB_PASSWORD ?? 'YourStrong@Password123!',\n  database: process.env.DB_NAME ?? 'master',\n  server: process.env.DB_SERVER ?? 'localhost', // or your server IP/hostname\n  pool: {\n    max: 10,\n    min: 0,\n    idleTimeoutMillis: 30000\n  },\n  options: {\n    encrypt: process.env.DB_ENCRYPT === 'true', // For Azure SQL, this should be true\n    trustServerCertificate: process.env.NODE_ENV !== 'production' // Change to true for local dev / self-signed certs\n  }\n};\n\nasync function runDatabaseOperations() {\n  let pool; // Declare pool outside try block for finally access\n  try {\n    pool = await sql.connect(sqlConfig);\n    console.log('Successfully connected to SQL Server.');\n\n    // Example 1: Insert data using a parameterized query\n    const newProductName = 'Test Product ' + Date.now();\n    const insertResult = await pool.request()\n      .input('productName', sql.NVarChar, newProductName)\n      .input('price', sql.Decimal(10, 2), 19.99)\n      .query`INSERT INTO Products (Name, Price) VALUES (@productName, @price); SELECT SCOPE_IDENTITY() AS NewId;`;\n    console.log(`Inserted new product with ID: ${insertResult.recordset[0].NewId}`);\n\n    // Example 2: Select data\n    const selectResult = await pool.query`SELECT Id, Name, Price FROM Products WHERE Name LIKE '%Test Product%';`;\n    console.log('Selected products:', selectResult.recordset);\n\n    // Example 3: Execute a stored procedure (assuming 'GetProductById' exists)\n    // const productId = insertResult.recordset[0].NewId;\n    // const spResult = await pool.request()\n    //   .input('id', sql.Int, productId)\n    //   .execute('GetProductById');\n    // console.log('Stored procedure result:', spResult.recordset);\n\n  } catch (err) {\n    console.error('Database operation failed:', err.message);\n    // Implement robust error handling here, e.g., logging, retries\n  } finally {\n    if (pool) {\n      await pool.close();\n      console.log('Database connection closed.');\n    }\n  }\n}\n\n// Ensure a Products table exists for the examples\n// CREATE TABLE Products (Id INT IDENTITY(1,1) PRIMARY KEY, Name NVARCHAR(255), Price DECIMAL(10, 2));\n// Make sure to set DB_USER, DB_PASSWORD, DB_NAME, DB_SERVER environment variables\nrunDatabaseOperations();","lang":"typescript","description":"This quickstart demonstrates how to establish a connection to SQL Server using a configuration object, perform parameterized inserts, retrieve data using a tagged template literal query, and properly close the connection pool. It uses environment variables for sensitive credentials and shows basic error handling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}