Skip to content

Commit bbc57b1

Browse files
committed
feat: enhance Postgres date handling by setting type parsers and adjusting field value processing
1 parent e5e3699 commit bbc57b1

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

adminforth/dataConnectors/postgres.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import AdminForthBaseConnector from './baseConnector.js';
55
import pkg from 'pg';
66
import { afLogger, dbLogger } from '../modules/logger.js';
77

8-
const { Client } = pkg;
8+
const { Client, types } = pkg;
9+
10+
// postgres-date (used by pg for OID 1114/1082) parses no-TZ strings with new Date(y,m,d,...)
11+
// which treats them as LOCAL server time. Return raw strings so getFieldValue can parse as UTC.
12+
types.setTypeParser(1114, (val) => val); // TIMESTAMP WITHOUT TIME ZONE
13+
types.setTypeParser(1082, (val) => val); // DATE
914

1015

1116
class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
@@ -200,7 +205,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
200205
return null;
201206
}
202207
if (field._underlineType == 'timestamp' || field._underlineType == 'int') {
203-
return dayjs(value).toISOString();
208+
return dayjs(value.replace(' ', 'T') + 'Z').toISOString();
204209
} else if (field._underlineType == 'varchar') {
205210
return dayjs(value).toISOString();
206211
} else {
@@ -212,7 +217,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
212217
if (!value) {
213218
return null;
214219
}
215-
return dayjs(value).toISOString().split('T')[0];
220+
return value;
216221
}
217222

218223
if (field.type == AdminForthDataTypes.BOOLEAN) {

0 commit comments

Comments
 (0)