pg-promise hangs after six queries - pg-promise

I'm working on a project using pg-promise. I've tried querying a few different ways with pg-promise but they all seem to cause it to hang after 6 queries.
It seems to me like the connections aren't being closed but I can't find anything in the documentation about closing a connection after a query.
Here's what I have
var cn = {
host: 'localhost',
port: 5432,
database: 'db',
user: 'user',
password: 'password'
};
var db = pgp(cn);
function query(sql, params) {
return db.task(function (t) {
// this = t = task protocol context;
// this.ctx = task config + state context;
return t.query(sql, params);
})
.then(function (events) {
// success;
console.log(events);
})
.catch(function (error) {
// error;
});
}
I also tried using a shared connection, object but the documentation recommended using tasks. Does anyone know what's going on here?

I'm not sure if this will be helpful to anyone in the future. But my problem was not return requests to the browser.
I hit the maximum number of connections and no responses to the browser made it appear to be hanging to me. I didn't realize requests is node/express won't automatically return like they do with php/apache.

Related

flutter read data from mysql

I just built flutter environment on Mac yesterday and started to learn.
trying to get data from mysql following the tutorial
https://www.youtube.com/watch?v=ig6WRq73iEg&t=165s&ab_channel=JesusHedo
here is my code
mysql.dart file
import 'package:mysql1/mysql1.dart';
class Mysql {
static String host = '127.0.0.1',
user = 'root',
password = 'justin',
db = 'users_test_MAC';
static int port = 3306;
Mysql();
Future<MySqlConnection> getConnection() async {
var settings = new ConnectionSettings(
host: host, port: port, user: user, password: password, db: db);
return await MySqlConnection.connect(settings);
}
}
related function in main.dart
void _getname() {
db.getConnection().then((conn) {
// String sql = 'insert into `userstable` values(50,"justin3","jsutin#gmail",26);';
// this describe can work
String sql = 'select `name` from `userstable` where id = 20;';
conn.query(sql).then((results) {
for (var row in results) {
setState(() {
name = row[0];
});
}
});
});
}
however it didn't work.
in debug mode, it showed var is empty while the describe worked when I queried in mysql workbench
BTW, I've tried to insert data and it can work
If anyone knows there got something wrong
You shouldn't work like that.. you need a backend service and work with http client

socket.io for react native (sending query problems)

I'm using this library and i can connect without problems.
Usually when I have worked with sockets the code that ever i used is:
socket = io.connect(url, { query: ‘token=’ + token});
and i can see this info reading socket.request._query
Using socket.io for react native i'm trying to send params:
this.socket = new SocketIO('http://localhost:3000', { query: ‘token=’ + token});
but in socket.request._query only can see this log:
{ transport: 'polling', b64: '1' }
In the library some options are mentioned like: connectParams. But i don't know how i can see the info
Related: link
It's not pretty detailed in the repo, but connectParams is a key/value object, and furthermore the values you sent in it will be appended in the url, as shown here:
if connectParams != nil {
for (key, value) in connectParams! {
let keyEsc = key.urlEncode()!
let valueEsc = "\(value)".urlEncode()!
queryString += "&\(keyEsc)=\(valueEsc)"
}
}
>Source<
So, you should try using connectParams like this(though I'm not sure how you tried it before):
this.socket = new SocketIO('http://localhost:3000', {
connectParams: {
myAwesomeQueryStringParam: "someRandomValue"
}
});
PS: forgive me, my english is pretty bad

How to connect socket.io and rethinkdb?

After hours of trying. I haven't make it work.
Here's what I have.
var app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http),
r = require('rethinkdb');
http.listen(5000);
console.log('Server started on port 5000');
r.connect({db: 'testRealtime'}).then(function(c) {
r.table('messages').insert(
{ message: "realtime" }
)
r.table('messages').changes().run(c)
.then(function(cursor) {
cursor.each(function(err, item) {
io.emit('messages', item)
})
})
})
As you can see on the above example. I am trying to insert a message realtime and look at it on rethinkdb dashboard. But this doesn't work. I don't know why.
Rethinkdb query r.db('testRealtime').table('messages').changes()
Since i'm using angular2. Here's the Service I created
import * as io from 'socket.io-client'
export class ChatService {
private url = 'http://localhost:5000'
private socket;
getMessages() {
this.socket = io(this.url);
this.socket.on('messages', function(data){
console.log(data.new_val.query_engine)
})
}
}
On my component, I just call the getMessages form service. Nothing to worry about angular code. I think it is more about the connection of socket.io and rethinkdb.
Any help would be appreciated. Thanks.

Idle Connection Timeout in mongoose

Is there any way to set Idle Connection Timeout in mongoose ?
Am trying like the one below.
mongoose.createConnection(IP:port/{server:{"maxIdleTimeMS":1800000}})
But the above code doesn't seem to work. Any help will be appreciated
this option called connectTimeoutMS not maxIdleTimeMS and it is a socket option. this is a sample code that work :
var uri = 'mongodb://localhost/myDb';
var options = { server: { socketOptions: { connectTimeoutMS: 1800000}}};
mongoose.createConnection(uri, options, function (err) {
if (!err){
console.log("Connection successful");}
});
Good luck

How to return Mongoose results from the find method?

Everything I can find for rending a page with mongoose results says to do it like this:
users.find({}, function(err, docs){
res.render('profile/profile', {
users: docs
});
});
How could I return the results from the query, more like this?
var a_users = users.find({}); //non-working example
So that I could get multiple results to publish on the page?
like:
/* non working example */
var a_users = users.find({});
var a_articles = articles.find({});
res.render('profile/profile', {
users: a_users
, articles: a_articles
});
Can this be done?
You're trying to force a synchronous paradigm. Just does't work. node.js is single threaded, for the most part -- when io is done, the execution context is yielded. Signaling is managed with a callback. What this means is that you either have nested callbacks, named functions, or a flow control library to make things nicer looking.
https://github.com/caolan/async#parallel
async.parallel([
function(cb){
users.find({}, cb);
},
function(cb){
articles.find({}, cb);
}
], function(results){
// results contains both users and articles
});
I'll play the necromancer here, as I still see another, better way to do it.
Using wonderful promise library Bluebird and its promisifyAll() method:
var Promise = require('bluebird');
var mongoose = require('mongoose');
Promise.promisifyAll(mongoose); // key part - promisification
var users, articles; // load mongoose models "users" and "articles" here
Promise.props({
users: users.find().execAsync(),
articles: articles.find().execAsync()
})
.then(function(results) {
res.render('profile/profile', results);
})
.catch(function(err) {
res.send(500); // oops - we're even handling errors!
});
Key parts are as follows:
Promise.promisifyAll(mongoose);
Makes all mongoose (and its models) methods available as functions returning promises, with Async suffix (.exec() becomes .execAsync(), and so on). .promisifyAll() method is nearly-universal in Node.JS world - you can use it on anything providing asynchronous functions taking in callback as their last argument.
Promise.props({
users: users.find().execAsync(),
articles: articles.find().execAsync()
})
.props() bluebird method takes in object with promises as its properties, and returns collective promise that gets resolved when both database queries (here - promises) return their results. Resolved value is our results object in the final function:
results.users - users found in the database by mongoose
results.articles - articles found in the database by mongoose (d'uh)
As you can see, we are not even getting near to the indentation callback hell. Both database queries are executed in parallel - no need for one of them to wait for the other. Code is short and readable - practically corresponding in length and complexity (or rather lack of it) to wishful "non-working example" posted in the question itself.
Promises are cool. Use them.
The easy way:
var userModel = mongoose.model('users');
var articleModel = mongoose.model('articles');
userModel.find({}, function (err, db_users) {
if(err) {/*error!!!*/}
articleModel.find({}, function (err, db_articles) {
if(err) {/*error!!!*/}
res.render('profile/profile', {
users: db_users,
articles: db_articles
});
});
});
Practically every function is asynchronous in Node.js. So is Mongoose's find. And if you want to call it serially you should use something like Slide library.
But in your case I think the easiest way is to nest callbacks (this allows f.e. quering articles for selected previously users) or do it completly parallel with help of async libraries (see Flow control / Async goodies).
I have a function that I use quite a bit as a return to Node functions.
function freturn (value, callback){
if(callback){
return callback(value);
}
return value;
};
Then I have an optional callback parameter in all of the signatures.
I was dealing with a very similar thing but using socket.io and DB access from a client. My find was throwing the contents of my DB back to the client before the database had a chance to get the data... So for what it's worth I will share my findings here:
My function for retrieving the DB:
//Read Boards - complete DB
var readBoards = function() {
var callback = function() {
return function(error, data) {
if(error) {
console.log("Error: " + error);
}
console.log("Boards from Server (fct): " + data);
}
};
return boards.find({}, callback());
};
My socket event listener:
socket.on('getBoards', function() {
var query = dbConnection.readBoards();
var promise = query.exec();
promise.addBack(function (err, boards) {
if(err)
console.log("Error: " + err);
socket.emit('onGetBoards', boards);
});
});
So to solve the problem we use the promise that mongoose gives us and then once we have received the data from the DB my socket emits it back to the client...
For what its worth...
You achieve the desired result by the following code. Hope this will help you.
var async = require('async');
// custom imports
var User = require('../models/user');
var Article = require('../models/article');
var List1Objects = User.find({});
var List2Objects = Article.find({});
var resourcesStack = {
usersList: List1Objects.exec.bind(List1Objects),
articlesList: List2Objects.exec.bind(List2Objects),
};
async.parallel(resourcesStack, function (error, resultSet){
if (error) {
res.status(500).send(error);
return;
}
res.render('home', resultSet);
});