mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

BIT column gets converted to a Buffer object instead of a boolean #188

Closed octogonz closed 2 years ago

octogonz commented 2 years ago

Repro steps:

  1. Create a table with a BIT column:

      CREATE TABLE `example` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `boolExample` BIT(1) NOT NULL DEFAULT b'0',
    PRIMARY KEY (`id`) USING BTREE,
    )
  2. Insert some rows in the table

  3. Use the mariadb NPM package to query the rows:

     const result = await connection.query('SELECT * FROM example');

Expected: The result[0].boolExample value should be a familiar JavaScript type such as true or false

Actual: The value is returned as Node.js Buffer, which requires an awkward boolean conversion such as result[0].boolExample.readInt8() !== 0.

octogonz commented 2 years ago

🤔 Reading the docs more closely, BIT supports arrays of bits. So maybe it would be counterintuitive for an array-of-length-one to be treated as boolean.

Seems like a better solution is to change my SQL schema to use INT for representing boolean values instead of BIT.