How to run SELECT ##SERVERNAME SQL statement in NodeJS?
Please beware that the said SQL statement will return an empty header with a single record (hostname).
How to process and take that record and display it on express site ( or put it into some variable and call it in .ejs page
I manage to use os.hostname, but that will only give the NodeJS machine name, not the machine name where SSQL Server is running.
This is for demonstration of container/Kubernetes, so, If the web page returns the info of where the NodeJS and Server SQL is running, it will helpful.
Thank You again.
Update: What I've done so far:
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 ##SERVERNAME',(err,rec)=>{
console.log('SQL RUNNING AT ' +JSON.stringify(rec.recordset, null, 0)
.replace('[{"":"', '')
.replace('"}]','')
);
});
}
});
The above code returns the ##SERVERNAME value, but only prints out at console.log. How do I put this into a variable? and send to ejs?
When executing your query in SQL, such as from SSMS, you would give your result a column name, e.g.:
select [MyServerName] = ##SERVERNAME
MyServerName
--------------------
FOO\MSSQLSERVER
Or:
select ##SERVERNAME as MyServerName
MyServerName
--------------------
FOO\MSSQLSERVER
Do the same when invoking from NodeJS:
request.query('select ##SERVERNAME as MyServerName',(err, result)=>{
const myServerName = result.recordset[0].MyServerName;
console.log('SQL RUNNING AT ' + myServerName)
);
});
Related
I am currently using sql.js to view my database:
async function sqliteRun () { // eslint-disable-line
const SQL = await initSqlJs({
locateFile: () => 'misc/sql-wasm.wasm'
})
const db = new SQL.Database('public/misc/test.sqlite')
const stmt = db.prepare('SELECT * FROM test')
while (stmt.step()) { //
const row = stmt.getAsObject()
console.log('Here is a row: ' + JSON.stringify(row))
}
}
But then I am getting an error: "File is not a database". I double checked my file and it seems correct (I was able to view it in a sqlite file browser)
I also tried using .db and .sql, all give out the same error.
I prefer to load the file directly in the new SQL.Database() constructor. I wont be able to use fs. Any thoughts on how to do this?
I am wirting a mongodb auto backup code but i am stuck in some error: 'mongodump' is not recognized as an internal or external command.
can anyone help me out?
or is there another way to get auto backup with mongodb
exports.dbAutoBackUp = () => {
let cmd =
'mongodump --host ' +
dbOptions.host +
' --port ' +
dbOptions.port +
' --db ' +
dbOptions.database +
' --username ' +
dbOptions.user +
' --password ' +
dbOptions.pass +
' --out ' +
newBackupPath;
exec(cmd, (error, stdout, stderr) => {
console.log("Error : "+error)
console.log("Error 1: "+stdout)
console.log("Error 2: "+stderr)
if (this.empty(error)) {
// check for remove old backup after keeping # of days given in configuration.
if (dbOptions.removeOldBackup == true) {
if (fs.existsSync(oldBackupPath)) {
exec('rm -rf ' + oldBackupPath, err => {
console.log(err);
});
}
}
}
});
}
};
The error is probably because you are not in the directory where you have mongodb executable.
There are two ways to do it.
change your directory to mongodb's installation path
Add the mongodb executable to your environment variables
Path should be something like
{installation_directory}:\Program Files\MongoDB\Server\{version}\bin
For example
C:\Program Files\MongoDB\Server\4.2\bin
I am trying to implement notify triggers in postgres , also using node-postgres to listen to db changes .
var pgConnection = 'postgres://' + DB_USER + ':' + DB_PASSWORD + '#' +
DB_HOST + ':' + DB_PORT + '/' + DB_NAME;
const pgClient = new pg.Client(pgConnection);
pgClient.connect();
pgClient.query('LISTEN leadupdated');
pgClient.query('LISTEN leadadded');
pgClient.on('notification', function (data) {
console.log(data.payload);
socket.emit(data.channel);
});
the script listens to changes for about 5-10 mins , but if inactive for sometime, it does not receive any db changes after that. How can i fix this timeout issue?
How can I make Mongoose debug output look pretty in console? Currently the output is displayed on one line.
Here is something I put together, maybe somebody will find it useful.
Install colors module for node.js (this is optional):
npm install colors
Then in your file:
var colors = require('colors');
mongoose.set('debug', function (collectionName, method, query, doc) {
console.log(
'Mongoose: '.cyan +
collectionName +
'.' +
method +
' (' +
JSON.stringify(query, null, 2) + ')');
});
i am using https://github.com/gf3/sandbox#readme but i am not getting that how can i pass some data from my code to javascript code through this sandbox. for example
var s = new sandBox();
s.run("(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function(output) {
console.log("Example 2: " + output.result + "\n")
})
now i want to pass some data to this function how can i do that?
It does not have any clean way to pass arguments. However, since you are passing code as a string anyway, you can simply add the arguments directly to the string:
var arg = 'Hello';
var code = "(function(name) { return 'Hi there, ' + name + '!'; })("+ JSON.stringify(arg) +")"
s.run(code, ...);
I'm using JSON.stringify to ensure that the string is a valid JS expression.