postgres connection from node.js - node.js

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.

Related

How do I get a MongoDB import script to close the db after inserting results?

I am running a quick little nodejs script to find documents in one collection and insert them into another collection but on the same DB. I came up with this guy, but it has no way to close because I think its running open or async?
I have tried placing the db.close() in various places and tried mongoClient.close(). No luck which had me thinking about trying to force a timeout for the async call. Added a connection Time out but it did not have the desired behaviour.
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const async = require("async");
// Connection URL
var url = 'mongodb://localhost:27017/sourceDB';
// Use connect method to connect to the Server
MongoClient.connect(url,{connectTimeoutMS: "5"}, (err, db) => {
db.collection('source.collection', function(err, col) {
assert.equal(null, err);
col.find().forEach(function (data) {
console.log(data);
db.collection('destination.collection').insertOne(data, function(err, res) {
assert.equal(null, err);
});
console.log("Moved");
});
});
});
The script does well and picks up the collection and inserts, but the connection remains open.
It is not recommended to explicitly close the connection as shown by this SO thread.
Rather, allow the client library to manage the connection for you.

Electron/NodeJS Connecting to SQL Server using mssql not working

I'm new to Electron and trying to make 1 st application in which I need to connect it to a SQL server database for data storing/retrieving. I've have installed this plugin (https://www.npmjs.com/package/mssql#connect-callback) and followed their instructions but got no success regarding the connection. The weird part is that I also get no error or whatever showing in the console so I'm totally lost. Any help would be much appreciated, thank you guys.
Ps: I'm sure that there's no problem with the database since I can still connect to it using the same config setting below with a database client manager tool.
Below is the code I've used for simple testing connection.
<script type="text/javascript">
$(document).ready(function () {
const electron = require('electron');
const sql = require('mssql');
const config = {
user: 'ql*****',
password: 'qlh****',
server: '123.20.****',
database: 'QLHS'
};
async () => {
try {
await sql.connect(config);
const result = await sql.query`select * from DM_DONVI`;
console.dir(result);
} catch (err) {
console.log(err);
}
};
});
</script>
The link you provided is working. I tried the same. The error log can be seen in view->Toogle Developer Tools. The issue is you need install mysql.
npm install mysql --save
Then the code works fine.
Thank you Mr :D Actually, the thing that didn't work in my original post is the async part. Changing that to this and everything is fine now:
sql.connect(config, function (err) {
if (err) console.log(err);
var request = new sql.Request();
request.query('select * from DM_DONVI', function (err, recordset) {
if (err) {
console.log("Something went wrong")
}
else {
var result = JSON.stringify(recordset);
console.log(recordset.recordsets[0]);
}
});
});

node-soap service that uses database (dependency issues)

First of all, this is one of my first projects in Node.js so I'm very new to it.
I have a project I want to make that is a SOAP (I know, SOAP... backwards compatibility, huh?) interface that connects to an Oracle database.
So I have a WSDL describing what these functions look like (validation for addresses and stuff) and I have a connection to the database.
Now when using the SOAP npm module, you need to create a server and listen using a service that allows you to respond to requests. I have a separate file that contains my SOAP service but this service should do queries on the database to get its results.
How would I go about sort of 'injecting' my database service into my SOAP service so that whenever a SOAP call is done, it orchestrates this to the correct method in my database service?
This is what my code looks like:
databaseconnection.js
var oracledb = require('oracledb');
var dbConfig = require('../../config/development');
var setup = exports.setup = (callback) => {
oracledb.createPool (
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
},
function(err, pool)
{
if (err) { console.error(err.message); return; }
pool.getConnection (
function(err, connection)
{
if (err) {
console.error(err.message);
return callback(null);
}
return callback(connection);
}
);
}
);
};
databaseservice.js
var DatabaseService = function (connection) {
this.database = connection;
};
function doSomething(callback) {
if (!this.database) { console.log('Database not available.'); return; }
this.database.execute('SELECT * FROM HELP', function(err, result) {
callback(result);
});
};
module.exports = {
DatabaseService: DatabaseService,
doSomething: doSomething
};
soapservice.js
var myService = {
CVService: {
CVServicePort: {
countryvalidation: function (args, cb, soapHeader) {
console.log('Validating Country');
cb({
name: args
});
}
}
}
};
server.js
app.use(bodyParser.raw({type: function(){return true;}, limit: '5mb'}));
app.listen(8001, function(){
databaseconnection.setup((callback) => {
var temp = databaseservice.DatabaseService(callback);
soapservice.Init(temp);
var server = soap.listen(app, '/soapapi/*', soapservice.myService, xml);
databaseservice.doSomething((result) => {
console.log(result.rows.length, ' results.');
});
});
console.log('Server started');
});
How would I go about adding the databaseservice.doSomething() to the countryvalidation soap method instead of 'name: args'?
Also: I feel like the structure of my code is very, very messy. I tried finding some good examples on how to structure the code online but as for services and database connections + combining them, I didn't find much. Any comments on this structure are very welcome. I'm here to learn, after all.
Thank you
Dieter
The first thing I see that looks a little off is the databaseconnection.js. It should be creating the pool, but that's it. Generally speaking, a connection should be obtained from the pool when a request comes in and release when you're done using it to service that request.
Have a look at this post: https://jsao.io/2015/02/real-time-data-with-node-js-socket-io-and-oracle-database/ There are some sample apps you could have a look at that might help. Between the two demos, the "employees-cqn-demo" app is better organized.
Keep in mind that the post is a little dated now, we've made enhancements to the driver that make it easier to use now. It's on my list to do a post on how to build a RESTful API with Node.js and Oracle Database but I haven't had a chance to do it yet.

Express keeping connection open?

I'm using node js, express and postgresql as backend.
This is the approach I used to make a rest API:
exports.schema = function (inputs, res) {
var query = knex('schema')
.orderBy('sch_title', 'asc')
.select();
query.exec(function (err, schemas) {
if(err){
var response = {
message: 'Something went wrong when trying to fetch schemas',
thrownErr: err
};
console.error(response);
res.send(500, response);
}
if(schemas.length === 0){
var message = 'No schemas was found';
console.error(message);
res.send(400, message);
return;
}
res.send(200, schemas);
});
};
It works but after a while postgres logs an error and it's no longer working:
sorry, too man clients already
Do I need a close each request somehow? Could not find any about this in the express docs. What can be wrong?
This error only occurs on production server. Not on developing machine.
Update
The app only brakes in one 'module'. The rest of the app works fine. So it's only some queries that gives the error.
Just keep one connection open for your whole app. The docs shows an example how to do this.
This code goes in your app.js...
var Knex = require('knex');
Knex.knex = Knex.initialize({
client: 'pg',
connection: {
// your connection config
}
});
And when you want to query in your controllers/middlewares...
var knex = require('knex').knex;
exports.schema = function (req, res) {
var query = knex('schema')
.orderBy('sch_title', 'asc')
.select();
// more code...
};
If you place Knex.initialize inside an app.use or app.VERB, it gets called repeatedly for each request thus you'll end up connecting to PG multiple times.
For most cases, you don't need to do an open+query+close for every HTTP request.

Number of busy connections used in pool

I am using connection pool in pg node js module.
As I understand when you are using connection pool, your connections created for you, and stored, when you need it, some method evoked, and you get already instantiated connection.
Now about pg:
Simple code
pg.connect(conString, function (err, client, done) {
if (err) {
callback(err);
} else {
client.query('SELECT * FROM users', function (err, result) {
done();
if (err) callback(err);
callback(null, result.rows);
});
}
});
When I call pg.connect method, I assume that, I get already instantiated connection from pool, when I call done I return my connection to the pool.
Now I want to debug my application, I can forgot to call done or do it not efficient way.
How can I print number of busy connections? Or another way to debug approach. Thank you in advance.
I had some problem, when request to my route hanging for 30 seconds, I suppose that it is, because I didn't return my connection to the pool.
From pg module documentation pages
var pg = require('pg');
pg.connect(function(err, client, done) {
var pool = pg.pools.getOrCreate();
console.log(pool.getPoolSize()); //1
console.log(pool.availableObjectsCount()); //0
done();
console.log(pool.getPoolSize()); //1
console.log(pool.availableObjectsCount()); //1
});
Hope this helps...

Resources