SELECT TOP (10) u.* FROM [Alg].[User] AS u;
, function (err, recordset) {connectionPool = new mssql.ConnectionPool({user, password, server, database});
connectionPool.connect()
const fs = require('fs');
const os = require('os');
const mssql = require('mssql');
class Database {
/**
* Ottieni il riferimento alla libreria di riferimento
*
* @static
* @returns
*
* @memberof Database
*/
static getLib() {
return mssql;
}
/**
* Apri connessione al database usando le configurazioni impostate.
*
* @private
* @static
* @param {string} [username]
* @param {string} [password]
* @param {string} [server]
* @param {string} [database]
* @returns
*
* @memberof Database
*/
static openConnection(username, password, server, database) {
if (Object.prototype.hasOwnProperty.call(this, 'connectionPromise') === true && this.connectionPromise !== null) {
return this.connectionPromise;
}
const configuration = this.getConfiguration(username, password, server, database);
if (!this.enable) {
return Promise.reject('Database disabled.');
}
this.connectionPool = new mssql.ConnectionPool(configuration);
this.connectionPromise = this.connectionPool.connect();
return this.connectionPromise;
}
static closeConnection() {
this.connectionPool.close();
}
/**
* Promessa alla connessione del database, che verrà avviata non appena viene richiesta la prima volta.
*
* @public
* @static
* @returns {Promise} <ConnectionPool>
*
* @memberof Database
*/
static willConnect() {
if (Object.prototype.hasOwnProperty.call(this, 'connectionPromise') === false || this.connectionPromise === null) {
try {
return this.openConnection();
} catch (err) {
console.error('willConnect: Database not connected.');
throw new Error(err);
}
}
return this.connectionPromise;
}
static getConnectionPool() {
return this.connectionPool;
}
/**
* Ottieni l'oggetto di configurazione per la connessione al database.
*
* @private
* @static
* @param {string} [username]
* @param {string} [password]
* @param {string} [server]
* @param {string} [database]
* @memberof Database
*/
static getConfiguration(username, password, server, database) {
let storedUsername = null;
let storedPassword = null;
let storedServer = null;
let storedDatabase = null;
let storedTimeout = null;
try {
fs.accessSync(`${__dirname}/../../config.js`);
const config = require(`${__dirname}/../../config`);
this.enable = config[this.configKey] ? config[this.configKey].enable : this.enable;
storedUsername = config[this.configKey] ? config[this.configKey].username : null;
storedPassword = config[this.configKey] ? config[this.configKey].password : null;
storedServer = config[this.configKey] ? config[this.configKey].server : null;
storedDatabase = config[this.configKey] ? config[this.configKey].database : null;
storedTimeout = config[this.configKey] ? config[this.configKey].timeout : null;
} catch (err) {
console.warn(err);
}
return {
user: storedUsername || username || '',
password: storedPassword || password || '',
server: storedServer || server || 'localhost',
database: storedDatabase || database || '',
connectionTimeout: storedTimeout || '15000',
requestTimeout: storedTimeout || '15000',
options: {
appName: os.hostname(),
},
};
}
dhensby on master
Update changelog for v5 Merge pull request #796 from dh… (compare)