mysqlnode.jsmysql2node-mysql2

How do I determine the column type name from the columnType integer value in mysql2?


When querying with mysql2, the third argument to the callback function fields has the following documentation:

console.log(fields); // fields contains extra meta data about results, if available

Ok, great. But when I look at the values in the fields array, I see the following:

[
      {
        "_buf":{},
        "_clientEncoding":"utf8",
        "_catalogLength":3,
        "_catalogStart":10,
        "_schemaLength":0,
        "_schemaStart":14,
        "_tableLength":11,
        "_tableStart":15,
        "_orgTableLength":0,
        "_orgTableStart":27,
        "_orgNameLength":2,
        "_orgNameStart":31,
        "characterSet":63,
        "encoding":"binary",
        "name":"id",
        "columnLength":11,
        "columnType":3, // <-- column type 3? This is an INTEGER field
        "flags":1,
        "decimals":0
      },
      {
        ...
        "columnType":253, // <-- column type 253? This is a VARCHAR field
        ...
      }
]

How do I determine what the actual column type is from these integer values?


Solution

  • After doing some more digging, I was able to find the answer by looking at the source. mysql2 exposes a Types field that has the column type names as keys with the column id as values.

    At the time of posting, that list looks like this:

    {
      "DECIMAL": 0,
      "TINY": 1,
      "SHORT": 2,
      "LONG": 3,
      "FLOAT": 4,
      "DOUBLE": 5,
      "NULL": 6,
      "TIMESTAMP": 7,
      "LONGLONG": 8,
      "INT24": 9,
      "DATE": 10,
      "TIME": 11,
      "DATETIME": 12,
      "YEAR": 13,
      "NEWDATE": 14,
      "VARCHAR": 15,
      "BIT": 16,
      "JSON": 245,
      "NEWDECIMAL": 246,
      "ENUM": 247,
      "SET": 248,
      "TINY_BLOB": 249,
      "MEDIUM_BLOB": 250,
      "LONG_BLOB": 251,
      "BLOB": 252,
      "VAR_STRING": 253,
      "STRING": 254,
      "GEOMETRY": 255
    }