fix(database/connection): disable typecasting for execute method

This commit is contained in:
Linden committed 2024-06-17 16:39:53 +10:00
1 parent 0f385900b4
commit 0691160aeb
2 files changed
+24 -14

No files matched your search

+7 -2
View File
@@ -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 { scheduleTick } from '../utils/scheduleTick';
import { sleep } from '../utils/sleep'; import { sleep } from '../utils/sleep';
import { pool } from './pool'; import { pool } from './pool';
import type { CFXParameters } from 'types'; import type { CFXParameters } from 'types';
import { typeCastExecute } from 'utils/typeCast';
(Symbol as any).dispose ??= Symbol('Symbol.dispose'); (Symbol as any).dispose ??= Symbol('Symbol.dispose');
@@ -34,7 +35,11 @@ export class MySql {
async execute(query: string, values: CFXParameters = []) { async execute(query: string, values: CFXParameters = []) {
scheduleTick(); 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; return result;
} }
+17 -12
View File
@@ -1,30 +1,35 @@
import { FieldPacket } from 'mysql2'; import type { TypeCastField, TypeCastNext } from 'mysql2/promise';
const BINARY_CHARSET = 63; const BINARY_CHARSET = 63;
type Field = { interface Field extends TypeCastField {
type: string;
length: number;
packet: FieldPacket;
charset: number; 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) { switch (field.type) {
case 'DATETIME': case 'DATETIME':
case 'DATETIME2': case 'DATETIME2':
case 'TIMESTAMP': case 'TIMESTAMP':
case 'TIMESTAMP2': case 'TIMESTAMP2':
case 'NEWDATE': case 'NEWDATE':
return new Date(field.string()).getTime(); return new Date(field.string() || '').getTime();
case 'DATE': case 'DATE':
return new Date(field.string() + ' 00:00:00').getTime(); return new Date(field.string() + ' 00:00:00').getTime();
case 'TINY': case 'TINY':
return field.length === 1 ? field.string() === '1' : next(); return field.length === 1 ? field.string() === '1' : next();
case 'BIT': 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 'TINY_BLOB':
case 'MEDIUM_BLOB': case 'MEDIUM_BLOB':
case 'LONG_BLOB': case 'LONG_BLOB':
@@ -38,4 +43,4 @@ export const typeCast = (field: Field, next: () => void) => {
default: default:
return next(); return next();
} }
}; }