Muidul Alam 2hin
Muidul Alam 2hin

Reputation: 189

Work around for sequelize boolean value from mysql database

I am getting data from mysql database to express rest api app. Using sequelize as a ORM. When it is comes to a BIT(1) value from mysql, sequelize returns a instance of buffer object.

{
  "id": 4,
  "ProductPrice": 12.25,
  "ProductQuantityOnHand": 0,
  "ProductCode": "P486",
  "ProductName": "FirstProduct",
  "ProductDescription": null,
  "ProductActive": {
      "type": "Buffer",
      "data": [
          1
      ]
  },
  "createdAt": "2019-02-02T11:27:00.000Z",
  "updatedAt": "2019-02-02T11:27:00.000Z"
}

Like here product active a BIT(1) and sequelize returning a object.

How can I get boolean value instead of an object?

Like this.

{
  "id": 4,
  "ProductPrice": 12.25,
  "ProductQuantityOnHand": 0,
  "ProductCode": "P486",
  "ProductName": "FirstProduct",
  "ProductDescription": null,
  "ProductActive": true,
  "createdAt": "2019-02-02T11:27:00.000Z",
  "updatedAt": "2019-02-02T11:27:00.000Z"
}

Upvotes: 1

Views: 2812

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522292

I might suggest that you just use an INT column in your MySQL table. Assuming you only store values 0 and 1, these same values should show up in your ORM/application layer.

As the value 0 is "falsy" in JavaScipt, it would logically behave the same way as false, and vice-versa for 1, which is "truthy."

Upvotes: 1

Related Questions