fix: Scalar return value

This commit is contained in:
erik.balog committed 2022-01-23 13:10:34 +01:00
1 parent e9721ca5ca
commit 38e30c6640
2 files changed
+7 -4

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ import { RowDataPacket, ResultSetHeader, OkPacket } from 'mysql2';
type SQLResponse = RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader;
export type QueryResponse = SQLResponse | SQLResponse[];
export type QueryResponse = SQLResponse | SQLResponse[] | RowDataPacket;
export type QueryType = 'insert' | 'update' | 'scalar' | 'single' | null;
+6 -3
View File
@@ -4,12 +4,15 @@ import type { QueryResponse, QueryType } from '../types';
const isOkPacket = (result: QueryResponse, insertId?: boolean): result is OkPacket =>
insertId ? (result as OkPacket).insertId !== undefined : (result as OkPacket).affectedRows !== undefined;
const isRowDataPacket = (result: QueryResponse): result is RowDataPacket[] => (result as RowDataPacket[]).length !== undefined;
const isRowDataPacket = (result: QueryResponse): result is RowDataPacket[] =>
(result as RowDataPacket[]).length !== undefined;
const isRowDataPacketArray = (result: QueryResponse): result is RowDataPacket[][] =>
(result as RowDataPacket[][]).length !== undefined && (result as RowDataPacket[][])[0].length !== undefined;
(result as RowDataPacket[][]).length !== undefined &&
(result as RowDataPacket[][])[0] !== undefined &&
Object.values((result as RowDataPacket[][])[0])[0] !== undefined;
export const parseResponse = (type: QueryType, result: QueryResponse): QueryResponse | RowDataPacket | number | null => {
export const parseResponse = (type: QueryType, result: QueryResponse): QueryResponse | number | null => {
switch (type) {
case 'insert':
return isOkPacket(result) ? result.insertId : null;