Skip to content

Commit

Permalink
fix(postgres): parse timestamp dates less than year 1000 (#5071)
Browse files Browse the repository at this point in the history
### Bug
Parsing a timestamp column in postgresql with a date less than year 1000
will create an invalid Date when using the `forceUtcTimezone` set to
`true`.
### Reproduction
Created test for reproduction, run without the fix to
`PostgresSqlConnection.ts`.
### Fix
Use the date parsing function from `postgres-date` that is used by
default in `pg` for parsing dates but add the `Z` to force the utc
timezone. `new Date('0022-01-01 09:17:58.000Z')` when parsing the
timestamp column str creates an invalid date in js.

---------

Co-authored-by: Martin Adámek <banan23@gmail.com>
  • Loading branch information
Robert-Schirmer and B4nan committed Jan 5, 2024
1 parent 6667656 commit 63eb5c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/postgresql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
},
"dependencies": {
"@mikro-orm/knex": "5.9.7",
"pg": "8.11.3"
"pg": "8.11.3",
"postgres-date": "2.1.0"
},
"devDependencies": {
"@mikro-orm/core": "^5.9.7"
Expand Down
3 changes: 2 additions & 1 deletion packages/postgresql/src/PostgreSqlConnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TypeOverrides from 'pg/lib/type-overrides';
import parseDate from 'postgres-date';
import type { Dictionary } from '@mikro-orm/core';
import { AbstractSqlConnection, MonkeyPatchable, type Knex } from '@mikro-orm/knex';

Expand All @@ -20,7 +21,7 @@ export class PostgreSqlConnection extends AbstractSqlConnection {
ret.types = types as any;

if (this.config.get('forceUtcTimezone')) {
[1114].forEach(oid => types.setTypeParser(oid, str => new Date(str + 'Z'))); // timestamp w/o TZ type
[1114].forEach(oid => types.setTypeParser(oid, str => parseDate(str + 'Z'))); // timestamp w/o TZ type
ret.parseInputDatesAsUTC = true;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/features/schema-generator/timestamp.postgres.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';

@Entity()
class TimestampTest {

@PrimaryKey()
id!: number;

@Property({ columnType: 'timestamp' })
createdAtTimestamp!: Date;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [TimestampTest],
dbName: '5071',
forceUtcTimezone: true,
});
await orm.schema.refreshDatabase();
});
afterAll(async () => await orm.close(true));

test('postgres timestamp is correctly parsed', async () => {
const createdAt = new Date('0022-01-01T00:00:00Z');
const something = orm.em.create(TimestampTest, { id: 1, createdAtTimestamp: createdAt });
await orm.em.persistAndFlush(something);

const res = await orm.em.fork().find(TimestampTest, something.id);

expect(isNaN(res[0]!.createdAtTimestamp.getTime())).toBe(false);
expect(res[0]!.createdAtTimestamp.getTime()).toEqual(createdAt.getTime());
});
8 changes: 8 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63eb5c5

Please sign in to comment.