I am using the sails js framework. I have a service in which I am using a query like this:
Users.query('select * from remotedb', (err, results) => {
//Do something with results
});
I have to use this query from remote database. How to achieve this?
You will first need to create a connection object. I wrote a snippet which will get this done. Assuming that we use MSSQL server:
let sql = require('mssql');
// Make sure that you add 'mssql' node module in your package.json file
// https://www.npmjs.com/package/mssql
// This is Microsoft SQL Server client for Node.js
let sqlConnectionConfig = {
server: 'Your Database Server URL',
user: 'Your Username',
password: 'Your Password',
database: 'Your Database Name',
};
let connection = new sql.Connection(sqlConnectionConfig);
connection.connect()
.then(() => {
new sql.Request(connection)
.query('SELECT * FROM Role')
.then(function (recordset) {
res.send(recordset); // Or do any other fancy stuff
})
.catch((sqlServerError) => res.send(sqlServerError));
})
.catch((connectionError) => res.send(connectionError));
You can change the connection string and driver based on which database you are going to use. Let me know if this worked for you!
Related
Suppose you have an MS SQL Server which has a Database A {id, name, age} and I need to take this into an REST API. Now I want to map the values into another Database B{student_id, student_name, student_age} in PostgreSQL . How do I do it?
Also assume I have made the API from Database A and now only mapping is required.
The API is as follows:
API goes like this!
I have read about spring boot one to one mappings but I have no idea how to do it.
Hello this is Gulshan Negi
Well, you can extract the data from the MS SQL Server database, transform it to match the schema of the PostgreSQL database, load it into the PostgreSQL database, and then set up a REST API to query the data and return the desired results in order to map values from an MS SQL Server database to a PostgreSQL database and expose it through a REST API. Before loading the data into PostgreSQL, it is essential to ensure that it has been accurately transformed and that any potential issues, such as naming conflicts or mismatches between data types, have been resolved.
Thanks
To map the values from MS SQL Server to PostgreSQL database using NodeJS, you can follow the following steps:
Connect to the MS SQL Server using the appropriate library for NodeJS
(for example, 'mssql' library).
Execute the SELECT statement on the 'Database A' and get the results.
Close the connection to the MS SQL Server.
Connect to the PostgreSQL database using the appropriate library for
NodeJS (for example, 'pg' library).
Use the results obtained from the MS SQL Server to construct INSERT
statements and execute them on 'Database B' in PostgreSQL using the
appropriate library for NodeJS.
Here is some example code to get you started:
const mssql = require('mssql');
const pg = require('pg');
// Connect to the MS SQL Server and execute the SELECT statement
const config = {
user: 'username',
password: 'password',
server: 'mssqlserver',
database: 'A'
};
mssql.connect(config, (err) => {
if (err) {
console.log(err);
return;
}
const request = new mssql.Request();
request.query('SELECT id, name, age FROM table', (err, result) => {
if (err) {
console.log(err);
return;
}
// Close the connection to the MS SQL Server
mssql.close();
// Connect to the PostgreSQL database and execute the INSERT statements
const pgConfig = {
user: 'username',
password: 'password',
host: 'postgresqlhost',
database: 'B',
port: '5432'
};
const pgClient = new pg.Client(pgConfig);
pgClient.connect();
result.recordset.forEach((row) => {
const query = {
text: 'INSERT INTO table(student_id, student_name, student_age) VALUES ($1, $2, $3)',
values: [row.id, row.name, row.age]
};
pgClient.query(query, (err, result) => {
if (err) {
console.log(err);
}
});
});
// Close the connection to the PostgreSQL database
pgClient.end();
});
});
So i have this small Node JS app where i have the following script, which i invoke in my HTML index page, in order to connect to a Cloud SQL database in GCP and perform a specific query so i can pass the values to a dropdown later:
try {
pool = new Pool({
user: "postgres",
host: "/cloudsql/sfmcsms-d-970229:europe-west1:dsi-sfmc-sms-database",
database: "postgres",
password: "dsi-sfmc-sms-database",
port: "5432",
});
console.log("Connection successfull!");
pool.query("select * from ConfigParameter;", (error, results) => {
if (error) {
console.log(error);
}
qResult = results;
console.log(qResult);
//insert logic to populate dropdowns
});
} catch (err) {
console.log("Failed to start pool", err);
}
I'm still working on the logic to populate the dropdowns but for now, i'm focusing on establishing a successful connection first before i get to that. However, everytime i run the script, i seem to get this particular error:
ReferenceError: Pool is not defined
I've been looking around for some possible answers but no luck.
Before using Pool you have import first like this
const { Pool } = require('pg')
And obviously node-postgres should be installed
npm i pg
Dear stackoverflowers:)
I am making my first mvc application on node.js and have such a problem:
Sometimes, when I don't make any queries to the database for a long time (about 3 mins) and try to do one, I have such an error: "This Socket Has Been Ended By The Other Party"
I found out, that this is because of wait_timeout option in mysql config which closes the connection if downtime is more than value
So am i right that i should check connection for being open before every query to database? And if i should, how and where?
This my db connection file:
const mysql = require('mysql2');
// create the connection to database
const connection = mysql.createConnection({
host: 'localhost',
user: 'mysql',
password: 'mysql',
database: 'qa'
});
exports.connect = (done) => {
connection.connect((err) =>{
if(err){
console.log('db connection error');
}
else{
done()
}
})
}
exports.connection = connection;
And this is the part of one of my models:
const db = require('../db');
exports.makeNewQuestion = async (topic, text, subsection, user) => {
return db.connection.promise().execute("INSERT INTO `questions` (`topic`, `text`,
`subsection_id`, `user_id`) VALUES (?, ?, ?, ?)", [topic, text, subsection, user]);
}
As far as I know you don't need a query an existent table to check the connection/
You can use something like:
select 1
If that works you are connected.
Normally you don't need this approach, apart if you need to leave the connection open in the long term.
Depending on the library you are using you can receive a Promise back. Every time you query your db you might want to check if that promise was refused or not and handle the problem accordingly.
My file db/index.js
const { Pool } = require('pg');
const pool = new Pool;
module.exports = {
query: (text, params, callback) => {
return pool.query(text,params,callback);
}
};
In my main file main.js I do:
const db = require('./db/index');
What command can I run on db to figure out if node-postgres is able to connect to my Postgres setup correctly?
To simply test if you can connect from node.js to pgsql database you can use the following snippet:
const { Pool } = require('pg')
const pool = new Pool()
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
// or you can use async/await instead of callbacks
const res = await pool.query('SELECT NOW()')
console.log(res)
await pool.end()
This should return the response in form of pg.Result object containing current datetime.
node-postgres uses the same environment variables as libpq to connect to a PostgreSQL server, so to run the above code you can invoke it like so:
PGUSER=postgres PGHOST=127.0.0.1 PGPASSWORD=mysecretpassword PGDATABASE=postgres PGPORT=5432 node script.js
But you have to provide connection details to you database instance.
The default values for the environment variables used are:
PGHOST='localhost'
PGUSER=process.env.USER
PGDATABASE=process.env.USER
PGPASSWORD=null
PGPORT=5432
You can also provide the connection details programmatically, directly to either the Pool or Client instances. You can also use the connection string URI.
You can read more in the node-postgres documentation in the "connecting" section.
I am using node-postgres in my application.
I would like to know the best practices that I want to follow to ensure the stable connection.
following is the code I'm using right now,
exports.getAll = function (args, callback) {
helper.client = new pg.Client('tcp://postgres:system6:5432#192.168.143.11/abc_dev');
helper.client.connect();
helper.client.query('select count(1) as total_records from facilities', function(err,response){
helper.client.query('select * ,'+response.rows[0].total_records+' as total_records from facilities',
function(err,response){
callback(response);
helper.client.end.bind(helper.client);
});
});
};
As you can see in the code I'm connecting the DB for every request and disconnect once the query has executed. I have one more idea where I can connect the DB globally only once and execute the query using the opened connection. The code look like
helper.client = new pg.Client('tcp://postgres:system6:5432#192.168.143.11/abc_dev');
helper.client.connect();
exports.getAll = function (args, callback) {
helper.client.query('select count(1) as total_records from facilities', function(err,response){
helper.client.query('select * ,'+response.rows[0].total_records+' as total_records from facilities',
function(err,response){
callback(response);
});
});
};
Here the connection never ends. As of my knowledge I am unable to decide which one is best.
Please suggest.
Thanks..
There is an example in the node-postgres wiki. It connects whenever a request is handled:
var server = http.createServer(function(req, res, next) {
pg.connect(function(err, client, done) {
This is what I think is right, too: Your connection should be in request scope.