Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeCast seems doesnt work #948

Closed
afwn90cj93201nixr2e1re opened this issue Apr 12, 2019 · 16 comments
Closed

TypeCast seems doesnt work #948

afwn90cj93201nixr2e1re opened this issue Apr 12, 2019 · 16 comments
Labels

Comments

@afwn90cj93201nixr2e1re
Copy link
Contributor

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

image
Seems doesn't work.

@sidorares
Copy link
Owner

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

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

@sidorares
Copy link
Owner

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

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

@sidorares
Copy link
Owner

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

Is that normal way?

 let payload = JSON.parse(JSON.stringify(rows[rows.length-1]));

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

afwn90cj93201nixr2e1re commented Apr 12, 2019

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

@sidorares
Copy link
Owner

no need for JSON.parse(JSON.stringify

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

afwn90cj93201nixr2e1re commented Apr 12, 2019

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?

@sidorares
Copy link
Owner

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

@afwn90cj93201nixr2e1re
Copy link
Contributor Author

Thx.

@jjeejj
Copy link

jjeejj commented Dec 2, 2020

Now, Whether the feature(typeCast) is supported ?

@euglv
Copy link

euglv commented Jul 31, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants