dmanjunath / node-redshift

A simple collection of tools to help you get started with Amazon Redshift from node.js
69 stars 48 forks source link

Problems with raw connections #17

Closed jotajunior closed 7 years ago

jotajunior commented 7 years ago

Hello!

It seems like your library is mostly suited for connection pooling, which is understandable. But my (and many others') usecase is just a simple connection opening, query and closing (inside the callback).

Having to execute the query as a callback to the connect function is a major drawback, along with not having an instance factory, which is the case for most mature database drivers.

In this sense, I've developed something that made my life really easy, and maybe you can integrate.

It's just a Redshift object factory and an easy way to query directly:

var redshiftFactory = function() {
    var redshiftClient = new Redshift(client, {rawConnection: true});
    redshiftClient.rawQuery = function(query, queryCb) {
        redshiftClient.connect(function(err) {
            if (err) console.log(err);
            else {
                redshiftClient.query(query, {raw: true}, function(err, data) {
                    if (err) {
                        console.log(err);
                    } else {
                        if (typeof queryCb === 'function') {
                            queryCb(err, data);
                        }
                        console.log(data);
                        redshiftClient.close();
                    }
                });
            }
        });
    };

    return redshiftClient;
}

module.exports = redshiftFactory;

This way, I can just call:

const redshiftClient = redshiftFactory();
redshiftClient.rawQuery('SELECT * FROM users', function(err, data) {});

Without worrying about connecting, all the ugly nested callbacks and also tightly controlling the scope of my connection.

Since Redshift is widely used for analytics (and since you'll find people using Node.js just as way to serve a data collecting API), a lot of people may be in the need of just using simple, straightforward, and fast connection handling.

Hope it helps

dmanjunath commented 7 years ago

@jotajunior I like this idea a lot! Just a helper function of sorts for a single raw query that creates the connection, runs the query and returns the data?

Done. It'll be in the next release.

dmanjunath commented 7 years ago

pushed to master and published to npm v0.1.4