Skip to content

Commit

Permalink
add examples for each type of connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dmanjunath committed Jan 6, 2017
1 parent 0606f92 commit 95f75cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ module.exports = redshiftClient;
You can either initialize a raw one time connection and close it after a single query, or you can open a connection pool and leave it open while your application is running.

##### ***By default node-redshift uses connection pooling
##### ***If you want to close a redshift connection by hand you have to use raw connection, you can't end a pool
####
##### rawConnection
Pass in the rawConnection parameter in the redshift instantiation options to specify a raw connection.
```javascript
var redshiftClient = new Redshift(client, {rawConnection: true});
```
#### Usage
Please see examples/ folder for full code examples.
##### Default connection
The redshift.js file exports a Redshift object which has a `query()` function bound to it you can call with the string of a sql query. I like [sql-bricks](http://csnw.github.io/sql-bricks/) to build queries.
```javascript
Expand Down
21 changes: 21 additions & 0 deletions examples/connection_pooling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var Redshift = require('../index.js');

var client = {
user: 'user',
database: 'database',
password: 'password',
port: 'port',
host: 'host'
};

var redshift = new Redshift(client); //no need to call connect(), without rawConnection, it automatically connects

redshift.query('SELECT * FROM "Tags"', {raw: true}, function(err, data){
if(err) throw err;
else{
console.log(data);
//if you want to close client pool
// but you won't be able to make subsequent calls because connection is terminated
redshift.close();
}
});
24 changes: 24 additions & 0 deletions examples/raw_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Redshift = require('../index.js');

var client = {
user: 'user',
database: 'database',
password: 'password',
port: 'port',
host: 'host'
};

var redshift = new Redshift(client, {rawConnection: true});

redshift.connect(function(err){ //create connection manually
if(err) throw err;
else{
redshift.query('SELECT * FROM "Tags"', {raw: true}, function(err, data){ //query redshift
if(err) throw err;
else{
console.log(data);
redshift.close(); //close connection manually
}
});
}
});

0 comments on commit 95f75cb

Please sign in to comment.