How to get RAISE INFO/NOTICE in Knex from node js? - node.js

I am using Knex query builder and Postgres as my data base.
I need to get the output from a raise notice or raise info from a Postgres function or procedure.
raise notice 'This is my Log';

we need to set notice after pool created, for that we need to use afterCreate function.
for your references
https://github.com/knex/knex/issues/4241
The sample code is below.
var pConn = {
host: 'SERVER',
port: 'PORT',
user: 'USER',
password: 'PASSWORD',
database: 'DATABAENAME'
};
var query = "select * from pglog_test()";
try {
const knex = require('knex')({
client: 'pg',
connection: pConn,
pool: {
afterCreate: function (conn, done) {
conn.query('select 1 as result', function (err, result) {
conn.on('notice', function (msg) {
// here we can get the pg procedure logs (raise notice/raise info)
console.log('logs from postgress' + msg);
});
done(err, conn);
});
}
}
});
// procedure calling
knex.raw(query).then(function (result) {
console.log(result);
}).catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}

Related

Can't return any value while connecting Redshift from Node JS

The following code connected to Redshift database but returning an empty response even the response will be displayed in the console.log, why?
index.js
const config = {
user: 'user',
database: 'database',
password: 'password',
port: port,
host: 'hostname',
};
var response = [];
console.log('Before Connection');
exports.handler = function index(event, context, callback) {
var redshiftClient = new Redshift(config, {rawConnection:true});
redshiftClient.connect(function(err){
console.log('After Connection');
if(err) throw err;
else{
redshiftClient.query('SELECT * FROM customer', {raw: true}, function(err, data){
if(err) throw err;
else{
response = data;
console.log(data);
redshiftClient.close();
return response;
}
});
}
});
return response;
};
I have used the promise concept and it does returning the response properly for me.
Sample JSON {"user_id": "1001"}
var Redshift = require('node-redshift');
const config = {
user: 'user',
database: 'database',
password: 'password',
port: port,
host: 'hostname',
};
// The values passed in to the options object will be the difference between a connection pool and raw connection
var redshiftClient = new Redshift(config, {rawConnection:true});
exports.handler = async (event) => {
return new Promise(function(resolve, reject){
redshiftClient.connect(function(err){
if(err) throw err;
else{
redshiftClient.parameterizedQuery('SELECT * FROM customer where user_id = $1', [event.user_id], {raw: true}, function(err, data){
if(err) throw reject(err);
else{
resolve(data);
redshiftClient.close();
}
});
}
});
});
};

Callback limit Node.JS? Or am I missing something?

Good day, I recently had help learning how to make things wait using callbacks, however now it's almost as if I hit a limit on how many can be used, but I can't seem to find information on this?
If I ran this code without tryToCreateAccount having a callback, it will connect, check and work as intended. As soon as I make tryToCreateAccount have a callback param, it appears to enter Connect.Connect (only sometimes) but then never query's.
Backend:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'host',
user: 'user',
password: 'pass',
database: 'db'
});
export function tryToCreateAccount(login, password, passwordConfirm, callback)
{
checkAccountNameAvailable(login, connection, function(err, accountNameAvailable)
{
callback(err, accountNameAvailable);
});
}
function checkAccountNameAvailable(login, connection, callback)
{
var accountNameAvailable = true;
console.log("Checking account name", login);
connection.connect(function (err)
{
console.log("Checking Connect Method");
if(err) callback(err);
connection.query("SELECT login FROM accounts WHERE login = ?", login, function (err, result)
{
if (err) callback(err);
if(result.length > 0) accountNameAvailable = false;
console.log("returning ", accountNameAvailable);
callback(null, accountNameAvailable);
connection.end();
});
});
}
Front End:
tryToCreateAccount($w('#input1').value, $w('#input2').value, $w('#input3').value, function (err, callback)
{
if(callback)
{
console.log("Was True");
}
else
{
console.log("Was False");
}
});
UPDATED CODE:
Backend:
const mysql = require('mysql');
const pool = mysql.createPool(
{
connectionLimit : 10,
host : 'host',
user : 'user',
password : 'pass',
database : 'db'
});
export function tryToCreateAccount(login, password, passwordConfirm, callback)
{
checkAccountNameAvailable(login, pool, function(err, accountNameAvailable)
{
if(err)
{
console.log(err);
callback(err, null);
}
callback(err, accountNameAvailable);
});
}
function checkAccountNameAvailable(login, pool, callback)
{
var accountNameAvailable = true;
console.log("Checking account name", login);
pool.getConnection(function (err, connection)
{
console.log("Made a connection.");
if(err)
{
console.log(err);
callback(err, null);
connection.destroy();
}
connection.query("SELECT login FROM accounts WHERE login = ?", login, function (err, result)
{
console.log("Checking Query Method");
if (err)
{
console.log(err);
callback(err, null);
}
if(result.length > 0) accountNameAvailable = false;
console.log("returning ", accountNameAvailable);
callback(null, accountNameAvailable);
connection.destroy();
});
});
}
Frontend is the same
Output:
Checking account name testaccount

REG: No result while connects NODEJS to ORACLEDB

nodeJS connects to oracle
var oracle = require('./');
var connectData = {
hostname: "myhost",
port: 1521,
database: "mydb", // System ID (SID)
user: "usertest",
password: "mypass"
}
oracle.connect(connectData, function(err, connection) {
if (err) { console.log("Error connecting to db:", err); return; }
connection.execute("SELECT * FROM <table_name>", [], function(err, results) {
if (err) { console.log("Error executing query:", err); return; }
console.log(results); //This statement's not working
connection.close(); // call only when query is finished executing
});
});
output-
C:\xampp\htdocs\motcua.dev\public\socket.io\node_modules\oracle>node tests-connection
C:\xampp\htdocs\motcua.dev\public\socket.io\node_modules\oracle>
Question- It is exiting without giving any output please help me with this

Invalid object name 'dbo.tbl_user' after running sql(azure) query from nodejs app

I am getting error Invalid object name dbo.tbl_user.while running a query on the Azure database from a nodejs app.My code which runs is following.
const pool = new sql.ConnectionPool(DatabaseConfig.config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[tbl_user]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
My connection string is :
config: {
server: 'azureserver',
options: {
database: 'cccc',
encrypt: true,
port: 1433
},
user: 'ccc',
password: 'ccccc'
}
what is the mistake I am doing here? Thanks in advance for the help.
I tested your code on my side but did not reproduce your issue.
Here is my test code:
const sql = require('mssql')
const config = {
server: '***.database.windows.net',
database: '***',
user: '***',
password: '***',
options: {
database: '***',
encrypt: true,
port: 1433
}
}
const pool = new sql.ConnectionPool(config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[Student]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
Query result on the portal:
Run query.js :
You need to make sure the table name is correct, it's not related to spelling case.
Hope it helps you.

Queries inside of a connection query callback

I am trying to fetch a table, whose name is defined in another table and return the results.
I have defined a database module with these methods:
var mysql = require("mysql");
var config = require("../config/database.js");
module.exports = {
pool: mysql.createPool({
host: config.host,
user: config.user,
database: config.database,
password: config.password,
}),
getConnection: function(callback) {
this.pool.getConnection(callback);
},
fetch: function(table, callback) {
this.getConnection(function(err, connection) {
if(err) {
callback(err);
} else {
connection.query("SELECT * FROM ??", table, callback);
connection.release();
}
});
},
};
I am trying to fetch the table and then the "sub" table like so:
db.fetch("foo", function(err, rows) {
if(err) {
throw err;
} else {
_.each(rows, function(row) {
db.fetch(row.tbl, function(err, result) { console.log(err, result); });
});
}
});
However the inner-fetch callback is never called.
Why is this? I suspected I needed to close the first connection before establishing a second so I tried taking the each loop outside but encountered things becoming out of sync.

Resources