const sql = require('mssql')
const pool1 = new sql.ConnectionPool(config);
const pool1Connect = pool1.connect();
pool1.on('error', err => {
// ... error handler
})
async function messageHandler() {
await pool1Connect; // ensures that the pool has been created
try {
const request = pool1.request(); // or: new sql.Request(pool1)
const result = await request.query('select 1 as number')
console.dir(result)
return result;
} catch (err) {
console.error('SQL error', err);
}
}
@TAVkiev That should work, yes. But note this is one pool not one connection. The pool will have multiple connections (depending on the pool config, of course).
The nature of node's file loading means that the call to require(...)
is cached so each time your test pulls in this function you will only have one pool.
Remember at the end of your tests to close the pool or you'll find your test suite hangs.
Having issues with a simple query that's failing with a RequestError: Invalid object name 'dbo.SomeTable'
Any idea why this would not find any tables?
SQL Management Studio testing shows the user credentials are correct and the account has access to the tables etc.
let sqlConfig = {
user: 'username',
password: 'password',
server: 'servers name or IP',
database: 'TestDB',
port: 1433,
options: {
database: 'TestDB',
"encrypt": true,
"enableArithAbort": true
}
};
let pool = await sql.connect(sqlConfig);
let result = await pool.request().query('SELECT * FROM [dbo].[SomeTable]');
Origin(DB value): 2020-03-31 04:35:43.410
useUTC: true(default) => Date: Mon Mar 30 2020 21:35:43 GMT-0700 (Mountain Standard Time)
if I call toDBString on the resulting value => '2020-03-31 04:35:43'
useUTC: false => Date: Tue Mar 31 2020 04:35:43 GMT-0700 (Mountain Standard Time)
if I call toDBString on the resulting value => '2020-03-31 11:35:43'
const currentTimestamp = new Date(Date.UTC(
now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes(),
now.getSeconds()
));
const now = new Date();
Hey everyone! I would like to get a little help.... I am trying to wrap my head around the connection pools, but I am a bit confused...
const sql = require('mssql');
const sqlConnector = () => {
config = {
user: "xxxxx",
password: "xxxx",
server: "localhost",
database: "Test",
options: {
trustServerCertificate: true
}
const pool = new sql.ConnectionPool(config);
return poolConnect = pool.connect();
}
sqlConnector,
sql
}
const pool = await sqlConnector.sqlConnector();
await pool.request().query(query)
As I understood from the documentation, once a pool has been created, the connect() will resolve to that pool always... so i tried creating a file 'writeToDb3.js' with the following code:
const sql = require('mssql')
function runQuery(query) {
// sql.connect() will return the existing global pool if it exists or create a new one if it doesn't
return sql.connect().then((pool) => {
return pool.query(query)
})
}
But this one throws an error Cannot read property 'port' of undefined. I believe its because i did not provide the config file. This part is not clear also...I already created the pool above with the config file.... I thought i just have to call the connect() function.
Any help would be highly appreciated!
@flashp0wa:matrix.org With your current code everytime you call sqlConnector()
you'll be creating a new pool. You're going to have a problem with this code because you're never closing the pools you create and will cause a connection limit error on your server like this, you'll also no doubt have created memory leaks within your application layer. If you want to have a persistent pool you would need a variable you assign the pool to and return that if it is assigned, eg:
let pool;
const getPool = () => {
if (!pool) pool = new ConnectionPool(config).connect();
return pool;
}
the reason that sql.connect()
is not working is because you have created a pool yourself , then you've attempted to access a global pool (managed by the library code) that you have not configured or instantiated yet
Hey Everybody!
I have here a function called streamRead(). In this function i have defined the logic to read values from the database which has two event 'request.on('row', callback) and request.on('done', callback). Looking like this:
function streamRead {
let rowsToProcess = [];
request.on('row', row => {
rowsToProcess.push(row);
if (rowsToProcess.length >= 15) {
request.pause();
processRows();
}
});
request.on('done', () => {
let iNeedThisValueReturned = processRows();
});
console.log('This runs immediately after calling request.on('done',...), without waiting the function inside (processRows()) to finish. This is sad because i need the value returned from processRows()');
return iNeedThisValueReturned; //this is a promise actually containing the resolve with the value i need
}
So the problem is decribed in the console.log. If i call streamRead from somewhere else in the code. I get no return object. Like this:
async function aFunctionInAnotherFile {
let valueReturnedFromStreamRead = await asd.streamRead();
console.log(valueReturnedFromStreamRead) // This is empty because reques.on('done') is still running and 'return iNeedThisValueReturned' has been returned immidiately after reques.on('done') has been called. resulting in an empty object. Also console.log() is immediately running after the await function. Like the await is not working
}
I dont know if the problem that the 'await' not working in 'aFunctionInAnotherFile()' or I just have to let JS know to wait for on.request('done') to finish.
The main problem just to reiterate... the whole code finished running while the function in the reques.on('done') is still counting it's stuff
Anybody has any idea what I am missing... cant wrap my head around it for 2 days now. I hope i was able to represent in an understandable fashion. If not please dont hesitate to request clarification, i am trying my best :)
Thank a lot in advance!
dhensby on master
Update changelog for v5 Merge pull request #796 from dh… (compare)