From 71f5c69cd8578be296582ec87dca0b772ba796a7 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 4 Jul 2024 11:21:57 +1000 Subject: [PATCH] feat(utils/typeCast): support date typecasting for binary protocol Support for execute typecasting (used by prepare/rawExecute methods) was added recently, but did not support dates until 3.10.2. --- src/utils/typeCast.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/utils/typeCast.ts b/src/utils/typeCast.ts index 456ecfe..bdb8b9c 100644 --- a/src/utils/typeCast.ts +++ b/src/utils/typeCast.ts @@ -6,7 +6,22 @@ const BINARY_CHARSET = 63; * node-mysql2 v3.9.0 introduced (breaking) typecasting for execute methods. */ export function typeCastExecute(field: TypeCastField, next: TypeCastNext) { - return next(); + switch (field.type) { + case 'DATETIME': + case 'DATETIME2': + case 'TIMESTAMP': + case 'TIMESTAMP2': + case 'NEWDATE': { + const value = field.string(); + return value ? new Date(value).getTime() : null; + } + case 'DATE': { + const value = field.string(); + return value ? new Date(value + ' 00:00:00').getTime() : null; + } + default: + return next(); + } } /**