kmh-db-connector
v2.0.1
Published
Simple abstraction layer for `mssql`.
Downloads
7
Readme
KM/H Database Connector (kmh-db-connector)
Simple abstraction layer for mssql
.
Installation
npm install kmh-db-connector
Usage
const Connector = require('kmh-db-connector');
const conf = {
user: 'username',
password: 'password',
server: 'host',
database: 'database name',
port: 1433,
connectionTimeout: 30000,
requestTimeout: 30000,
pool: {
max: 30,
idleTimeoutMillis: 20000,
},
logger: (...args) => console.log("Log: ", ...args)
};
Connector(conf, 'Deutsch').then(connector => {
const getUser = (firstName, lastName) => connector.executeProcedureSafeNormalized('Person.getUser', { FirstName: firstName, LastName: lastName, ModifiedDate: (new Date()) }, 'AdventureWorks2017');
const run = async() => {
return getUser('Pilar', 'Ackerman')
};
run()
.then((res) => { console.log("OK", res) }).catch(e => console.error(e)).then(() => process.exit(0));
});
API
Connector
function Connector(configuration, language, mapTypesFunction) -> Promise<ConnectorApi>
configurtation : object
- mssql - configuration objectlanguage : string
- optional sets language for each query when givenmapTypesFunction : function any -> mssqlType
returns Object
- Connector-API
Connector-API
async function executeProcedure(procedureName, parameters, dbName)
procedureName : string
- name of stored procedureparameters : Array<string>
- list of parameter values (example:["@parameter='some value'", "@anotherParam=123"]
)dbName : string
- name of Database to apply procedure
returns Promise<Response>
- mssql response
async function executeProcedureSafe(procedureName, parameters, dbName)
Executes a procedure with prepared statement.
procedureName : string
- name of stored procedureparameters : Object
- named parameters object (make sure object keys match procedure parameter keys including case)dbName : string
- name of Database to apply procedure
returns Promise<Response>
- mssql response
async function executeProcedureNormalized / executeProcedureSafeNormalized (procedureName, parameters, dbName)
returns Promise<Array<Object>>
- normalized sql response
Type Mapping
Example:
const sql = require('mssql');
const getParameterType = val => {
const jsType = typeof val;
if (jsType === 'string') return sql.Text;
if (jsType === 'number') return (Number.isInteger(val)) ? sql.Int : sql.Numeric;
if (jsType === 'boolean') return sql.Bit;
if (val instanceof Date) return sql.DateTime;
return sql.Text;
}