diff --git a/src/database/connection.ts b/src/database/connection.ts index 6c51eed..92c8380 100644 --- a/src/database/connection.ts +++ b/src/database/connection.ts @@ -1,8 +1,9 @@ -import type { Connection, PoolConnection } from 'mysql2/promise'; +import type { Connection, PoolConnection, TypeCast } from 'mysql2/promise'; import { scheduleTick } from '../utils/scheduleTick'; import { sleep } from '../utils/sleep'; import { pool } from './pool'; import type { CFXParameters } from 'types'; +import { typeCastExecute } from 'utils/typeCast'; (Symbol as any).dispose ??= Symbol('Symbol.dispose'); @@ -34,7 +35,11 @@ export class MySql { async execute(query: string, values: CFXParameters = []) { scheduleTick(); - const [result] = await this.connection.execute(query, values); + const [result] = await this.connection.execute({ + sql: query, + values: values, + typeCast: typeCastExecute as TypeCast, + }); return result; } diff --git a/src/utils/typeCast.ts b/src/utils/typeCast.ts index eff3cc6..fd255a9 100644 --- a/src/utils/typeCast.ts +++ b/src/utils/typeCast.ts @@ -1,30 +1,35 @@ -import { FieldPacket } from 'mysql2'; +import type { TypeCastField, TypeCastNext } from 'mysql2/promise'; const BINARY_CHARSET = 63; -type Field = { - type: string; - length: number; - packet: FieldPacket; +interface Field extends TypeCastField { charset: number; - string: () => string; - buffer: () => number[]; -}; +} -export const typeCast = (field: Field, next: () => void) => { +/** + * node-mysql2 v3.9.0 introduced (breaking) typecasting for execute methods. + */ +export function typeCastExecute(field: Field, next: TypeCastNext) { + return next(); +} + +/** + * mysql-async compatible typecasting. + */ +export function typeCast(field: Field, next: TypeCastNext) { switch (field.type) { case 'DATETIME': case 'DATETIME2': case 'TIMESTAMP': case 'TIMESTAMP2': case 'NEWDATE': - return new Date(field.string()).getTime(); + return new Date(field.string() || '').getTime(); case 'DATE': return new Date(field.string() + ' 00:00:00').getTime(); case 'TINY': return field.length === 1 ? field.string() === '1' : next(); case 'BIT': - return field.length === 1 ? field.buffer()[0] === 1 : field.buffer()[0]; + return field.length === 1 ? field.buffer()?.[0] === 1 : field.buffer()?.[0]; case 'TINY_BLOB': case 'MEDIUM_BLOB': case 'LONG_BLOB': @@ -38,4 +43,4 @@ export const typeCast = (field: Field, next: () => void) => { default: return next(); } -}; +}