mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 22:17:14 +03:00
Fix postgres type cast
Postgres is handling numbers as 64bit and requires special handling to prevent returning numbers as string
This commit is contained in:
@@ -1,9 +1,19 @@
|
|||||||
import { Logger } from '../utils/logger';
|
import { Logger } from '../utils/logger';
|
||||||
import { IDatabase, QueryType } from './IDatabase';
|
import { IDatabase, QueryType } from './IDatabase';
|
||||||
import { Client, Pool } from 'pg';
|
import { Client, Pool, types } from 'pg';
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
|
// return numeric (pg_type oid=1700) as float
|
||||||
|
types.setTypeParser(1700, function(val) {
|
||||||
|
return parseFloat(val);
|
||||||
|
});
|
||||||
|
|
||||||
|
// return int8 (pg_type oid=20) as int
|
||||||
|
types.setTypeParser(20, function(val) {
|
||||||
|
return parseInt(val, 10);
|
||||||
|
});
|
||||||
|
|
||||||
export class Postgres implements IDatabase {
|
export class Postgres implements IDatabase {
|
||||||
private pool: Pool;
|
private pool: Pool;
|
||||||
|
|
||||||
@@ -51,31 +61,10 @@ export class Postgres implements IDatabase {
|
|||||||
case 'get': {
|
case 'get': {
|
||||||
const value = queryResult.rows[0];
|
const value = queryResult.rows[0];
|
||||||
Logger.debug(`result (postgres): ${JSON.stringify(value)}`);
|
Logger.debug(`result (postgres): ${JSON.stringify(value)}`);
|
||||||
if (value) {
|
|
||||||
for (const [key, v] of Object.entries(value)) {
|
|
||||||
if (!isNaN(v as any)) {
|
|
||||||
value[key] = parseFloat(v as string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.debug(`result (postgres): ${value}`);
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
case 'all': {
|
case 'all': {
|
||||||
let values = queryResult.rows;
|
let values = queryResult.rows;
|
||||||
if (values) {
|
|
||||||
values = values.map((row) => {
|
|
||||||
for (const [key, v] of Object.entries(row)) {
|
|
||||||
if (!isNaN(v as any)) {
|
|
||||||
row[key] = parseFloat(v as string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return row;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.debug(`result (postgres): ${values}`);
|
Logger.debug(`result (postgres): ${values}`);
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user