Skip to content

Commit bae4083

Browse files
committed
fix: enhance timestamp handling in PostgresConnector and add corresponding tests
1 parent d68f79e commit bae4083

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

adminforth/dataConnectors/postgres.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,14 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
205205
if (!value) {
206206
return null;
207207
}
208-
if (field._underlineType == 'timestamp' || field._underlineType == 'int') {
209-
return dayjs(value.replace(' ', 'T') + 'Z').toISOString();
208+
if (field._underlineType == 'timestamp') {
209+
if (typeof value == 'string') {
210+
const normalizedValue = value.includes(' ') ? `${value.replace(' ', 'T')}Z` : value;
211+
return dayjs(normalizedValue).toISOString();
212+
}
213+
return dayjs(value).toISOString();
214+
} else if (field._underlineType == 'int') {
215+
return dayjs.unix(+value).toISOString();
210216
} else if (field._underlineType == 'varchar') {
211217
return dayjs(value).toISOString();
212218
} else {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AdminForthDataTypes } from '../../adminforth/index.js';
2+
import PostgresConnector from '../../adminforth/dataConnectors/postgres.js';
3+
4+
describe('PostgresConnector DATETIME normalization', () => {
5+
const connector = new PostgresConnector();
6+
const timestampField = {
7+
type: AdminForthDataTypes.DATETIME,
8+
_underlineType: 'timestamp',
9+
name: 'created_at',
10+
table: 'adminuser',
11+
} as any;
12+
13+
it('parses raw postgres timestamp strings as UTC', () => {
14+
expect(connector.getFieldValue(timestampField, '2026-05-09 17:09:21.622')).toBe('2026-05-09T17:09:21.622Z');
15+
});
16+
17+
it('accepts Date objects returned by pg timestamp parsers', () => {
18+
expect(connector.getFieldValue(timestampField, new Date('2026-05-09T17:09:21.622Z'))).toBe('2026-05-09T17:09:21.622Z');
19+
});
20+
});

0 commit comments

Comments
 (0)