1

I have inherited an node.js app for RESTFUL api, which uses knex pooling to connect to mysql. I have a nned to perform multiple queries in single function (statement), and as I understand, for that I need multipleStatemnt to be set to truu in knex pooling settings. I have done that:

    const connection = (user, password) =>
      knex({
       client: "mysql",
       connection: {
       multipleStatements: true,
       host: process.env.MYSQL_IP,
       port: process.env.MYSQL_PORT,
       user,
       password,
       database: "",
       dateStrings: true,
          }
});

However, this does not seem to get applied and I cannot execute multiple statements in single query (as per documentation in mysql pooling):

var qString = 'SELECT ?;SELECT ?'
   self._client.query(qString, [1,5], function (err, result) {

And if I check my client, I see that multipleStatements are still being set to false:

    Pool {
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  config:
   PoolConfig {
     acquireTimeout: 10000,
     connectionConfig:
      ConnectionConfig {
        host: 'xxx.xxx.xxx.xxx',
        port: 'xxxx',
        localAddress: undefined,
        socketPath: undefined,
        user: 'xxxxxxx',
        password: 'xxxxxxx',
        database: 'xxxxxxxxx',
        connectTimeout: 10000,
        insecureAuth: false,
        supportBigNumbers: false,
        bigNumberStrings: false,
        dateStrings: false,
        debug: undefined,
        trace: true,
        stringifyObjects: false,
        timezone: 'local',
        flags: '',
        queryFormat: undefined,
        pool: [Circular],
        ssl: false,
        multipleStatements: false,
        typeCast: true,
        maxPacketSize: 0,
        charsetNumber: 33,
        clientFlags: 455631 },
     waitForConnections: true,

So my question is - - is there anywhere else I can change this setting (Express session or???) or if perhaps someone has some ideas?

Gregor Sotošek
  • 357
  • 1
  • 4
  • 22

1 Answers1

2

I can't find multipleStatements: true related settings in knex doc

but according to this issue

maybe you can manually join your multiple statements in one query string like:

const queries = [
   knex.table("game0.stats").update(updateCurrency),
   knex.table("game1.stats").update(updateCurrency),
   knex.table("game2.stats").update(updateCurrency),
];
const multiQuery = queries.join(";");
console.log(multiQuery);
return knex.raw(multiQuery)
   .then((result) => {
   })
   .catch((error) => {
   });

furthermore, you may wrap a function before execute query:

function executeMultiStatements(queries) {
  // do some checks
  // avoid sql injection
  // ...
  const multiQuery = queries.join(';')
  return knex.raw(multiQuery);
}
Sinux
  • 1,728
  • 3
  • 15
  • 28
  • The `multipleStatements` configuration is a driver option. Knex passes all driver options to the `mysql` driver. Here is the documentation for the driver - https://github.com/mysqljs/mysql#multiple-statement-queries also this comment explains that it can work with `knex.raw` - https://github.com/knex/knex/issues/257#issuecomment-394129840 – Wayne Bloss Dec 07 '20 at 16:37