JavaScript RethinkDB and Node.js/Express - Opening multiple connections for parallel queries? rethinkdb nodejs,rethinkdb nodejs example,rethinkdb nodejs npm,rethinkdb nodejs

Is there a better way to run parallel queries with the RethinkDB Node driver than opening multiple connections per request? Or is this actually a good way of going about what I need? I'd prefer to stay away from connection pools/third-party packages. The app in question has a singleton containing functions that wrap RethinkDB queries. Those functions handle creating and closing connections. This pattern allows me to require the database service in multiple routers with minimal overhead and without it having to know about each request. Contrived example to explain how unrelated the data may be that gets queried:

database.js

var r = require('rethinkdb');

module.exports = {

    getApples: function(callback) {
        r.connect(/* config */)
            .then(function(conn){
                r.db('fruitDatabase').table('apples').run(conn)
                    .then(function(cursor){
                        return cursor.toArray();
                    })
                    .then(function(apples){
                        return callback(null, apples);
                    })
                    .finally(function(){
                        return conn.close();
                    });
            });
    },
    getPotatoes: function(callback) {
        r.connect(/* config */)
            .then(function(conn){
                r.db('vegetableDatabase').table('potatoes').run(conn)
                    .then(function(cursor){
                        return cursor.toArray();
                    })
                    .then(function(potatoes){
                        return callback(null, potatoes);
                    })
                    .finally(function(){
                        return conn.close();
                    });
            });
    }
};

Now, I need to create a page/endpoint that displays all the apples and all the potatoes, so I currently call both of those functions via async.parallel in my page router:

pages.js

var pages = require('express').Router(),
    async = require('async'),
    db = require('./database');

pages.route('/food')
    .get(function(req, res, next){
        async.parallel({
            apples: db.getApples,
            potatoes: db.getPotatoes
        }, function(err, data){
            if(err) return next(err);

            res.render('food',
                {
                    apples: data.apples,
                    potatoes: data.potatoes
                });
        });
    });

Thoughts? What if it were 4 connections opened in parallel (or many more)?

Answer:1

If you cannot wait for the official driver to get a connection pool, You could also use a middleware to open a connection for each request, as in this example from the official doc :

app.use(createConnection);                      // Create a RethinkDB connection

function createConnection(req, res, next) {
    r.connect(config.rethinkdb, function(error, conn) {
        if (error) {
            handleError(res, error);
        }
        else {
            // Save the connection in `req`
            req._rdbConn = conn;
            // Pass the current request to the next middleware
            next();
        }
    });
}
Answer:2

I think that RethinkDB can handle many parallel connections just fine. It also maybe easier to work with. If you use a share connection for all, we have to deal with re-try to connect when connection break, and have to ensure it is open throught the life cycle of request or the sever.

This RethinkDB driver https://github.com/neumino/rethinkdbdash runs every query on its own connection.

The driver execute one query per connection. Now that rethinkdb/rethinkdb#3296 is solved, this behavior may be changed in the future.

More than that, if you ever bring change feed into your app, you will be very likely want change feed on its own connection according to http://docs.rethinkdb.com/2.1/api/javascript/changes/

It’s a good idea to open changefeeds on their own connection. If you don’t, other queries run on the same connection will experience unpredictable latency spikes while the connection blocks on more changes.

So, in other word, I say go ahead and let's make it simple and easier by using many parallel connections.

Answer:3

This is my FIDDLE <table> <tr> <td> <div style="background:blue;color:white">hello</div> </td> </tr> <tr> ...

This is my FIDDLE <table> <tr> <td> <div style="background:blue;color:white">hello</div> </td> </tr> <tr> ...

  1. sort table rows javascript
  2. sort table rows matlab
  3. sort table rows with jquery
  4. sort table rows
  5. sort table rows in word
  6. sort table rows html
  7. sort table rows using ajax php
  8. sort table rows angularjs
  9. sort table rows using jquery
  10. sort table rows angular
  11. sort table rows mysql
  12. sort table row php
  13. order table rows jquery
  14. order table rows javascript
  15. order table rows
  16. order table rows html
  17. sort pivot table rows
  18. javascript sort table rows by column
  19. js sort table rows
  20. jquery sort table rows by column value

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...

Is it possible in js to create a date object that is not affected by timezone? For example i want to set the year "2015", the month "03" and the day "31". var ex = new Date(2015,3,31); If i "print" ...

Is it possible in js to create a date object that is not affected by timezone? For example i want to set the year "2015", the month "03" and the day "31". var ex = new Date(2015,3,31); If i "print" ...

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

  1. dynamic jquery dialog
  2. jquery dynamic dialog box example
  3. jquery dynamic dialog example
  4. dynamic jquery ui dialog
  5. jquery dynamic modal dialog
  6. create dynamic dialog jquery
  7. jquery dynamic confirm dialog
  8. dynamic content in jquery dialog
  9. dynamic buttons dialog jquery
  10. jquery dialog dynamic width
  11. jquery dialog dynamic title
  12. jquery dialog dynamic height
  13. jquery dialog dynamic button text
  14. jquery dialog dynamic text
  15. jquery dialog dynamic size
  16. jquery dialog dynamic html
  17. jquery dialog dynamic position
  18. jquery dialog dynamic div
  19. jquery dialog dynamic message