Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
autostart: true
},
beforeConnect: conn => {
conn.on('debug', message => console.info(message));
conn.on('error', err => console.error(err));
conn.removeAllListeners();
}
}
var connectionStringTest: sql.ConnectionPool = new sql.ConnectionPool("connectionstring", (err) => {
if (err) {
return err;
}
});
var connection: sql.ConnectionPool = new sql.ConnectionPool(config, function (err: any) {
if (err != null) {
console.warn("Issue with connecting to SQL Server!");
}
else {
connection.query`SELECT ${1} as value`.then(res => { });
var requestQuery = new sql.Request(connection);
var getArticlesQuery = "SELECT * FROM TABLE";
requestQuery.query(getArticlesQuery, function (err, result) {
if (err) {
console.error(`Error happened calling Query: ${err.name} ${err.message}`);
}
// checking to see if the articles returned as at least one.
else if (result.recordset.length > 0) {
}
function test_promise_returns() {
// Methods return a promises if the callback is omitted.
var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
connection.connect().then(() => { });
connection.close().then(() => { });
connection.query('SELECT 1').then((recordset) => { });
connection.query('SELECT 1 as value').then(res => { });
connection.query`SELECT ${1}`.then((recordset) => { });
connection.batch('create procedure #temporary as select * from table').then((recordset) => { });
connection.batch('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });
connection.batch`create procedure #temporary as select ${1} from table`.then((recordset) => { });
var preparedStatment = new sql.PreparedStatement(connection);
preparedStatment.prepare("SELECT @myValue").then(() => { });
preparedStatment.execute({ myValue: 1 }).then((recordSet) => { });
preparedStatment.unprepare().then(() => { });
var transaction = new sql.Transaction(connection);
// License: MIT
// ------------------------------------------------------------------------//
'use strict';
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
//database
const mssql = require('mssql');
const dbconfig = require('./src/config/dbconfig.json');
const pool = new mssql.ConnectionPool(dbconfig);
mssql.globalConnectionPool = pool;
//open database connection
pool.connect((err)=>{
if (err){
console.log('connection failed to server:' + dbconfig.server + ' database:' + dbconfig.database);
console.log(err);
}
else {
console.log ('Connected to ' + dbconfig.server
+ '/' + dbconfig.database
+ ' (' + dbconfig.user + ')');
const request = new mssql.Request(pool);
request.query('select @@version',(err,rec)=>{
console.log(JSON.stringify(rec.recordset, null, 0)
.replace('[{"":"', '')
.replace('"}]','')
port: config.Sql.Port,
requestTimeout: 10 * 60 * 1000,
connectionTimeout: config.Sql.Timeout,
user: config.Sql.PupilCensus.Username,
password: config.Sql.PupilCensus.Password,
pool: {
min: 1,
max: 3
},
options: {
appName: config.Sql.Application.Name, // docker default
encrypt: config.Sql.Encrypt
}
}
pool = new mssql.ConnectionPool(poolConfig)
pool.on('error', err => {
context.log('SQL Pool Error:', err)
})
await pool.connect()
return pool
}
function test_classes_extend_eventemitter() {
var connection: sql.ConnectionPool = new sql.ConnectionPool(config);
var transaction = new sql.Transaction();
var request = new sql.Request();
var preparedStatment = new sql.PreparedStatement();
connection.on('connect', () => { });
transaction.on('begin', () => { });
transaction.on('commit', () => { });
transaction.on('rollback', () => { });
request.on('done', () => { });
request.on('error', () => { });
preparedStatment.on('error', () => { })
}
private batch(config: Config, conn: Connection) {
const files = this.getFilesOrdered(config);
let promise = new sql.ConnectionPool(conn).connect();
this.spinner.start(`Pushing to ${chalk.blue(conn.server)} ...`);
files.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
const statements = content.split('GO' + EOL);
statements.forEach(statement => {
promise = promise.then(pool => {
return pool
.request()
.batch(statement)
.then(() => pool);
});
});
});
function getConnectionPool(connection: msconfig): ConnectionPool {
try {
return new ConnectionPool(connection);
}
catch(e) {
console.error(e);
}
}
constructor (public connectionString: string) {
this.db = new ConnectionPool(this.connectionString)
let url = urlParse(connectionString, true)
if (url && url.pathname) {
this.defaultSchema = url.pathname.substr(1)
} else {
this.defaultSchema = 'master'
}
}
sqlService.initPool = async () => {
if (pool) {
logger.warn('The connection pool has already been initialised')
return
}
pool = new mssql.ConnectionPool(poolConfig)
pool.on('error', err => {
logger.error('SQL Pool Error:', err)
})
return pool.connect()
}
export async function getDb(databaseConfig: IDbConnection, dbType: DbTypes) {
const dbKey = JSON.stringify(databaseConfig);
if (cachedDbConnections.has(dbKey)) {
const dbConnection = cachedDbConnections.get(dbKey);
if (dbConnection) {
return dbConnection.db;
}
}
if (dbType === 'mssql') {
const pool = new sql.ConnectionPool({ ...databaseConfig } as sql.config);
const db = await pool.connect();
cachedDbConnections.set(dbKey, {
db,
close: pool.close.bind(pool),
});
return db;
} else {
const pgp = require('pg-promise')({ schema: databaseConfig.schema || 'public', });
const db = pgp(databaseConfig);
cachedDbConnections.set(dbKey, {
db,
close: pgp.end,
});
return db;
}
};