Node-mysql2: TypeCast seems doesnt work

Created on 12 Apr 2019  路  15Comments  路  Source: sidorares/node-mysql2

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: https://github.com/sidorares/node-mysql2/issues/649 https://github.com/sidorares/node-mysql2/issues/347

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

question

Most helpful comment

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 };

All 15 comments

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 };

Thx.

Now, Whether the feature(typeCast) is supported ?

Was this page helpful?
0 / 5 - 0 ratings