refactor(utils/typeCast): Change BIT and BLOB types

BIT fields should only cast as boolean if they have a length of 1.
This commit is contained in:
Linden committed 2022-02-10 17:23:42 +11:00
1 parent 4975a17a31
commit 7096237ce2
1 file changed
+15 -2
+15 -2
View File
@@ -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();
}