From 7096237ce29d229c7c8e875d41e4641dd26d7e9f Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 10 Feb 2022 17:23:42 +1100 Subject: [PATCH] refactor(utils/typeCast): Change BIT and BLOB types BIT fields should only cast as boolean if they have a length of 1. --- src/utils/typeCast.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/utils/typeCast.ts b/src/utils/typeCast.ts index fee724a..6059f92 100644 --- a/src/utils/typeCast.ts +++ b/src/utils/typeCast.ts @@ -1,6 +1,9 @@ +import { FieldPacket } from 'mysql2'; + type Field = { type: string; length: number; + packet: FieldPacket; string: () => string; buffer: () => number[]; }; @@ -13,11 +16,21 @@ export const typeCast = (field: Field, next: () => void) => { case 'TIMESTAMP2': case 'NEWDATE': case 'DATE': - return field.type === 'DATE' ? new Date(field.string() + ' 00:00:00').getTime() : new Date(field.string()).getTime(); + return field.type === 'DATE' + ? new Date(field.string() + ' 00:00:00').getTime() + : new Date(field.string()).getTime(); case 'TINY': return field.length === 1 ? field.string() === '1' : next(); case 'BIT': - return field.buffer()[0] === 1; + return field.length === 1 ? field.buffer()[0] === 1 : field.buffer()[0]; + case 'TINY_BLOB': + case 'MEDIUM_BLOB': + case 'LONG_BLOB': + case 'BLOB': + if (field.packet.charsetNr === 63) { + return [...field.buffer()]; + } + return field.string(); default: return next(); }