npm.io
0.0.16 • Published 6 years ago

kmk-mssqlclient

Licence
ISC
Version
0.0.16
Deps
2
Size
81 kB
Vulns
0
Weekly
0

kmk-mssqlclient

Installation

npm install kmk-mssqlclient

Usage

Initialization

const SqlClient = require('kmk-mssqlclient');
const cn = new SqlClient();
cn.setConfig({
	user: 'username',
	password: 'password',
	server: 'server',
	database: 'database',
	options: {
		encrypt: false
	}
});

Connecting

cn.connect()
.catch(e => {
	console.log(e);
})
.then(() => {
	console.log('connected');
})

/* OR */

SqlClient.open({
	user: 'username',
	password: 'password',
	server: 'server',
	database: 'database',
	options: {
		encrypt: false
	}
}, (err, client) => {
	if(err) console.log(err);
	else console.log(client);
})

Connecting using async/await mode

(async () => {
	try{
		await cn.connect();
		console.log('connected');
	}
	catch(e){
		console.log(e);
	}
})()

Querying data

cn.query('select * from table')
.catch(e => {
	console.log(e);
})
.then(result => {
	console.log(result);
})

Querying data using async/await mode

(async () => {
	try{
		let result = await cn.query('select * from table');
		console.log(result);
	}
	catch(e){
		console.log(e);
	}
})()
Methods
Method Name Returns Parameters Description
connect Promise<void<T>> (void) Connects to a MS SQL Server
query<T> Promise<IQueryResult<T>> (sqlCommandStatement: string, ...optionalParams: any[]) Executes a sql query and returns recordsets and rows affected
executeRowSet<T> Promise<Array<T>> (sqlCommandStatement: string, ...optionalParams: any[]) Executes a query and returns the first rowSet for that query
executeLastRowSet<T> Promise<Array<T>> (sqlCommandStatement: string, ...optionalParams: any[]) Executes a query and returns the last rowSet for that query
executeRowSets Promise<Array>> (sqlCommandStatement: string, ...optionalParams: any[]) Executes a query and returns all rowSets for that query
fetchRow<T> Promise<T> (sqlCommandStatement: string) Executes a query and returns the first row from the first rowSet for that query
fetchColumn (sqlCommandStatement: string, index: number | string = 0) Executes a query and returns a specific field (index or name) from the first row from the first rowSet for that query
executeNonQuery Promise<number> (sqlCommandStatement: string, ...optionalParams: any[]) Executes a sql statement and returns affected row count
getLastsqlCommandStatement string (void) Returns the last sqlCommandStatement executed

Keywords