Skip to content

SNOW-1016934: support setting session variables from Connection #756

Open
@dhpcc

Description

@dhpcc

Please answer these questions before submitting your issue.
In order to accurately debug the issue this information is required. Thanks!

  1. What version of NodeJS driver are you using?
    1.9.0

  2. What operating system and processor architecture are you using?
    Mac M2

  3. What version of NodeJS are you using?
    (node --version and npm --version)
    v21.6.0

  4. What are the component versions in the environment (npm list)?

5.Server version:* E.g. 1.90.1
You may get the server version by running a query:

SELECT CURRENT_VERSION();
  1. What did you do?

snowflake.createConnection() receives a config object that specifies a key/value for the session parameter.

const snowflake = require('snowflake-sdk');

const createSnowflakeConnectionWithSessionVar = (orgKey, orgValue) => {
  const {
    SNOWFLAKE_ACCOUNT_IDENTIFIER,
    SNOWFLAKE_USERNAME,
    SNOWFLAKE_PASSWORD,
  } = require('../utils/config');

  // Create a new connection
  const connection = snowflake.createConnection({
    account: SNOWFLAKE_ACCOUNT_IDENTIFIER,
    username: SNOWFLAKE_USERNAME,
    password: SNOWFLAKE_PASSWORD,
    application: 'CORE_PLATFORM',
    clientSessionKeepAlive: true,
    clientSessionKeepAliveHeartbeatFrequency: 3600,
    sessionParameters: {
      [orgKey]: orgValue,
    },
  });

  // Return a Promise that resolves with the connection
  return new Promise((resolve, reject) => {
    connection.connect((err, conn) => {
      if (err) {
        reject(err);
      } else {
        resolve(conn);
      }
    });
  });
};

module.exports = createSnowflakeConnectionWithSessionVar;

Usage in backend middleware function:

try {
    const conn = await createSnowflakeConnectionWithSessionVar('client', org);

    conn.execute({
      sqlText: query,
      complete: function (err, stmt, rows) {
        if (err) {
          console.error('Failed to execute query', err);
          next(err);
        } else {
          const random = rows.map((row) => row.RANDOM);
          res.locals.random = random;
          next();
        }
        conn.destroy((err, conn) => {
          if (err) {
            console.error('Failed to close connection', err);
          }
        });
      },
    });
  } catch (error) {
    console.error('Connection error', error);
    next(error);
  }
  1. What did you expect to see?

I expected the session parameter to be set to whatever I call this function with. Instead, the session parameter remains undefined. I looked through the API documentation and can't seem to find how to do this.
Snowflake Session Variables

What should have happened and what happened instead?

  1. Can you set logging to DEBUG and collect the logs?

    https://community.snowflake.com/s/article/How-to-generate-log-file-on-Snowflake-connectors

e.g
Add this to get standard output.

var snowflake = require('snowflake-sdk');
snowflake.configure(
{
  logLevel: 'trace'
});
  1. What is your Snowflake account identifier, if any? (Optional)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementThe issue is a request for improvement or a new featureparitythis feature is supported in other driversstatus-triage_doneInitial triage done, will be further handled by the driver team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions