sidorares/node-mysql2

TypeCast seems doesnt work

afwn90cj93201nixr2e1re opened this issue ยท 16 comments

const mysql2 = require('mysql2');

const pool = mysql2.createPool({
    host:'localhost', user: 'admindb', database: 'tests',password:'password',charset:'UTF8MB4_UNICODE_CI',timezone:'Z',
    typeCast: (field, next) => {
        console.log('typeCast');
        if (field.type === "BIT") {
            console.log(field);
            console.log(field.buffer());
            return field.buffer()[0] == 1;
        }
        return next()
    }

});;
const promisePool = pool.promise();
export default {pool, promisePool}

Doesn't call with execute.
dup: #649 #347

How i can turn to bool/int?
https://dev.mysql.com/doc/refman/8.0/en/bit-type.html

Is there typical pure c way's to convert to bool? like !!var.

image
Seems doesn't work.

yes, this is known problem. Unfortunately typeCast api is too low level and resullts for execurte come in different binary format, I haven't figured out yet good way of making typeCast work for execute with compatible api

@sidorares ok, so, how convert BinaryRow to json obj?

Just do it in your code that consumes data? Not very central, but usually you have some mapping function anyway data -> model or view

So, i have only clear code. Without that.
And need payload for jwt.

      console.log(rows[rows.length-1]);

BinaryRow {
  id: 1,
  name: 'dsad',
  surename: 'asdad',
  fname: 'd',
  login: 'abcd',
  password: 'qwdqwd',
  group: 1,
  type: 's',
  active: <Buffer 01>,
  login_changed: <Buffer 00> }
const lastRow = rows.slice(-1)
console.log(JSON.stringify({ ...lastRow, active: lastRow.actibe === 1, login_changed: lastRow.login_changed[0] === 1 }, null, 2));

Is that normal way?

 let payload = JSON.parse(JSON.stringify(rows[rows.length-1]));
const lastRow = rows[0];
      console.log(JSON.stringify({ ...lastRow, active: lastRow.active[0] === 1, login_changed: lastRow.login_changed[0] === 1 }, null, 2));

@sidorares thank's, that work perf.

      console.log(JSON.parse(JSON.stringify({ ...lastRow, active: lastRow.active[0] === 1, login_changed: lastRow.login_changed[0] === 1 }, null, 2)));

no need for JSON.parse(JSON.stringify

But i need it for let payload, no?

      let payload = JSON.parse(JSON.stringify({ ...lastRow, active: lastRow.active[0] === 1, login_changed: lastRow.login_changed[0] === 1 }));
delete payload.password;

also

 const lastRow = rows.slice(-1)[0]; //right?
const payload = { ...lastRow, active: lastRow.active[0] === 1, login_changed: lastRow.login_changed[0] === 1 };

to avoid delete payload.password:

const { password, ...payload } = { ...lastRow, active: lastRow.active[0] === 1, login_changed: lastRow.login_changed[0] === 1 };

Now, Whether the feature(typeCast) is supported ?

euglv commented

I have the same issue.
Are there any way to convert all BIT(1) fields to boolean in one place for all queries? Or the only option is not to use the prepared statements?