The mssql-dbOperations
package provides a simplified interface for performing database operations on Microsoft SQL Server databases. It encapsulates connection handling, query execution, and basic CRUD operations into a reusable and modular class.
- Connection pool management.
- Automated reconnection on connection loss.
- Simplified query execution with parameter support.
- CRUD operations including check existence, fetch, insert, update, and delete records.
- Truncate table functionality.
Install the package using npm:
npm install mssql-dboperations
const config = { user: 'yourUsername', password: 'yourPassword', server: 'yourServerAddress', // You can use 'localhost\instance' if your SQL Server runs on the same machine but using a named instance database: 'yourDatabaseName', options: { encrypt: true, // Use this if you're on Windows Azure enableArithAbort: true } };
const Database = require('mssql-dboperations'); const db = new Database(config);
async function checkExistence() { const exists = await db.isExist("TableName", "ColumnName", "ValueToCheck"); console.log('Existence:', exists); } checkExistence();
async function fetchValues() { const records = await db.getDBValues('Employees', 'Name'); console.log('Employee Names:', records); } fetchValues();
async function insertValues() { const resultId = await db.insertDBValues('Employees', { Name: 'John Doe', Position: 'Software Engineer' }); console.log('Inserted Record ID:', resultId); } insertValues();
async function updateValues() { const success = await db.updateDBValues('Employees', { Position: 'Senior Engineer' }, "EmployeeId = 123"); console.log('Update Success:', success); } updateValues();
async function clearTable() { await db.truncateTable('TemporaryData'); console.log('Table truncated successfully.'); } clearTable();