简体   繁体   中英

How to define default settings for each connection in a connection pool with Knex.js

I need to set for each connection in a connection pool default session configurations like TIME_ZONE, LNS_DATE_LANGUAGE and others for Oracle database. Knex.js documentation provide a snippet demonstrated below but for PostgreSQL:

var knex = require('knex')({
  client: 'pg',
  connection: {hos: ''},
  pool: {
    afterCreate: function (conn, done) {
      // in this example we use pg driver's connection API
      conn.query('SET timezone="UTC";', function (err) {
        if (err) {
          // first query failed, return error and don't try to make next query
          done(err, conn);
        } else {
          // do the second query...
          conn.query('SELECT set_limit(0.01);', function (err) {
            // if err is not falsy, connection is discarded from pool
            // if connection aquire was triggered by a query the error is passed to query promise
            done(err, conn);
          });
        }
      });
    }
  }
});

The oracledb way of doing that is defining a callback for the sessionCallback attribute:

async function init() {
  try {
    await oracledb.createPool({
      user: 'USER',
      password: 'PWD',
      connectString: 'HOST:1521/SERVICE',
      sessionCallback: initSession //HERE MY DEFAULT SETTINGS
    });
  } catch (e) {
    console.error(e);
  }
}

async function initSession(connection, requestedTag, cb) {
  try {
    await connection.execute(`
      ALTER SESSION SET 
      TIME_ZONE='UTC'
      NLS_LANGUAGE = 'BRAZILIAN PORTUGUESE'
      NLS_TERRITORY = 'BRAZIL'
      NLS_CURRENCY = 'R$'
      NLS_ISO_CURRENCY = 'BRAZIL'
      NLS_NUMERIC_CHARACTERS = ',.'
      NLS_CALENDAR = 'GREGORIAN'
      NLS_DATE_FORMAT = 'DD/MM/YYYY'
      NLS_DATE_LANGUAGE = 'BRAZILIAN PORTUGUESE'
      NLS_SORT = 'WEST_EUROPEAN'
      NLS_TIMESTAMP_FORMAT = 'DD/MM/YYYY HH24:MI:SS'
      NLS_DUAL_CURRENCY = 'R$'
    `);
  } catch (e) {
    console.error(e);
    cb(e);
  } finally {
    if (connection) {
      await connection.close();
    }
  }
}

I would like to be able of translating this snippet above for a Knex.js implementation. How to do that?

Does your KNEX use Oracle DB's connection pool?? https://github.com/tgriesser/knex/issues/2665 show it as a future project.

Try the pg snippet you posted, buyt using the ALTER SESSION from your other example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM