Connecting to SQream with nodejs connector

This article describes how to create a new connection to SQream for a nodejs application.

The SQream NodeJS connector works on node version 8.x

Prerequisites 

1) Install node.js version 8.x

Requirements 

1) Download sqream connector module: sqream-connector-{version}.tgz

2) Install the connector with npm install from file:

sudo npm install sqream-connector-{version}.tgz

 Create a sample connection for testing

Below is a sample nodejs file you can use to test your connection. Make sure to edit the required details, such as <your server address><your server port><your username> and <your password>, etc.

For example: node_connector.js

const Connection = require('sqream-connector');
const config = { 
  host: '<your server address>',
  port: <your server port>,
  username: '<your server port>',
  password: '<your password>',
  connectDatabase: '<your database>',
  cluster: '<true/false>',
  };
  const query1 = "SELECT 1 as test, 2 as other_test";
  const myConnection = new Connection(config);
  myConnection.runQuery(query1, function (err, data){
console.log(err, data);
});

Run your file with node

node test_sqream.js

On a successful connection, you should see:

null [ { test: 1, other_test: 2 } ]

Otherwise, please contact SQream support with the error description.

Events

The sqream-connector has a few events return with event emitter.

getConnectionId

getConnectionId event return the executing connection Id.

getStatementId

getStatementId event return the executing statement Id

getTypes

getTypes event return the results columns types


Events example

const myConnection = new Connection(config);

myConnection.runQuery(query1, function (err, data){
  myConnection.events.on('getConnectionId', function(data){
console.log('getConnectionId', data);
});

myConnection.events.on('getStatementId', function(data){
console.log('getStatementId', data);
});

myConnection.events.on('getTypes', function(data){
console.log('getTypes', data);
});
});

Set statement Flag

You can add a 'SET' flag as for each statement run with connector. Add SET flag as a second parameter on 'runQuery' function.

const setFlag = 'SET showfullexceptioninfo = true;'

const myConnection = new Connection(config);
myConnection.runQuery(query1, function (err, data){
console.log(err, data);
}, setFlag);

Limitation

The node application which utilizes the sqream connector should consider the heap size node configuration.

 When processing large datasets,It is recommended to increase the application heap size with the "--max_old_space_size" node run flag:

node --max_old_space_size={heapSize} my-application.js

 In addition,

The following error mentions that the heap size configured is not sufficient:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

  • 138
  • 08-Aug-2018
  • 896 Views