node-mysql2: TypeCast seems doesnt work

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 <Buffer xx> to bool/int? https://dev.mysql.com/doc/refman/8.0/en/bit-type.html

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 16 (14 by maintainers)

Most upvoted comments

Now, Whether the feature(typeCast) is supported ?

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 };
const lastRow = rows.slice(-1)
console.log(JSON.stringify({ ...lastRow, active: lastRow.actibe === 1, login_changed: lastRow.login_changed[0] === 1 }, null, 2));